密码:具有约束的最短路径 [英] Cypher: Shortest Path with Constraint

查看:69
本文介绍了密码:具有约束的最短路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行最短路径查询,如下所示:

I want to perform a shortest path query like the following:

START source=node:myIndex(name="<src>"), destination=node:myIndex(name = "<dst>")                                                                               
MATCH p = shortestPath(source-[:REL1*..5]-destination),
          source-[sourceRel:REL1]-m, 
          destination-[destRel:REL1]-k
WHERE sourceRel.a=<someValue> and destRel.a=<someOtherValue>                                                                                                      
RETURN NODES(p);

我想获得< src> < dst> 之间的最短路径,并限制属性 a 具有一个分别从src和dst到下一个节点的 first 关系上的确定值.

I want to get the shortest path between <src> and <dst> with the constraint that the property a has a certain value on the first relationship from src and dst respectively to the next node.

但是,neo4j仅返回找到的任何最短路径,而没有考虑我的约束.我究竟做错了什么?为最短路径查询在最短路径的第一个跳"上指定约束的正确方法是什么?

However, neo4j just returns any shortest path it finds without taking into account my constraint. What am I doing wrong? What is the correct way to specify constraints on the first "hop" of a shortest path for a shortest path query?

编辑:我正在使用Neo4j 1.8.2.

edit: I'm using Neo4j 1.8.2.

推荐答案

现在,在当前的Neo4j版本(感谢Wes)中无法知道我想做什么,我使用了一种解决方法来实现这一目标.首先,我为src和目标节点都获得了与约束匹配的下一个节点:

Now knowing what I want to do is not possible in the current Neo4j version (thanks Wes), I use a workaround to get this going. First I obtain both for the src and destination node the next node matching the constraint:

START n=node:myIndex(name="<nodename>")                             
MATCH n<-[rel:REL1]-m
WHERE rel.a=<somevalue>
RETURN m

像这样,我有src和dst的下一个"节点的两个节点ID.我现在使用计算这两个节点ID之间的最短路径,并在原始源之前加上原始目标节点以获取完整路径.它需要3个查询而不是1个查询,但可能不像Wes建议的那样昂贵.

Like this, I have the two node IDs of the "next" nodes of src and dst. I now use calculate the shortest path between those two node IDs and prepend the original source and append the original destination node to obtain the full path. It requires 3 queries instead of one but is probably not as expensive as the method Wes suggested.

这篇关于密码:具有约束的最短路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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