T.S.
T.S.

Reputation: 1019

Limiting ShortestPath in Cypher to nodes with specific properties

I am trying to figure out how to limit a shortest path query in cypher so that it only connects "Person" nodes containing a specific property.

Here is my query:

MATCH p = shortestPath( (from:Person {id: 1})-[*]-(to:Person {id: 2})) RETURN p

I would like to limit it so that when it connects from one Person node to another Person node, the Person node has to contain a property called "job" and a value of "engineer."

Could you help me construct the query? Thanks!

Upvotes: 0

Views: 137

Answers (1)

cybersam
cybersam

Reputation: 66957

Your requirements are not very clear, but if you simply want one of the people to have an id of 1 and the other person to be an engineer, you would use this:

MATCH p = shortestPath( (from:Person {id: 1})-[*]-(to:Person {job: "engineer"}))
RETURN p;

This kind query should be much faster if you also created indexes for the id and job properties of Person.

Upvotes: 2

Related Questions