3
votes

Is it possible to SET a property on node and REMOVE it within a single cypher query without causing an actual disk write in Neo4j.

For example, I tried duplicating the n node in this query using n as errand but a SET on errand results to a SET on n and equally the REMOVE such the property is lost in the return value.

This is sort of my attempt at creating a transient property on a node.

String q = "MATCH (owner)-[:POSTED]->n WITH owner, n, n as errand, 
      CASE WHEN owner-[:RECOMMENDED]->n THEN 'BROADCASTED' 
      WHEN owner-[:POSTED]->n THEN 'POSTED' 
      WHEN owner-[:GOT_NOMINATED]->n THEN 'NOMINATED' 
      ELSE 'CONNECTED' 
      END AS relationship 
SET errand.meta = relationship 
REMOVE n.meta 
RETURN errand LIMIT 1";

However, when ever I check my domain object to see if the property was set by Neo4j, it returns null.

public class Errand {
     private String meta;

     Boolean isMetaSet () {
          return meta != null;
     }
}

Using SDN Neo4j I get my errand object like.

Errand single = template.query(q, null).to(Errand.class).singleOrNull();

System.out.println (single.isMetaSet());

returns false.

Is the answer in Neo4J create temp variable within Cypher applicable for Nodes?

I am currently using Neo4j in Embedded Mode with Neo4j, and my queries are run using the Neo4jTemplate class.

See the response to a similar question I asked on this issue at Set a transient property on a node neo4j

1
I would like to return a single node deserializable into my model object - F.O.O
Great answer to when trying to return a property. - F.O.O
This causes a null pointer exception because it is removed before it is returned. Is there something wrong with my cypher query? - F.O.O
Rephrased my question - F.O.O
Please update the question to show what code is populating the Errand object on the basis of that query. And have you tried not binding n twice, once to itself, once to errand in the WITH block? - FrobberOfBits

1 Answers

1
votes

Your query does not need a temporary node property or a special variable at all. (Also, as @FrobberOfBits said, your approach was flawed.)

The following query eliminates errand (which was just an alias for the n node), and also sets the n.meta property:

MATCH (owner)-[:POSTED]->n
SET n.meta =
      CASE WHEN owner-[:RECOMMENDED]->n THEN 'BROADCASTED' 
      WHEN owner-[:POSTED]->n THEN 'POSTED' 
      WHEN owner-[:GOT_NOMINATED]->n THEN 'NOMINATED' 
      ELSE 'CONNECTED' 
      END
RETURN n.meta LIMIT 1

(Since you are limiting to 1, there is no need to use DISTINCT.)