How to get all the connected nodes and relationship of a particular node?

I have a graph with this hierarchy, so I want to find all nodes and relationships connected to a specific node.

I've tried this query :

MATCH path=(:Building {name: 'Trinity'})-[:CONTAIN_SITE*4]-()
RETURN path;

Which in turn is returning the path but not as expected, along with that remaining paths are also retrieved. So i need to get the particular entire path of a node named "Trinity" and should ends with "mdt1" and "device".

And also facing issue in retrieval of nodes in both(->, <-) directions.

Is there any way to achieve this?

When you query for a path, you will get a result for each path. Each path will contain all of the nodes and relationships along its path. The nodes and relationships in the collections of paths contain duplicates among the paths, due to the hierarchical nature of the data. The following query will get all the paths originating from one root node, consolidate all the nodes and relationships from all the paths, then perform some double unwinding to eliminate the duplicates using the DISTINCT clause. This is necessary since the collect(nodes(p)) operation results in a list of lists. The inner list is the nodes along one path. Same is true for the relationships. That explains why the double unwind is done twice.

match p=(n{name:'root'})-[*]-()
with n, collect(nodes(p)) as listOflistOfnodes, collect(relationships(p)) as listOfListOfrelationships
unwind listOflistOfnodes as list
unwind list as element
with n, collect(distinct element) as distinctNodes, listOfListOfrelationships
unwind listOfListOfrelationships as list
unwind list as element
return, distinctNodes, collect(distinct element) as distinctRelationships

Here is my test data:


Can you please explain it briefly, I'm not getting expected results.

Am I addressing your requirement? I am assuming you want the collection of nodes and relationships for the subgraph originating a root node.

The first line of the query results in rows representing each path anchored to the node of interest. The query binds the paths to the variable p, producing one result row for each path. You can get all the nodes along path p as a collection using the nodes(p) function and relationships using the relationships(p) function. Line 2 aggregates the node and relationship collections using the collect method to reduce the data from all the paths to one collection to meet your requirement. The issue here is that the collect method does not merge the individual lists from each path into a collection that represent the union of all the nodes and relationships. Instead, it appends each list into a new list representing. This is expected. The result is a list containing lists of nodes and the same for the relationships. The list of lists for the nodes and relationships also contain duplicates. The rest of the query manipulates theses lists to remove the duplicates and flatten them into lists of unique nodes and relationships.

Back to the query, step 2 passes the single root node to the next phase of the query, forcing the collect methods to aggregate over all the paths, like a group by clause in sql. The result of line 2 is a single row consisting of node n and two collections of lists. To flatten the list of lists and apply the distinct clause on rows, lines 3 and 4 unwinds the list of lists of nodes twice to get a stream of nodes. Line 5 recollects the stream of nodes with the distinct clause, resulting in a list of unique nodes. Lines 6 and 7 repeat the process for the relationships. The final result on line 8 consist of the root node, and one list of unique nodes from all the paths, and one list of the unique relationships from all the paths.

  1. match p=(n{name:'root'})-[*]-()
  2. with n, collect(nodes(p)) as listOflistOfnodes, collect(relationships(p)) as listOfListOfrelationships
  3. unwind listOflistOfnodes as list
  4. unwind list as element
  5. with n, collect(distinct element) as distinctNodes, listOfListOfrelationships
  6. unwind listOfListOfrelationships as list
  7. unwind list as element
  8. return, distinctNodes, collect(distinct element) as distinctRelationships

Can you explain your results? Can you provide a cypher query to create the subgraphs so we can test the query on it?

Here, to create the subgraphs firstly i'm creating each nodes and after that providing association by relating them.

//To create a node

CREATE (n:Location {name: 'Building'})

CREATE (n:Building {buildingId: 111, name: 'Trinity'})

After that creating a relationship between these two

//To create relationship

MATCH (s:Location) 
WHERE = 'Building' AND = 'Trinity' 

I'm getting this result as a graph

But expecting this as a results

If i provide the any parameter of particular node the path should be returned like here based on buildingId. So in this "Trinity" is the building name, "2" is floor number and "Abcd" & "Efgh" is room name with different room id. All these nodes comes under label known as Building.

And remaining nodes in the path device, device1, mdt1, gps and Tenant are created under different respective labels.

The reason you are getting a subset of the subgraph you want is because of the match in line 1. It specifies all nodes connected to your building node that are 1 relationship away. Change the match to p=(n:Building{name:'Trinity'})-[:CONTAIN_SITE*]-(). The '*' indicates any number of relationships between node 'n' and any other node connected through a series of 'CONTAIN_SITE' relationships.

You can learn more from the cypher documentation:

the cypher reference card is very handy too:

1 Like

Now getting all the connected paths

Is there any way to filter this query as to get the highlighted node path