WildBill
WildBill

Reputation: 9301

How to enumerate nodes and relationships along path returned via Cypher

I opened this question here: How to find specific subgraph in Neo4j using where clause to find a path of a certain criteria. Yet when I try to do things like get the relationship type I cannot.

For example I tried MATCH p = (n:Root)-[rs1*]->() WHERE ALL(rel in rs1 WHERE rel.relevance is null) RETURN nodes(p), TYPE(relationships(p))

But I get the error:

Type mismatch: expected Relationship but was Collection<Relationship>

I think I need to use a WITH clause but not sure.

Similarly I wanted the ID of a node but that also failed.

Upvotes: 0

Views: 247

Answers (1)

Gabor Szarnyas
Gabor Szarnyas

Reputation: 5057

The problem is that relationships returns a collection and the type function only works on a single relationship. There are two main approaches to solve this.

Use UNWIND to get a separate row for each relationship:

MATCH p = (n:Root)-[rs1*]->()
WHERE ALL(rel in rs1 WHERE rel.relevance is null)
WITH relationships(p) AS rs
UNWIND n, rs AS r
RETURN n, type(r)

Use extract to get the results in a list (in a single row per root node):

MATCH p = (n:Root)-[rs1*]->()
WHERE ALL(rel in rs1 WHERE rel.relevance is null)
WITH n, relationships(p) AS rs
RETURN n, extract(r IN rs | type(r))

Or even shorter:

MATCH p = (n:Root)-[rs1*]->()
WHERE ALL(rel in rs1 WHERE rel.relevance is null)
RETURN n, extract(r IN relationships(p) | type(r))

Upvotes: 1

Related Questions