cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Create a directional relationship, if there is no relationship exist

the current scenario is: There is two-person node A(absd) and B (Ali), now if A Met B, A will form a relationship "MET" e.g A-[: MET{time: "12/12/2019"]->B, now later if B MET A, B has to check is there any relationship exist, if yes, update the properties inside the relationship, else form a MET relationship with the property time with A

2X_5_57df231739393f6b720beb38c829f81cdf649847.png

2X_6_61761e7d6e0165763981fe2e24882618b12bd0d9.png

I am using this query to check A, B exist, if not form a relationship:

match (p:Person),(a)
where p.id=100 and a.id=2
merge (p)-[:HAS_MET]->(a)
return a, count(*)

Now How B check is there any relationship exists between B and A, if yes then update the property of relationship else create a new relationship.

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven
Try this:

match (a:Person) where a.id = 2
match (b:Person) where a.id = 4
optional match (b)-[r:HAS_MET]->(a)
WITH coalesce(r) as r1

//relationship exists..updating relationship property.....

CALL apoc.do.when(r1 is not null, 'match (b)-[r:HAS_MET]->(a) where b.id = 4 and b.id = 2 set r.time = "xxx" RETURN a, b') yield value
return value.a, value.b

//adding relationship.....

CALL apoc.do.when(r1 is null, 'MATCH (a:Person), (b:Person) WHERE a.id = 2 and b.id = 4  merge (b)-[:HAS_MET{time: "vvv"}]->(a) RETURN b, a') yield value
return value.a, value.b

View solution in original post

3 REPLIES 3

ameyasoft
Graph Maven
Try this:

match (a:Person) where a.id = 2
match (b:Person) where a.id = 4
optional match (b)-[r:HAS_MET]->(a)
WITH coalesce(r) as r1

//relationship exists..updating relationship property.....

CALL apoc.do.when(r1 is not null, 'match (b)-[r:HAS_MET]->(a) where b.id = 4 and b.id = 2 set r.time = "xxx" RETURN a, b') yield value
return value.a, value.b

//adding relationship.....

CALL apoc.do.when(r1 is null, 'MATCH (a:Person), (b:Person) WHERE a.id = 2 and b.id = 4  merge (b)-[:HAS_MET{time: "vvv"}]->(a) RETURN b, a') yield value
return value.a, value.b

Hi Ameya,

Finally with your help, i have sorted out the query. It will look like this.

match (a:Person) where a.id = 1
match (b:Person) where b.id = 2
optional match (a)-[a_b:HAS_MET]->(b)
optional match (a)<-[b_a:HAS_MET]-(b)
WITH *, coalesce(a_b) as r1, coalesce(b_a) as r2

//when A and B aleady Met and A->B exist
CALL apoc.do.when(r1 is not null and r2 is null, 'match (a)-[r:HAS_MET]->(b) WHERE a.id = 1 and b.id = 2 set r.time = "a met b" RETURN a, b') yield value
with value as firstSet, r1, r2, a, b

//when A and B already Met and A->B relation does not exist but B -> A exist
CALL apoc.do.when(r2 is not null and r1 is null, 'match (b)-[r:HAS_MET]->(a) WHERE a.id = 1 and b.id = 2 set r.time = "b met a " RETURN a, b') yield value
with value as secondSet, a, b, r1, r2

//When A and B exist, but no relation
CALL apoc.do.when(r1 is null and r2 is null, 'MATCH (a:Person), (b:Person) WHERE a.id = 1 and b.id = 2 merge (a)-[:HAS_MET{time: "firstTime"}]->(b) RETURN a, b') yield value
return a, b

You're on the right track with MERGE, now review the documentation on using ON MATCH SET and ON CREATE SET with MERGE, allowing conditional setting of properties in each situation.