Geo_point sort by distance

I would like to know what would be the best way to perform a search and performing a sorting by distance, today _geo_distance filters and does not sort. What would be the best solution in which you need to perform two "should" queries and return sorted by distance.

GET places/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"formattedAddress": {
"query": "santa marta",
"operator": "and"
}
}
},
{
"geo_distance": {
"distance": "5km",
"coords": {
"lat": -27.87,
"lon": -54.43
}
}
}
]
}
}
}

The best ways is using geo distance sorting:

https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-request-body.html#geo-sorting

This api of the sort "_geo_distance" does not sort but filters in all the tests I performed, I would like to perform the search in the fields informed in the search and sort by the search results.

What do you mean with filter? Could you provide an example? I would expect that the query bellow would apply the geo_distance and match filters and then it would sort those result by distance:

GET places/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "formattedAddress": {
              "query": "santa marta",
              "operator": "and"
            }
          }
        },
        {
          "geo_distance": {
            "distance": "5km",
            "coords": {
              "lat": -27.87,
              "lon": -54.43
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "coords": {
          "lat": -27.87,
          "lon": -54.43
        },
        "order": "asc",
        "unit": "km",
        "mode": "min",
        "distance_type": "arc",
        "ignore_unmapped": true
      }
    }
  ]
}

otherwise, how should you query to search for a term and give relevance in terms that the distance is close.

Just remove the geo_distance filter:

GET places/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "formattedAddress": {
              "query": "santa marta",
              "operator": "and"
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "coords": {
          "lat": -27.87,
          "lon": -54.43
        },
        "order": "asc",
        "unit": "km",
        "mode": "min",
        "distance_type": "arc",
        "ignore_unmapped": true
      }
    }
  ]
}

Thank you very much

1 Like

I'd also recommend filtering a bit by distance first.
I mean that if you have 10 million documents matching santa marta, it means that you have to sort 10 million points. That could take some time.

I did not test recently with all the optimizations that went into the geo part, but my experience has been to add a geo filter as well to narrow down the number of documents to sort on.
For example, if you know that you are not going to give results which are more than 10km away from -54.43,-27.87, you can add a filter of 10km range around this point.

This is what I described in this blog post. Have a look at " Searching by Geo Point" part.

HTH. @Ignacio_Vera correct me if this is not needed anymore with some internal optimizations.

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