I have a pretty large network that contains privileged information. Because of this i can't share the exact graph but in its simplest form it can be abstracted as follows.
(Person{name:str, age:int})-[:KNOWS]-(Person{name:str, age:int})
I would like to know the closest person that is over an specific age.
I have the following query which works, but i dont think its running very efficient, because the queries take a long time even if there is a close connection that matches the requirements.
MATCH path=((p:Person{name:'Bob'})-[:KNOWS*BFS..]-(p2:Person))
WHERE p2.age > 80
RETURN p2 ,size(path) AS distance
ORDER BY distance
LIMIT 1
If my understanding is correct this will:
- Build a list with all the people over 80 that know (someone who knows, etc etc) Bob. Which is huge in a densly connected network and therefor inefficient.
- Find one of the shortest paths between Bob and this person.
- Repeat for all connected persons over 80.
- Find the shortest one of these paths.
Is there a way to stop the BFS search as soon as a person with age > 80 is found and not build the rest of all the paths?
I would also like to be able to expand this search to more than one Person over the age of 80.
The steps would be as follows:
- Match Bob
- Search all the people that Bob knows
- If one of these people is over 80 -> return this person
- If not -> search all the people that know someone that knows Bob.
- Repeat 3-5 recursivly
Thanks in advance!