Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-30-2020 01:35 AM
Hi,
I am new to Neo4j. I am trying to build a basic friends graph DB.
I have created node:
MERGE (F1:Friend {FID:1,FName:'John'})
MERGE (F2:Friend {FID:2,FName:'Emil'})
MERGE (F3:Friend {FID:3,FName:'Alice'})
I have created relationship as below:
Match (f1:Friend{FID:1}),(f2:Friend{FID:2})
MERGE (f1)-[:Met {mdate:"2020-04-28"}]->(f2)
Match (f2:Friend{FID:2}),(f3:Friend{FID:3})
MERGE (f2)-[:Met {mdate:"2020-04-29"}]->(f3)
Match (f3:Friend{FID:3}),(f2:Friend{FID:2})
MERGE (f3)-[:Met {mdate:"2020-04-30"}]->(f2)
After a lot of browsing, I managed to write a query to fetch all friends who met between a particular date range. The query I used is below:
MATCH p = (a:Friend)-[rels:Met*]-(b:Friend) WHERE (a.FID<>b.FID) AND ALL(r in relationships(p) WHERE r.mdate >= "2020-04-27" AND r.mdate <= "2020-04-29")
AND apoc.coll.different(nodes(p))
WITH a, b, relationships(p) as rels, [rel in relationships(p) | rel.mdate] as contactDates
WHERE
apoc.coll.sort(contactDates) = contactDates
RETURN a.FID,b.FID,contactDates
The result I get is below:
a.FID b.FID contactDates
1 2 ["2020-04-28"]
1 3 ["2020-04-28", "2020-04-29"]
2 3 ["2020-04-29"]
2 1 ["2020-04-28"]
3 2 ["2020-04-29"]
Now,
(1) While the result is technically unique and correct, I would like to avoid duplicate relationships here. Means, I need 1 and 2 and not 2 and 1 again and 2 and 3 and not 3 and 2 again. As, if I met 2, it implies 2 met 1. Similarly for 2 and 3.
(2) Also when I returned * (RETURN *) and look at the graph, it has plotted the relationship between FID 3 and 2 for "2020-04-30" also, though it wasn't shown in the table or text returned. I am not sure why...
Looking for an answer...
Thanks in advance.
--Venky
04-30-2020 02:50 AM
Hi Venky,
I think the following query should solve your first issue:
MATCH p = (a:Friend)-[rels:Met*]->(b:Friend) WHERE (a.FID<>b.FID) AND ALL(r in relationships(p) WHERE r.mdate >= "2020-04-27" AND r.mdate <= "2020-04-29")
AND apoc.coll.different(nodes(p))
WITH a, b, relationships(p) as rels, [rel in relationships(p) | rel.mdate] as contactDates
WHERE
apoc.coll.sort(contactDates) = contactDates
RETURN a.FID,b.FID,contactDates
All the sessions of the conference are now available online