我在这里打开了这个问题:How to find specific subgraph in Neo4j using where clause找到某个标准的路径。然而,当我尝试做一些事情,比如得到关系类型,我不能。
例如,我尝试了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
条款,但不确定。
同样,我想要一个节点的ID,但也失败了。
答案 0 :(得分:1)
问题是relationships
返回一个集合,而type
函数仅适用于单个关系。有两种主要方法可以解决这个问题。
使用UNWIND
为每个关系获取单独的行:
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
在列表中获取结果(每个根节点在一行中):
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))