1
votes

Given are 2 nodes of type "System" and i want to set a property and create a missing relation (and a node if it not exist)

Example:

MATCH (s:System {Name: 'system1'})
SET s.ClusterPartner = 'system2'
WITH s
WHERE has(s.ClusterPartner)
CREATE UNIQUE (s)-[:ClusteredWith]-(c:System {Name: s.ClusterPartner})

Before that query i've 2 Nodes of type "System" and both have their Name property set correctly ('system1' and 'system2') without any relation between them.

This query will not just create the missing relation, it also creates a new node with the name 'system2', even when it exists.

Maybe i've not understood the docs, but i think it should just create the missing relation in my case and only relation and a new node when 'system2' node does not exists.

What is the problem with this query?

Update1: using the merge approach:

MATCH (s:System {Name: 'system1'})
SET s.ClusterPartner = 'system2'
WITH s
WHERE has(s.ClusterPartner)
MERGE (c:System {Name : s.ClusterPartner})
MERGE (s)-[:ClusteredWith]->(c)

will also create a new unlabled node.

Update2: using the merge and create approach:

MATCH (s:System {Name: 'system1'})
SET s.ClusterPartner = 'system2'
WITH s
WHERE has(s.ClusterPartner)
MERGE (c:System {Name : s.ClusterPartner})
CREATE UNIQUE (s)-[:ClusteredWith]-(c)

looks like this one works as expected

1
Can you share a console with data on which the MERGE query doesn't work as expected? I ran CREATE (s:System { Name: 'system1' }),(:System { Name: 'system2' }) and pasted your query from "update1" and that seems to work fine.jjaderberg
Thanks for jumping in. I don't know what the difference would be as well. Would be interesting to know!tstorms
@jjaderberg: i cannot reporduce it on console.neo4j.org .. but on my local Neo4j 2.0 on Win 8.1 64bitdna
What is recommend? MERGE or CREATE UNIQUE?dna
MERGE as CREATE UNIQUE is deprecated in Neo4j 2.0.tstorms

1 Answers

2
votes

I think you'd only need the following query. Let me explain it line by line:

MATCH (s:System {Name : 'system1'})
MERGE (c:System {Name : 'system2'})
MERGE (s)-[:ClusteredWith]->(c)

MATCH (s:System {Name : 'system1'}) searches for a System labeled node with name 'system1'. If it does not exist, your query will exit.

MERGE (c:System {Name : 'system2'}) will find or create a System labeled node with name 'system2'.

MERGE (s)-[:ClusteredWith]-(c) will, in your scenario, create a ClusteredWith relationship.

Make sure to put a direction in the relationship. Also, refer to property values with ":" instead of "==".