Reputation: 620
I'm creating test database for transport in a city. My goal find path betweeb any stops. I created this graph:
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});
When I try find short way:
match p=shortestPath((a)-[:TO*]-(c))
where a.Id=131 and c.Id=268
return p, length(p) limit 1
Or this query:
MATCH (p1:Tram {id: 131}), (p2:Tram {id: 263133}),
path = shortestpath((p1)-[:NEXT*]-(p2))
RETURN path
It's doesn't show any route. Can you please help me edit query?
P.S. I forgot add relation:
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)
Upvotes: 0
Views: 36
Reputation: 20185
You are trying to match shortest paths that have a TO
relationship, however your graph shows that the type of the relationship is NEXT
.
I replicated your graph here http://console.neo4j.org/r/boin78
And the following query is working as expected by just specifying the correct relationship type :
match p=shortestPath((a)-[:NEXT*]-(c))
where a.Id=131 and c.Id=268
return p, length(p)
limit 1
For the second query, you used the id
as property while your nodes have the Id
property name, the following is working :
MATCH (p1:Tram {Id: 131}), (p2:Tram {Id: 263133}),
path = shortestpath((p1)-[:NEXT*]-(p2))
RETURN path
Upvotes: 2