0

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 * 
DarqMoth
  • 603
  • 1
  • 13
  • 31

1 Answers1

1

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;
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Thanks! What if there are several sub-graphs of this kind in a database which are disconnected from each other? In other words: there are several trees in a forest, where each tree has a root different from other tree roots. Nodes in all these distinct subtrees are connected with one and the same relation. What query will return all these trees? – DarqMoth Apr 18 '14 at 20:06
  • 1
    The same query will match all the subgraphs. Try it out. – cybersam Apr 18 '14 at 20:10