如何枚举通过Cypher返回的路径上的节点和关系(How to enumerate nodes and relationships along path returned via Cypher)

编程入门 行业动态 更新时间:2024-10-28 20:25:10
如何枚举通过Cypher返回的路径上的节点和关系(How to enumerate nodes and relationships along path returned via Cypher)

我在这里打开了这个问题: 如何使用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))

更多推荐

本文发布于:2023-07-31 12:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1343783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   路径   关系   Cypher   enumerate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!