Reputation: 3
I have a Neo4J db that I query e.g. to obtain the shortest path between E and F (but it could be any sub graph of the database). I do this using the RNeo4J driver in R.
I would like to return the path(s) as an edge list to R using Cypher so that I can use use the edge list directly to create an Igraph object and plot it or analyze it.
I'm new to Cypher, but have been trying something in the line of the query below using the browser:
"MATCH p = shortestpath((n)-[*..5]->(m))
WHERE n.nodename = 'E' AND m.nodename = 'F'
WITH p
MATCH (f)-->(t)
WHERE f IN nodes(p) AND t IN nodes(p)
RETURN DISTINCT f.nodename, t.nodename"
This produces an edge list containing the shortest path (say E->G->F) but also includes a few nodes not on the shortest path (say I and C) but adjacent to it as (E->C, C->E, and G->I->F)...
Am I on the right track? How can I ensure that only the nodes in the shortest path is considered in my second MATCH statement? Is there a better way to do it? Any help will be highly appreciated.
/Jannik
Upvotes: 0
Views: 498
Reputation: 66967
This should get the names of the start and end nodes of every relationship in the shortest path, in path order:
MATCH shortestpath((n)-[rels*..5]->(m))
WHERE n.nodename = 'E' AND m.nodename = 'F'
UNWIND rels AS rel
RETURN STARTNODE(rel).nodename AS s, ENDNODE(rel).nodename AS e;
Upvotes: 2