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.

Shortest path between nodes have relationship properties as list of timestamps

neo4j desktop 1.2.3 , trying the following query in the browser

The graph represents the call history between two persons and the relationship CALLED has property timestamp. This property is a list of timestamps.

The below code gives the shortest path, irrespective of the timestamp

MATCH (ms:Person { name: 'Martin Sheen' }),(cs:Person { name: 'Charlie Sheen' }), p = shortestPath((ms)-[:CALLED*]-(cs))
WITH p
WHERE length(p)> 1
RETURN p

I want to know the shortest path after time t1 or between time t1 and t2. I am not able to filter list properties using the Cypher query. But the below query doesn't work.

MATCH (ms:Person { name: 'Martin Sheen' }),(cs:Person { name: 'Charlie Sheen' }), p = shortestPath((ms)-[r:CALLED*]-(cs))
WITH p, r as relationships(p)
WHERE length(p)> 1 and all(r IN relationships(p) WHERE (time in r WHERE time > datetime({year:2019,month:3,day:31})))
RETURN p

Any help is appreciated.

1 ACCEPTED SOLUTION

How about this then:

MATCH (ms:Person { name: 'Martin Sheen' }), (cs:Person { name: 'Charlie Sheen' })
CALL algo.shortestPath(ms, cs, null,{
nodeQuery:'MATCH (p:Person) RETURN id(n) as id',
relationshipQuery:'MATCH (p1:Person)-[r:CALLED]->(p2:Person) where any(x in r.time where x > datetime({year:2019,month:3,day:31})) RETURN id(p1) as source, id(p2) as target',
graph:'cypher'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost

View solution in original post

4 REPLIES 4

shan
Graph Buddy

You can possibly use cypher projection in algo.shortestPath. Here's a sample:

MATCH (ms:Person { name: 'Martin Sheen' }), (cs:Person { name: 'Charlie Sheen' })
CALL algo.shortestPath(ms, cs, null,{
nodeQuery:'MATCH (p:Person) RETURN id(n) as id',
relationshipQuery:'MATCH (p1:Person)-[r:CALLED]->(p2:Person) where r.time > datetime({year:2019,month:3,day:31}) RETURN id(p1) as source, id(p2) as target',
graph:'cypher'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost

Hi Shan,

The relationship CALLED has properties as list I am unable to loop through using two WHERE and check each timestamp.

...
relationshipQuery:'MATCH (p1:Node)-[re:IS_NEIGHBOR]->(p2:Node) where r IN re.timestamp WHERE time in r WHERE time > datetime({year:2019,month:3,day:31}) RETURN id(p1) as source, id(p2) as target',
...

Not sure if I am doing the right thing here.
Thanks @shan

How about this then:

MATCH (ms:Person { name: 'Martin Sheen' }), (cs:Person { name: 'Charlie Sheen' })
CALL algo.shortestPath(ms, cs, null,{
nodeQuery:'MATCH (p:Person) RETURN id(n) as id',
relationshipQuery:'MATCH (p1:Person)-[r:CALLED]->(p2:Person) where any(x in r.time where x > datetime({year:2019,month:3,day:31})) RETURN id(p1) as source, id(p2) as target',
graph:'cypher'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost

Thank you Shan. It works but for other who might have similar question while comparing timestamps make sure both the timestamps are in same format

One more step before the final solutions.
How can get the graph to show or json results, I am able to get the Node ids using stream
I looked at the docs and tried to find in forum, but I could not find any.