我在这里打开了这个问题: 如何使用where子句查找Neo4j中的特定子图以查找某个条件的路径。 然而,当我尝试做一些事情,比如得到关系类型我不能。
例如,我尝试了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,但也失败了。
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.
最满意答案
问题是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))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))更多推荐
发布评论