Alexis Pister
Alexis Pister

Reputation: 499

Can we match subgraphs in cypher?

I know that in cypher it is possible to match paths like this :

MATCH p=(n)-[r]-(m)
RETURN p

But is there a way to do something similar but for subgraphs, i.e groups of nodes and relationships which are not expressible in the form of a path ?

I have something like this in my mind :

MATCH subgraph=[(n)-[r]-(m)-[s]-(p), (m)-[j]-(u)]
RETURN subgraph

Upvotes: 0

Views: 451

Answers (2)

Graphileon
Graphileon

Reputation: 5385

MATCH path1=(n)-[r]-(m)-[s]-(p),
      path2=(m)-[j]-(u)
RETURN apoc.coll.toSet(nodes(path1),nodes(path2)) AS subGraphNodes,
       apoc.coll.toSet(rels(path1),rels(path2)) AS subGraphRelationships

Would return the distinct nodes and rels for each matching subgraph.

If you just need the virtual subgraph, you can use

apoc.graph.fromPaths()

Upvotes: 0

Brakebein
Brakebein

Reputation: 2237

Maybe the APOC path expander procedures serve your needs. You have much control over how the graph should be traversed.

However, you don't get a subgraph into one variable as you have in mind:

CALL apoc.path.expandConfig(n, { ... }) YIELD path will return multiple rows (one row for each found path).

CALL apoc.path.subgraphAll(n, { ... }) YIELD nodes, relationships will return lists of all found nodes and relationships.

Upvotes: 1

Related Questions