Reza Shirazi
Reza Shirazi

Reputation: 381

Neo4j cypher query get all nodes between a start and end node including

I have created a model of Route/Stops(Rail/Bus)/Operators and trying to find a route by specifying the start/end stop. When I run the below query I am getting an extra stop node ("PUD") which I think should not be included in the result!! Appreciate if you let me know why the "PUD" is included and whether this is a right approach or not?

MATCH p = (a:Station)-[r:Goto|Operates|HasStop|Changeto*0..]-(c:Station) 
WHERE (a.name='LDS' AND 234 IN a.time) 
AND c.name='MVC' 
RETURN p

Model: Model

Result: Result

Upvotes: 3

Views: 1345

Answers (1)

Dave Bennett
Dave Bennett

Reputation: 11216

Your query is matching multiple paths in against your sample diagram. Because your query has multiple relationship types allowed in the match, the query is undirected and does not specify a shorted path, it will literally find every combination of relationships that get from a to c in your model.

If you were to change the query so it only matches Goto and Changeto relationship types then you would get the result you are looking for.

Alternatively, if you made your query directed or used shortestPath you would also get the result you are seeking.

MATCH p =(a:Station)-[r:Goto|Changeto*0..]-(c:Station)
WHERE (a.name='LDS' AND 234 IN a.time) 
AND c.name='MVC' 
RETURN p

Directed...

MATCH p = (a:Station)-[r:Goto|Operates|HasStop|Changeto*0..]->(c:Station) 
WHERE (a.name='LDS' AND 234 IN a.time) 
AND c.name='MVC' 
RETURN p

ShortestPath...

MATCH p = shortestPath((a:Station)-[r:Goto|Operates|HasStop|Changeto*0..]-(c:Station))
WHERE (a.name='LDS' AND 234 IN a.time) 
AND c.name='MVC' 
RETURN p

Upvotes: 3

Related Questions