How to enumerate nodes and relationships along path returned via Cypher

戏子无情 提交于 2019-12-12 03:46:42

问题


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.


回答1:


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))


来源:https://stackoverflow.com/questions/41895481/how-to-enumerate-nodes-and-relationships-along-path-returned-via-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!