neo4j / graphql

A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations.
https://neo4j.com/docs/graphql-manual/current/
Apache License 2.0
509 stars 149 forks source link

Sort nodes by point distance from another point #3900

Open Sapulidi opened 1 year ago

Sapulidi commented 1 year ago

Bascially, I would like to be able to get the 100 (limit) people who are closest to a certain location. And if possible sort them afterwards but that can be done in post processing if needed.

I am building an application that shows me nodes who are close to my location. Let's say I have 5 nodes: A, B, C, D and E. Node A is closest to me and node E is the farthest away. I expect the following query to return A, B and C but instead it returns A, B and E.

{
  "where": {
    "location_LT": {
      "point": {
        "longitude": xxx,
        "latitude": xxx
      },
      "distance": 10000
    },
  },
  "options": {
    "limit": 3,
    "sort": [
      {
        "location": "ASC"
      }
    ]
  }
}

I just found out through the Discord server that the sort is not related to the location (which I thought it was). I currently get a fairly good results if I make 2 separate queries LT and GT but if I add more filtering, some nodes nearby that should be in the results get dropped.

An example of what I'd like might look like this (if you implement this I trust you will use better naming than me).

{
  "where": {
    "location_NEIGHBOUR": {
      "point": {
        "longitude": xxx,
        "latitude": xxx
      },
    },
  },
  "options": {
    "limit": 3,
    "sort": "NEAR" | "FAR"
  }
}

For more context (Neo4J Discord thread): https://discord.com/channels/787399249741479977/1148644450097168524

Thanks!

helenesanche commented 1 year ago

Hi @Sapulidi thank you very much for raising this interesting feature. We will take into consideration prioritising this feature as we're collecting more geospatial use cases.

In the meantime, what I could suggest is to create an index on the point property and this should enable cypher statements to return back the data in a sorted ordered. The issue with this workaround is that it's quite CPU expensive and depending on the amount of data you have it can get quite slow.

I suggest you take a look at our cypher documentation which may help you finding a workaround until we can properly embed this capability in the library.