Sorting by distance with minimum distance

I'm trying to sort results by distance. Something like this works perfectly:

_geo_distance: {
  "nested_path": ...,
  "location": [..., ...],
  "order": "asc",
  "unit": "m",
  "mode": "min",
  "distance_type": "sloppy_arc"
}

However I would like to use an minimum distance (if under minimum distance all elements should have the same weight in result). So I've tried

_geo_distance: {
  "nested_path": ...,
  "location": {
    "type": "circle",
    "coordinates": [..., ...],
    "radius": "100m"
  }
  "order": "asc",
  "unit": "m",
  "mode": "min",
  "distance_type": "sloppy_arc"
}

But that doesn't seem to work. Is there an easy way to accomplish what I want to do? Do I have to go with scoring?

Anyone has an idea?

ping

I would go with scoring. The function_score query supports decay functions which will allow you to score documents based on a geo distance. These decay functions allow you to define an offset, which would be a radius for which documents get a maximum score. Any document with a distance larger than this offset will get a lower score, based on how you configure the decay function.

The docs have an example: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-decay

Sweet, thank you very much for your reply! I'll hopefully get to implementing this in the next few weeks and will try to post our final solution here.

Something like this seems to get the job done:

      "functions": [
        {
          "exp": {
            "venues.location": {
              "origin": [
                0.5, 
                0.5
              ], 
              "decay": "0.5", 
              "scale": "20km", 
              "offset": "100m"
            }
          }
        },
        ...
      ]

This works for nested objects as well if you use include_in_root on the nesting.

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