Sort Results by Matching Pair of GeoPoints within a single document

Hi Everyone,

We have an elastic index that contains the following field mapping:

"mappings": {
            "properties": {
                "destination": {
                    "type": "geo_point"
                },
                "name": {
                    "type": "text"
                },
                "source": {
                    "type": "geo_point"
                }
            }
        },

Question:

We want results sorted by radius of 100 KM of both the Source and Destination of the same document. So the idea is matching results based on closest to Source and Destination. May I get some help how to write this Search Query to give me this type of result? Do you have sample queries?

Thank you very much in advance

Just wanted to follow up if anyone has any insights how we can acheive this? The document mapping can be any structure. The key point is that the document contains a source and destination and being able to use two gepoints in the query to order results? Is this possible

Hi @ty.mivance

I don't have in-depth knowledge of geo queries but what I would try in your scenario would be to use a sort script. I would write a script that was capable of calculating the distance of the points from the radius.
I believe it would work, but on the other hand you could lose performance because we are using script in the query.

GET my_index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": """
         //script distance points
         """,
        "params": {
          "radius": 100
        }
      },
      "order": "asc"
    }
  }
}

I appreciate the answer. I am hoping someone can help me write this using standard elastic query to acheive this goal?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.