简体   繁体   中英

Add a Temporary Property on a Node Cypher

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

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 .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM