2
votes

I've set up a Neo4j Graph DB.

And I've the following Nodes and Relationship Types:

Left panel - Database Information

My question is how I get the Path between 2 Nodes from Station only with relationship type "BUS"?

Station(1)->BUS->Station(31)

Thank you.

1

1 Answers

1
votes

You can use a simple Cypher query like this:

MATCH p = (:Station {id:1})-[:BUS*]->(:Station {id:31})
RETURN p

This query will MATCH the path (p) between (:Station{id:1}) and (:Station {id:31}) following only relationships of type :BUS with any length (denoted by the * after the relationship type).

You can also specify the number of hops the query will follow, putting it after the *(Example: [:BUS*1..3] will follow at least 1 and at most 3 hops). Take a look in the MATCH clause docs.

EDIT:

From Hans comment:

I am interested in the shortest path

You can use shortestPath() function. This way:

MATCH (a:Station {id:1}), (b:Station {id:31}), p = shortestPath((a)-[*]-(b))
WHERE ALL (r IN relationships(p) WHERE type(r) = 'BUS')
RETURN p

From the docs:

Predicates used in the WHERE clause that apply to the shortest path pattern are evaluated before deciding what the shortest matching path is.

The query above will return the shortest path between a and b evaluating only :BUS relationships.