在 Neo4j Cypher 查询中仅返回简单路径 [英] Returning only simple paths in Neo4j Cypher query
本文介绍了在 Neo4j Cypher 查询中仅返回简单路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定如下查询:
START n = node(123)
MATCH p = n-[r:LIKES*..3]->x
RETURN p;
我通过上述查询获得的结果路径包含循环.
The result paths that I get with the query above contain cycles.
如何只返回简单的路径?
How can I return only simple paths?
鉴于此示例:
- 如何避免具有重复节点的路径,例如:[Neo、Morpheus、Trinity、Morpheus、Neo]
推荐答案
指定路径的唯一性是 cypher 的一个计划功能.
Specifying the uniqueness of paths is a planned feature of cypher.
所以现在我们必须确定路径中没有节点是重复的.
So right now we have to ascertain that no node is a duplicate in the path.
有一个 ALL
谓词必须对集合(路径是)的所有元素都成立.使用 filter
,您可以提取集合的元素,以确保某个条件成立.
There is an ALL
predicate that must hold true for all elements of a collection (which a path is).
And with filter
you can extract the elements of an collection for that a certain condition holds true.
START neo=node(1)
MATCH path= neo-[r:KNOWS*..4]->other
WHERE ALL(n in nodes(path) where
1=length(filter(m in nodes(path) : m=n)))
RETURN neo, LENGTH(path) AS length, EXTRACT(p in NODES(path) : p.name), other
ORDER BY length
所以我所做的是:
- 对于路径
n
的所有节点 - 过滤等于
n
的节点的路径 - 确定该集合的
长度
- 使用
ALL
断言每个n
必须是 1
- For all nodes of the path
n
- Filter the path for the nodes that are equal to
n
- determine the
length
of that collection - assert with
ALL
that it has to be ONE for eachn
参见:http://console.neo4j.org/r/dpalbl
这篇关于在 Neo4j Cypher 查询中仅返回简单路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文