19
votes

In Cypher in Neo4J, given two nodes, if there's no relationship between them, I'd like to create a relationship (of type Foo) with a weight property of one. If this relationship already exists, I'd like to increment its weight property.

Is there a good way to do this in a single Cypher query? Thanks!

Edit: Some additional details: The nodes are already created, unique, and in an index.

3
Are you okay with using a SNAPSHOT version of neo4j? In neo4j 1.8 you can update the graph using Cypher, but not in any other versions.Nicholas
So essentially you are looking to scan all nodes and if connected, increment, if not, create relationship?Nicholas
Nicholas, thanks for response. I added some additional details; the nodes are unique and indexed, so I don't think I should have to scan all the nodes. But, other than that, your comment is correct: if connected, increment, if not, create relationship.Newtang

3 Answers

45
votes

This is exactly why we added CREATE UNIQUE in 1.8.

START a=node(...), b=node(...)
CREATE UNIQUE a-[r:CONNECTED_TO]-b
SET r.weight = coalesce(r.weight?, 0) + 1

Read more about CREATE UNIQUE here, the question mark here, and coalesce here.

11
votes

To complete Andres answer, question mark at the end of a property is now an error with Neo4j 2. So request will be :

MATCH a, b
WHERE a(...) AND b(...)
CREATE UNIQUE a-[r:CONNECTED_TO]->b
SET r.weight = coalesce(r.weight, 0) + 1
4
votes

For future reference, CREATE UNIQUE has since been deprecated (see here). It looks like you can do something similar with MATCH and MERGE:

                MATCH (a:Person {name: 'Wonder Woman'})
                MERGE (b:Person {name: 'Aries'})
                MERGE (a)-[r:FOUGHT]->(b)
                ON CREATE SET r.weight = 1
                ON MATCH SET r.weight = r.weight + 1

So here, Wonder Woman fought Aries at least once, else it will increment the weight.