Neo4J查询没有显示路径

时间:2016-07-24 13:06:25

标签: neo4j

我正在为城市中的交通创建测试数据库。 我的目标是在任何一站点之间找到路径。 我创建了这张图:

create (Stop_13_1:Tram {Id: 131}),
(Stop_13_2:Tram {Id: 132}),
(Stop_26_1:Tram {Id: 261}),
(Stop_26_2:Tram {Id: 262}),
(Stop_26_3_13_3:Tram {Id: 263133}),
(Stop_26_4_13_4:Tram {Id: 264134}),
(Stop_26_5_13_5:Tram {Id: 265135}),
(Stop_26_6_13_6:Tram {Id: 266136}),
(Stop_26_7_13_7:Tram {Id: 267137}),
(Stop_26_8:Tram {Id: 268}),
(Stop_7_1:Trollebus {Id: 71}),
(Stop_7_2:Trollebus {Id: 72}),
(Stop_7_3:Trollebus {Id: 73}),
(Stop_7_4:Trollebus {Id: 74}),
(Stop_7_5:Trollebus {Id: 75}); 

当我尝试找到简短的方法时:

match p=shortestPath((a)-[:TO*]-(c))
where a.Id=131 and c.Id=268
return p, length(p) limit 1

或者这个查询:

MATCH (p1:Tram {id: 131}), (p2:Tram {id: 263133}),
      path = shortestpath((p1)-[:NEXT*]-(p2))
RETURN path

它没有显示任何路线。 你能帮我编辑查询吗?

P.S。我忘了添加关系:

MATCH (Stop_13_1 {Id: 131}),
  (Stop_13_2 {Id: 132}),
  (Stop_26_1 {Id: 261}),
  (Stop_26_2 {Id: 262}),
  (Stop_26_3_13_3 {Id: 263133}),
  (Stop_26_4_13_4 {Id: 264134}),
  (Stop_26_5_13_5 {Id: 265135}),
  (Stop_26_6_13_6 {Id: 266136}),
  (Stop_26_7_13_7 {Id: 267137}),
  (Stop_26_8 {Id: 268}),
  (Stop_7_1 {Id: 71}),
  (Stop_7_2 {Id: 72}),
  (Stop_7_3 {Id: 73}),
  (Stop_7_4 {Id: 74}),
  (Stop_7_5 {Id: 75})
  MERGE         (Stop_13_1)-    [:NEXT{distance:4.7,route:13,transport:'tram',direct:'down'}]->(Stop_13_2)
  MERGE         (Stop_13_2)-[:NEXT{distance:4.7,route:13,transport:'tram',direct:'up'}]->(Stop_13_1)
  MERGE         (Stop_13_2)-[:NEXT{distance:3.7,route:13,transport:'tram',direct:'down'}]->(Stop_26_3_13_3)
  MERGE         (Stop_26_3_13_3)-[:NEXT{distance:3.7,route:13,transport:'tram',direct:'up'}]->(Stop_13_2)
  MERGE         (Stop_26_1)-[:NEXT{distance:5.8,route:26,transport:'tram',direct:'down'}]->(Stop_26_2)
  MERGE         (Stop_26_2)-[:NEXT{distance:5.8,route:26,transport:'tram',direct:'up'}]->(Stop_26_1)
  MERGE         (Stop_26_2)-[:NEXT{distance:2.5,route:26,transport:'tram',direct:'down'}]->(Stop_26_3_13_3)
  MERGE         (Stop_26_3_13_3)-[:NEXT{distance:2.5,route:26,transport:'tram',direct:'up'}]->(Stop_26_2)
  MERGE         (Stop_26_3_13_3)-[:NEXT{distance:3.1,route:26,route:13,transport:'tram',direct:'down'}]->(Stop_26_4_13_4)
  MERGE         (Stop_26_4_13_4)-[:NEXT{distance:3.1,route:26,route:13,transport:'tram',direct:'up'}]->(Stop_26_3_13_3)
  MERGE         (Stop_26_4_13_4)-[:NEXT{distance:5.8,route:26,route:13,transport:'tram',direct:'down'}]->(Stop_26_5_13_5)
  MERGE         (Stop_26_5_13_5)-[:NEXT{distance:5.8,route:26,route:13,transport:'tram',direct:'up'}]->(Stop_26_4_13_4)
  MERGE         (Stop_26_5_13_5)-[:NEXT{distance:10.8,route:26,route:13,transport:'tram',direct:'down'}]->(Stop_26_6_13_6)
  MERGE         (Stop_26_6_13_6)-[:NEXT{distance:10.8,route:26,route:13,transport:'tram',direct:'up'}]->(Stop_26_5_13_5)
  MERGE         (Stop_26_6_13_6)-[:NEXT{distance:2.5,route:26,route:13,transport:'tram',direct:'down'}]->(Stop_26_7_13_7)
  MERGE         (Stop_26_7_13_7)-[:NEXT{distance:2.5,route:26,route:13,transport:'tram',direct:'up'}]->(Stop_26_6_13_6)
  MERGE         (Stop_26_5_13_5)-[:NEXT{distance:0.6,transport:'walking',direct:'down'}]->(Stop_7_2)
  MERGE         (Stop_7_2)-[:NEXT{distance:0.6,transport:'walking',direct:'up'}]->(Stop_26_5_13_5)
  MERGE         (Stop_26_8)-[:NEXT{distance:1,route:26,transport:'tram',direct:'down'}]->(Stop_26_7_13_7)
  MERGE         (Stop_26_7_13_7)-[:NEXT{distance:1,route:26,transport:'tram',direct:'up'}]->(Stop_26_8)
  MERGE         (Stop_7_1)-[:NEXT{distance:2.2,route:7,transport:'trolleybus',direct:'down'}]->(Stop_7_2)
  MERGE         (Stop_7_2)-[:NEXT{distance:2.2,route:7,transport:'trolleybus',direct:'up'}]->(Stop_7_1)
  MERGE         (Stop_7_2)-[:NEXT{distance:1.6,route:7,transport:'trolleybus',direct:'up'}]->(Stop_7_3)
  MERGE         (Stop_7_3)-[:NEXT{distance:2.5,route:7,transport:'trolleybus',direct:'up'}]->(Stop_7_4)
  MERGE         (Stop_7_4)-[:NEXT{distance:3.1,route:7,transport:'trolleybus',direct:'down'}]->(Stop_7_5)
  MERGE         (Stop_7_5)-[:NEXT{distance:4.4,route:7,transport:'trolleybus',direct:'down'}]->(Stop_7_2)

1 个答案:

答案 0 :(得分:2)

您尝试匹配具有TO关系的最短路径,但是您的图表显示关系的类型为NEXT

我在这里复制了您的图表http://console.neo4j.org/r/boin78

通过指定正确的关系类型,以下查询正常工作:

match p=shortestPath((a)-[:NEXT*]-(c)) 
where a.Id=131 and c.Id=268 
return p, length(p) 
limit 1

对于第二个查询,您使用id作为属性,而您的节点具有Id属性名称,以下是有效的:

MATCH (p1:Tram {Id: 131}), (p2:Tram {Id: 263133}), 
path = shortestpath((p1)-[:NEXT*]-(p2)) 
RETURN path