Reputation: 5511
I have an extraordinarily basic question about Neo4j.
I've imported a simple dataset which contains a number of nodes as 'example_nodes' like so:
sourceId
, targetId
Thus, my database has a simple two column table of sources and targets.
How could I find the shortest path between an arbitrary sourceId
and targetId
?
My initial attempt is along the lines of:
MATCH (source:example_nodes),(target:example_nodes),
p = shortestPath((source)--(target))
WHERE (source.sourceId) = 1234 AND (target.targetId) = 5678
return p
Which returns no records, when I can clearly see the first line in my database is itself a single path:
{"sourceId":"1234","targetId":"5678"}
What am I doing wrong? Do I need to create all the relationships before I can run a query (as all I've done so far is imported the nodes and created indices)
Upvotes: 1
Views: 3937
Reputation: 23
A more compact and clean query would be:
match s=shortestPath((src {sourceId:1234})-[*]-(dst {targetId:5678})) return s
Upvotes: 0
Reputation: 66989
You should find the source
and target
first, and then invoke shortestpath
:
MATCH (source:example_nodes),(target:example_nodes)
WHERE source.sourceId = 1234 AND target.targetId = 5678
MATCH p = shortestPath((source)-[*]-(target))
return p;
If this query runs too long, try limiting the maximum path length searched. For example, use [*..8]
to limit the length to 8.
Upvotes: 5