Distance feature query

Boosts the relevance score of documents closer to a provided origin date or point. For example, you can use this query to give more weight to documents closer to a certain date or location.

You can use the distance_feature query to find the nearest neighbors to a location. You can also use the query in a bool search’s should filter to add boosted relevance scores to the bool query’s scores.

Example request

Index setup

To use the distance_feature query, your index must include a date, date_nanos or geo_point field.

To see how you can set up an index for the distance_feature query, try the following example.

  1. Create an items index with the following field mapping:

    1. PUT /items
    2. {
    3. "mappings": {
    4. "properties": {
    5. "name": {
    6. "type": "keyword"
    7. },
    8. "production_date": {
    9. "type": "date"
    10. },
    11. "location": {
    12. "type": "geo_point"
    13. }
    14. }
    15. }
    16. }
  2. Index several documents to this index.

    ``` PUT /items/_doc/1?refresh { “name” : “chocolate”, “production_date”: “2018-02-01”, “location”: [-71.34, 41.12] }

    PUT /items/_doc/2?refresh { “name” : “chocolate”, “production_date”: “2018-01-01”, “location”: [-71.3, 41.15] }

  1. PUT /items/_doc/3?refresh
  2. {
  3. "name" : "chocolate",
  4. "production_date": "2017-12-01",
  5. "location": [-71.3, 41.12]
  6. }
  7. ```

Example queries

Boost documents based on date

The following bool search returns documents with a name value of chocolate. The search also uses the distance_feature query to increase the relevance score of documents with a production_date value closer to now.

  1. GET /items/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match": {
  7. "name": "chocolate"
  8. }
  9. },
  10. "should": {
  11. "distance_feature": {
  12. "field": "production_date",
  13. "pivot": "7d",
  14. "origin": "now"
  15. }
  16. }
  17. }
  18. }
  19. }
Boost documents based on location

The following bool search returns documents with a name value of chocolate. The search also uses the distance_feature query to increase the relevance score of documents with a location value closer to [-71.3, 41.15].

  1. GET /items/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match": {
  7. "name": "chocolate"
  8. }
  9. },
  10. "should": {
  11. "distance_feature": {
  12. "field": "location",
  13. "pivot": "1000m",
  14. "origin": [-71.3, 41.15]
  15. }
  16. }
  17. }
  18. }
  19. }

Top-level parameters for distance_feature

field

(Required, string) Name of the field used to calculate distances. This field must meet the following criteria:

origin

(Required, string) Date or point of origin used to calculate distances.

If the field value is a date or date_nanos field, the origin value must be a date. Date Math, such as now-1h, is supported.

If the field value is a geo_point field, the origin value must be a geopoint.

pivot

(Required, time unit or distance unit) Distance from the origin at which relevance scores receive half of the boost value.

If the field value is a date or date_nanos field, the pivot value must be a time unit, such as 1h or 10d.

If the field value is a geo_point field, the pivot value must be a distance unit, such as 1km or 12m.

boost

(Optional, float) Floating point number used to multiply the relevance score of matching documents. This value cannot be negative. Defaults to 1.0.

Notes

How the distance_feature query calculates relevance scores

The distance_feature query dynamically calculates the distance between the origin value and a document’s field values. It then uses this distance as a feature to boost the relevance score of closer documents.

The distance_feature query calculates a document’s relevance score as follows:

  1. relevance score = boost * pivot / (pivot + distance)

The distance is the absolute difference between the origin value and a document’s field value.

Skip non-competitive hits

Unlike the function_score query or other ways to change relevance scores, the distance_feature query efficiently skips non-competitive hits when the track_total_hits parameter is not true.