3

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

Community
  • 1
  • 1
F.O.O
  • 4,730
  • 4
  • 24
  • 34
  • I would like to return a single node deserializable into my model object – F.O.O May 13 '15 at 18:55
  • Great answer to when trying to return a property. – F.O.O May 13 '15 at 18:56
  • 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 May 13 '15 at 19:54
  • Rephrased my question – F.O.O May 14 '15 at 12:41
  • 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 May 14 '15 at 13:04
  • That didn't work. I thought that would even out if I SET and REMOVE in a single query. Question updated. – F.O.O May 14 '15 at 13:55
  • So if you're saying `n` and `errand` are the same thing, why is this result suprising? You remove `errand.meta` and then return `n`, meaning `errand.meta` **should be null**. So if meta is null, then return meta != null returns false, and this behavior is expected. What is the problem here? – FrobberOfBits May 14 '15 at 15:07
  • Exactly, so this means you answer is not applicable for nodes. Your answer seems to work for an arbitrary property. – F.O.O May 15 '15 at 12:11

1 Answers1

1

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

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • You answer seems to deviate from the question. Could you please look into how we can set a temporary property on a NODE in Neo4j? – F.O.O May 15 '15 at 12:06
  • This is quite different from creating an arbitrary variable within Neo4j which is what @FrobberOfBits answered http://stackoverflow.com/questions/28375229 – F.O.O May 15 '15 at 12:07
  • The question's subject line is about temporary node properties, not variables -- they are completely different things. Also the question's first link points to a question whose accepted answer had to do with node properties. Finally, the query provided in the question did not actually need to do anything fancy to get back a single `relationship` value, so I provided a more direct query. Are you saying that your whole question actually was asking how to set the `meta` property? – cybersam May 15 '15 at 15:23
  • I've modified my answer to do that as well. – cybersam May 15 '15 at 15:30
  • A node n is has three properties persisted in database. You want to add a fourth temporary property meta to the node which is visible only in the returned node but not in the database how do we do it? – F.O.O May 16 '15 at 18:13