Links of Link with depth

Hi There,

I am a newborn baby in Neo4j world. I have downloaded version 4.0.3. I tried browsing for an answer to my requirement, but couldn't find one that exactly matches my need.

I have created 10 person nodes as below:

CREATE (U1:Person {UID:'1',Name:'Person1'}),
(U2:Person {UID:'2',PName:'Person2'}),
(U3:Person {UID:'3',PName:'Person3'}),
(U4:Person {UID:'4',PName:'Person4'}),
(U5:Person {UID:'5',PName:'Person5'}),
(U6:Person {UID:'6',PName:'Person6'}),
(U7:Person {UID:'7',PName:'Person7'}),
(U8:Person {UID:'8',PName:'Person8'}),
(U9:Person {UID:'9',PName:'Person9'}),
(U10:Person {UID:'10',PName:'Person10'})

I have created a relationship "Met" with property "Date" as below:
CREATE (P1:Person {UID:'1'})-[:Met {Date:'25-Apr-2020 14:00'}]->(P2:Person {UID:'2'}),
(P1)-[:Met {Date:'25-Apr-2020 14:00'}]->(P3:Person {UID:'3'}),
(P2)-[:Met {Date:'25-Apr-2020 14:00'}]->(P1),
(P3)-[:Met {Date:'25-Apr-2020 14:00'}]->(P1)

CREATE (P2:Person {UID:'2'})-[:Met {Date:'26-Apr-2020 10:00'}]->(P4:Person {UID:'4'}),
(P2)-[:Met {Date:'26-Apr-2020 10:00'}]->(P5:Person {UID:'5'}),
(P4)-[:Met {Date:'26-Apr-2020 10:00'}]->(P2),
(P5)-[:Met {Date:'26-Apr-2020 10:00'}]->(P2)

CREATE (P3:Person {UID:'3'})-[:Met {Date:'26-Apr-2020 14:00'}]->(P6:Person {UID:'6'}),
(P3)-[:Met {Date:'26-Apr-2020 14:00'}]->(P7:Person {UID:'7'}),
(P6)-[:Met {Date:'26-Apr-2020 14:00'}]->(P3),
(P7)-[:Met {Date:'26-Apr-2020 14:00'}]->(P3)

CREATE (P4:Person {UID:'4'})-[:Met {Date:'27-Apr-2020 8:00'}]->(P8:Person {UID:'8'}),
(P4)-[:Met {Date:'27-Apr-2020 8:00'}]->(P9:Person {UID:'9'}),
(P8)-[:Met {Date:'27-Apr-2020 8:00'}]->(P4),
(P9)-[:Met {Date:'27-Apr-2020 8:00'}]->(P4)

CREATE (P8:Person {UID:'8'})-[:Met {Date:'27-Apr-2020 10:00'}]->(P10:Person {UID:'10'}),
(P10)-[:Met {Date:'27-Apr-2020 10:00'}]->(P8)

When I run "match(n) return n". I can see some hanging nodes without a relationship though each one of them is linked.

Query:
(1) I wanted to know if the graph displayed is how it will be?
(2) I want to retrieve all the friend's friend with level and date they met by passing a friend id
for e.g
PersonID FriendID Depth
1 2 1
1 3 1
2 4 2
2 5 2
3 6 2
3 7 2
4 8 3
4 9 3
8 10 4

If I can also get the Date Met, it will be great.

Thanks in advance...

Hi vramkri,

welcome to the Neo4j Community!

Concerning your first question

After you create your 10 nodes in the first place, you need to MATCH these nodes in order to create relationships between them. All your relationships are created between "newly created" nodes and not the nodes you want them to be. A solution would be:

MATCH (P1:Person {UID:'1'}), (P2:Person {UID:'2'}), (P3:Person {UID:'3'}), (P4:Person {UID:'4'}), (P5:Person {UID:'5'}), (P6:Person {UID:'6'}), (P7:Person {UID:'7'}), (P8:Person {UID:'8'}), (P9:Person {UID:'9'}), (P10:Person {UID:'10'})
CREATE (P1)-[:Met {Date:'25-Apr-2020 14:00'}]->(P2),
(P1)-[:Met {Date:'25-Apr-2020 14:00'}]->(P3),
(P2)-[:Met {Date:'25-Apr-2020 14:00'}]->(P1),
(P3)-[:Met {Date:'25-Apr-2020 14:00'}]->(P1), 
(P2)-[:Met {Date:'26-Apr-2020 10:00'}]->(P4),
(P2)-[:Met {Date:'26-Apr-2020 10:00'}]->(P5),
(P4)-[:Met {Date:'26-Apr-2020 10:00'}]->(P2),
(P5)-[:Met {Date:'26-Apr-2020 10:00'}]->(P2), 
....

This way you are creating relationships between the existing nodes and not creating new ones. Your graph will then look like this:

As an annotation: You will see that there are always 2 relationships between any two nodes. Of course if Person A meets Person B at a certain date, this is also true vize versa. However, you should only have one relationship between the nodes.

If you have your cleaned graph, the following query will achieve what you want:

MATCH p = (a:Person)-[:Met*]-(b:Person) RETURN a.UID AS PersonID, b.UID AS FriendID, length(p) AS depth

How do you want to get the date into that table? if a friend is more than 1 hops away, there is no date that the two have met...

I hope this helped you.

Regards,
Elena

Hi Elena,

Many thanks to you for your quick response

I could see all the nodes linked. But when I try to run the query
MATCH p = (a:Person)-[:Met*]-(b:Person) RETURN a.UID AS PersonID, b.UID AS FriendID, length(p) AS depth

to retrieve all the friend's friend with level and date they met by passing a friend id, it goes into infinite loop. Even if I change it a little bit by adding the UID filter, it doesn't work as intended.

QUERY:

MATCH p = (a:Person {UID:'1')-[:Met*]-(b:Person) RETURN a.UID AS PersonID, b.UID AS FriendID, length(p) AS depth

Message in Status Bar

"Started streaming 118840 records after 3 ms and completed after 11 ms, displaying first 100000 rows."

And, for your query "How do you want to get the date into that table?", I want the Query to return the Datetime property of "Met" relationship when they meet.
For e.g, if P1 meets P2 on 24 Apr 2020 and P2 meets P3 on 25 Apr 2020, I want the result as

Friend Date Level
P2 24 Apr 2020 1 --> P2 is Direct friend P1
P3 25 Apr 2020 2 --> P3 is P2's friend

Thanks once again.

Hi vramkri,

have you deleted the second pair of edges? If you are working with the two-way relationships you wanted to have in the beginning, the query will always run infinitely.

The graph I was running the query on is:

As for the second problem: the table that you gave is a different table. Putting it into the same table is more tedious. It also depends on what you want to do with the table. A simple (not nice) query to get the information is

MATCH p = (a:Person)-[:Met*]-(b:Person) with collect(relationships(p)) as rels, collect (nodes(p)) as nodes, a,b,p RETURN a.UID AS PersonID, b.UID AS FriendID, length(p) AS depth, rels, nodes

This will first show you the graph but you can click on "Table" to see the table with the dates and the nodes that belong to every path.

Hi Elena,

Thanks again...

I understand your point of two way relationship. Will do some changes, check and revert.

Hi Elena,

Thanks a lot. My query worked after making it a one way relationship.