Reputation: 1713
I have given one or more start nodes (by ID) and I need to expand over one or more hops and return one result with an array of distinct nodes and and array of distinct relationships.
I can do this either via apoc.path.expand()
or apoc.path.subgraphAll()
, but either way it produces multiple rows for each expanded path, and therefore may contain duplicate nodes. To reduce the multiple rows into one row, I have used collect()
with apoc.coll.toSet()
and apoc.coll.flatten()
to remove duplicates from the nodes
and relationships
array:
apoc.path.subgraphAll:
MATCH (n) WHERE id(n) IN $ids
CALL apoc.path.subgraphAll(n, { minLevel: 1, maxLevel: 2 }) YIELD nodes, relationships
WITH collect(nodes) as nodes, collect(relationships) as relationships
RETURN apoc.coll.toSet(apoc.coll.flatten(nodes)) as nodes, apoc.coll.toSet(apoc.coll.flatten(relationships)) as relationships
apoc.path.expand:
MATCH (n) WHERE id(n) IN $ids
CALL apoc.path.expand(n, null, null, 1, 2) YIELD path
WITH collect(nodes(path)) as nodes, collect(relationships(path)) as relationships
RETURN apoc.coll.toSet(apoc.coll.flatten(nodes)) as nodes, apoc.coll.toSet(apoc.coll.flatten(relationships)) as relationships
Is there another way to remove the duplicates from the two arrays or to query the nodes and relationships?
Upvotes: 1
Views: 104
Reputation: 66999
Your use case does not need APOC at all. This may work for you:
MATCH (n)-[r*..2]-(m)
WHERE ID(n) IN $ids
WITH COLLECT(n)+COLLECT(m) AS nodes0, COLLECT(DISTINCT r) AS relationships
UNWIND nodes0 AS node
RETURN COLLECT(DISTINCT node) as nodes, relationships
The query uses a variable-length relationship pattern to find all relevant paths of length 1 or 2. Then it collects all the nodes in the paths, unwinds them, and returns a collection of distinct nodes.
As shown, returning a collection of distinct relationships is straightforward.
Upvotes: 0