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.

Bidirectional Relationship query based on time

Hello Team,

I am writing a cypher query to find out how many people one person met with another. A person has a bidirectional relationship HAS_MET which indicates the number of counts they met each other as well as a timestamp.

query looks like this

//ahmad contact of contact in last 14 days
match (p:Person)-[rel1:HAS_MET]->()-[rel2:HAS_MET]->(other)
with p,other,rel1.timeStamp as firstContactTime, rel2.timeStamp as secondContactTime
where p.id=1 and datetime() > firstContactTime >datetime("2020-04-03T19:32:24") and datetime() > secondContactTime >datetime("2020-04-03T19:32:24")
return p, other

The above query will provide a response of Ahmad's contact and contact of a contact.

Now I am modifying the query to look like this

MATCH (p:Person)-[:HAS_MET*2]->(other)
WHERE p.id=1 and NOT (p)-[:HAS_MET]->(other)
return other, p

In this query how do I access timestamp property of HAS_MET relationship?

2X_2_23793dadcdfb93ce0d7ef803cbefd8435cbd24c5.png

1 ACCEPTED SOLUTION

it is accessible in several ways. I suspect you may be most interested in just the last relationship in each path, not all the relationships? if that is the case you can explicitly access the last relationship in each path like this

MATCH t=(p:Person)-[:HAS_MET*2]->(other)
with last(relationships(t)) as r
return r.timeStamp

View solution in original post

2 REPLIES 2

it is accessible in several ways. I suspect you may be most interested in just the last relationship in each path, not all the relationships? if that is the case you can explicitly access the last relationship in each path like this

MATCH t=(p:Person)-[:HAS_MET*2]->(other)
with last(relationships(t)) as r
return r.timeStamp

Thanks Joel, it works,
finally query look like this, as per your suggestion

MATCH a= (p:Person)-[:HAS_MET*2]->(other)
with *, last(relationships (a)) as rel
WHERE p.id=1 and NOT (p)-[:HAS_MET]->(other) and datetime() >rel.timeStamp>datetime("2020-04-03T19:32:24")
return other, p, rel

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online