如何枚举通过Cypher返回的路径上的节点和关系

时间:2017-01-27 13:49:12

标签: neo4j cypher

我在这里打开了这个问题: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,但也失败了。

1 个答案:

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