如何使用密码查询找到所有最长的路径? [英] how to find all the longest paths with cypher query?

查看:45
本文介绍了如何使用密码查询找到所有最长的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个密码查询,以查找与STATUS ="on"属性相互关联的节点之间的所有最长路径,这是我到目前为止所做的:

I want to write a cypher query which finds all the longest paths among nodes which have relationship with STATUS="on" property with each other,this is what I have done so far:

start n=node(*) 
match p = n-[r:INCLUDE*..]->m 

with n,MAX(length(p)) as l 
match p = n-[r:INCLUDE*..]->m 
WHERE all(rel in r 
 where rel.status='on' AND (length(p) = l) )
return p,l 

它返回3条路径,长度分别为1,2和3,不仅是最长的路径,我的查询应该只找到最长的路径,我的意思是如果有8条路径适合我的第一个where条件(where rel.status='on'),长度为1,2,3,3,4,6,6,6,仅返回长度为6的三个路径.

It returns 3 paths with 1,2 and 3 length,not only the longest path,my query should find only the longest paths,I mean if there are 8 paths which suit to my first where condition ( where rel.status='on') ,with the length of 1,2,3,3,4,6,6,6 ,only the three paths with the length of 6 should be returned.

我该怎么办?

请指导我,我是neo4j的新手,尝试了很多,但除了头晕以外什么都没有,我将非常感谢您的帮助.

please guide me,I am new to neo4j,and tried a lot but have not got anything except dizziness,I will be so thankful for your help.

推荐答案

尝试将您的关系属性条件上移至第一个路径匹配,否则您将在未使用该条件过滤的路径上计算最大长度.然后将路径和最大长度带入查询的第二行,这样就不必再次匹配所有路径.您可以在WITH子句中收集路径以携带它们,然后在返回时根据路径长度进行过滤.尝试类似

Try moving up your relationship property criterion to the first path match, or you'll be calculating the max length on paths that are not filtered with that criterion. Then carry the paths and the max length into the second leg of the query so you don't have to match all the paths again. You can collect the paths to carry them in the WITH clause, and then filter on path length when you return. Try something like

START n=node(*)
MATCH p=n-[rels:INCLUDE*]->m 
WHERE ALL (rel IN rels 
  WHERE rel.status='on') 
WITH COLLECT(p) AS paths, MAX(length(p)) AS maxLength 
RETURN FILTER(path IN paths 
  WHERE length(path)= maxLength) AS longestPaths

这篇关于如何使用密码查询找到所有最长的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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