I am using rest api of neo4j in my php application , my requirement is to find the related nearest nodes from one node say "2" and The nodes need to find are related using relation "road_to".
Its like finding nearest locations from one location at level one and then two and so on.All locations are attached using path "road_to".
I have cypher query to find but
1) It gives nodes repeatedly which i dont need.
2) I need to give limit to what extent nodes are required to display,here in query i have to give ()-[:road_to]->() again and again for which i need to give some limit for levels
start n=node(2) Match (n)-[r:road_to]->()-[:road_to]->()-[:road_to]->(foaf) return r,foaf,n
Any help would be appreciated .Thanx
So the simplest way to collapse multiple hops is by using a limited variable length path, like this:
Notice that I got rid of the
START
clause in favor ofMATCH
withWHERE id(n)=2
. They're basically the same though. Here the operator[:road_to*1..3]
matches between 1 and 3 hops of the:road_to
relationship. Put whichever limits you like in there, and read more about this approach here in the "variable relationships" subsection.If you want to find the SHORTEST link that meets some criteria, then you can do that like this:
Here we're binding the path to
p
, so if you want the individual relationships inside of that path, you can get them with other cypher functions. Also notice that I'm looking for a particularfoaf
, not just any. In general when you're looking for the shortest path you're looking for the path to a particular node. If you have many nodes linked by:road_to
then without this extra criteria, you'd just get one hop.More on shortestPath can be found here.