查找具有附加条件的所有最短路径的查询不返回任何记录

时间:2017-05-29 19:46:19

标签: neo4j cypher

我必须找到A和B之间的所有最短路径以及附加属性。

此查询只返回一条路线(这是真的,只有一条路线有3部分):

MATCH (darjeeling { name: 'Darjeeling' }),(sandakphu { name: 'Sandakphu' }), 
    paths = allShortestPaths((darjeeling)-[*]-(sandakphu))
RETURN paths

结果:

{"start":{"identity":1,"labels":["town"],"properties":{"name":"Darjeeling"}},"end":{"identity":3,"labels":["peak"],"properties":{"altitude":"12400 ft","name":"Sandakphu"}},"segments":[{"start":{"identity":1,"labels":["town"],"properties":{"name":"Darjeeling"}},"relationship":{"identity":2,"start":1,"end":0,"type":"trek","properties":{"distance":15,"name":"Darjeeling-Rimbik","winter":"true","summer":"true"}},"end":{"identity":0,"labels":["town"],"properties":{"name":"Rimbik"}}},{"start":{"identity":0,"labels":["town"],"properties":{"name":"Rimbik"}},"relationship":{"identity":3,"start":0,"end":2,"type":"trek","properties":{"distance":18,"name":"Rimbik-BhikeBhanja","winter":"true","summer":"true"}},"end":{"identity":2,"labels":["village"],"properties":{"name":"BhikeBhanja"}}},{"start":{"identity":2,"labels":["village"],"properties":{"name":"BhikeBhanja"}},"relationship":{"identity":4,"start":2,"end":3,"type":"trek","properties":{"distance":4,"name":"BhikeBhanja-Sandakphu","winter":"true","summer":"true"}},"end":{"identity":3,"labels":["peak"],"properties":{"altitude":"12400 ft","name":"Sandakphu"}}}],"length":3}

此路线的所有部分都将属性冬季设置为true,但如果我想在查询中添加此类条件,则无效:

MATCH (darjeeling { name: 'Darjeeling' }),(sandakphu { name: 'Sandakphu' }), 
    paths = allShortestPaths((darjeeling)-[*]-(sandakphu))
WHERE ALL (p IN relationships(paths) WHERE p.winter = true)
RETURN paths

我的问题是,为什么第二个查询没有返回结果,即使A和B之间有路由,这是最短路径,并且此路由上的所有路径都将属性winter设置为true?

2 个答案:

答案 0 :(得分:1)

将此形式化为答案。

看起来你有字符串属性而不是布尔属性。这种情况对您有用:

WHERE p.winter = "true"

如果要将这些更改为布尔属性,则需要匹配与此属性的所有关系并使用:

SET p.winter = (p.winter = "true")

答案 1 :(得分:0)

我相信你需要为路径做另一个MATCH,如下所示:

MATCH (darjeeling { name: 'Darjeeling' }), (sandakphu { name: 'Sandakphu' })
MATCH paths = allShortestPaths((darjeeling)-[*]-(sandakphu))
WHERE ALL (p IN relationships(paths) WHERE p.winter = true)
RETURN paths