如何枚举通过Cypher返回的路径上的节点和关系 [英] How to enumerate nodes and relationships along path returned via Cypher
问题描述
我在这里打开了一个问题:如何查找Neo4j中的特定子图,使用where子句查找特定条件的路径.然而,当我尝试做类似获取关系类型的事情时,我却无法做到.
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.
例如,我尝试了MATCH p = (n:Root)-[rs1*]->()
WHERE ALL(rel in rs1 WHERE rel.relevance is null)
RETURN nodes(p), TYPE(relationships(p))
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))
但是我得到了错误:
Type mismatch: expected Relationship but was Collection<Relationship>
我认为我需要使用WITH
子句,但不确定.
I think I need to use a WITH
clause but not sure.
类似地,我想要一个节点的ID,但这也失败了.
Similarly I wanted the ID of a node but that also failed.
推荐答案
问题是relationships
返回一个集合,而type
函数仅适用于单个关系.解决此问题的方法主要有两种.
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.
使用 UNWIND
可以单独获取每个关系的行:
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)
使用 extract
获得结果列表中(每个根节点在单行中):
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))
或更短:
MATCH p = (n:Root)-[rs1*]->()
WHERE ALL(rel in rs1 WHERE rel.relevance is null)
RETURN n, extract(r IN relationships(p) | type(r))
这篇关于如何枚举通过Cypher返回的路径上的节点和关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!