在Neo4j Cypher查询中仅返回简单路径 [英] Returning only simple paths in Neo4j Cypher query

查看:508
本文介绍了在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谓词,该谓词必须对集合的所有元素(路径为true)成立. 并使用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
  • 的节点的路径
  • 确定该集合的length
  • 使用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 each n

请参阅: http://console.neo4j.org/r/dpalbl

这篇关于在Neo4j Cypher查询中仅返回简单路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆