Neo4J: find a sub-graph of arbitrary depth with nodes connected by a given set of relations?

StackOverflow https://stackoverflow.com/questions/23160713

  •  05-07-2023
  •  | 
  •  

Question

How to build a Neo4J query that:

1) Will return all nodes in a sub-graph of arbitrary depth with nodes connected by a given set of relations?

For example in Cypher-like syntax:

MATCH (*)-[r1:FRIEND_OF AND r2:COLLEAGUE_WITH]->(*) RETURN * 
Was it helpful?

Solution

This query will return just the nodes, as you stated in your question:

MATCH (n)-[:FRIEND_OF|COLLEAGUE_WITH*]->(m)
RETURN n, m;

If you also want the relationships:

MATCH (n)-[r:FRIEND_OF|COLLEAGUE_WITH*]->(m)
RETURN n, r, m;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top