Krishna Shetty
Krishna Shetty

Reputation: 1441

Neo4j modelling - sorting nodes ordered by distance

I am using Neo4j with PHP. In my project, I have restaurant nodes. Each node has latitude, longitude and taxonomy properties.

I need to return the restaurant nodes matching user's given taxonomy with results ordered by distance from user's location (that is nearest restaurant at the first).

What is the easiest solution?

I have worked on Mongo DB and Elasticsearch,this is very easy to achieve there using special indexing. But I could not find a straightforward way in Neo4j.

Upvotes: 2

Views: 1229

Answers (3)

Michael Hunger
Michael Hunger

Reputation: 41676

Besides the aforementioned Neo4j-Spatial, in Neo4j 3.0 there is also a built in distance() function.

See this GraphGist:

http://jexp.github.io/graphgist/idx?dropbox-14493611%2Fcypher_spatial.adoc

So if you find and match your restaurants some way you can order them by distance:

MATCH (a:Location), (b:Restaurant)
WHERE ... filtering  ...
RETURN b
ORDER BY distance(point(a),point(b)) 

Upvotes: 2

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

There are a couple of solutions :

Upvotes: 2

Stefan Armbruster
Stefan Armbruster

Reputation: 39915

Neo4j Spatial features distance queries (among lots of other things) and also cares about ordering.

Upvotes: 1

Related Questions