0
votes

I have a database containing millions of nodes and edge data and I want to get all the nodes and relationships data between two specified nodes. Below is the sample data for the graph which has 7 nodes and 7 relationships.

To traverse from 1st node to 7th node I can use the variable length relationship approach and can get the nodes and relationships in between the first and 7th nodes (but in this approach we need to know the number of relationships and nodes between 1st and 7th node). For using variable length relationship approach we have to specify the number where we will get the end node and it traverses in one direction. But in my case I know the start and end node and don't know how many relationships and nodes are in between them. Please suggest how I can write a Cypher query for this case.

I have used the APOC spanning tree procedure where it returns ‘path’ from the 1st and 7th element, but it does not return the nodes and relationships. Can I get nodes and relationships data in return using the spanning tree procedure and how?

Is there any other way to get all nodes and relations between two nodes without using the APOC procedure?

Here is query with apoc procedure:

MATCH (start:temp {Name:"Joel"}), (end:temp {Name:"Jack"}) CALL apoc.path.spanningTree(start,{terminatorNodes:[end]}) YIELD path return path

Note: In our graph database nodes can have multi direction relations.

[Sample nodes and relationships snapshot] : https://i.stack.imgur.com/nN9hk.png

2
Are you interested in finding the shortest path between two nodes?Tomaž Bratanič
Yes @Tomaz, I need the path between the two specified nodes, but I require the nodes and relationships in return on the specified path which I am not getting with the APOC procedure response. It will be very useful for me if you can help me out in getting the APOC procedure response in terms of nodes and relationships OR it will be more useful if there is a Cypher query available for my case where I do not know how much relationships and node will be there in between the start and end node. Thank you in advance.SOMESH NANDKISHOR PANDE

2 Answers

0
votes

Might be better to use shortestPath operator to find the shortest path between two nodes.

MATCH (start:temp {Name:"Joel"}), (end:temp {Name:"Jack"})
MATCH p=shortestPath((start)-[*]->(end))
RETURN nodes(p) as nodes, relationships(p) as rels
0
votes

I assume you do not want to have duplicates in your result, so my approach would be this

MATCH (start:temp {Name:"Joel"}), (end:temp {Name:"Jack"})
MATCH p=shortestPath((start)-[*]->(end))
UNWIND nodes(p) AS node
UNWIND relationships(p) AS rel
RETURN COLLECT(DISTINCT node) as nodes, COLLECT(DISTINCT rel) as rels