elasticsearchrelevance

Java API for distance_feature in Elastic Search


I am trying to implement a relevance boost for date and geo-location fields using elastic search distance_feature. What Java API corresponds to distance_feature query?

The following query works fine on Kibana:

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "mountain",
            "fields": [
              "description$string"
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "distance_feature": {
                  "field": "location$location",
                  "pivot": "1km",
                  "origin": [
                    -35.58,20.4
                  ],
                  "boost": 1
                }
              },
              {
                "distance_feature": {
                  "field": "date_established$date",
                  "pivot": "10d",
                  "origin": "now",
                  "boost": 10
                }
              }
            ]
          }
        }
      ]
    }
  },
  "_source": {
    "includes": [
      "title$string",
      "location$location",
      "date_established$date"
    ]
  }
}```

I do not want to use the decay function for this case.

Solution

  • You can use the DistanceFeatureQueryBuilder class:

    For the geo point:

    GeoPoint geo = new GeoPoint(-35.58, 20.4);
    DistanceFeatureQueryBuilder.Origin origin = new DistanceFeatureQueryBuilder.Origin(geo);
    DistanceFeatureQueryBuilder dfqb = new DistanceFeatureQueryBuilder("location$location", origin, "1km")
    

    For the date:

    Origin origin = new Origin("now");
    DistanceFeatureQueryBuilder dfqb = new DistanceFeatureQueryBuilder("date_established$date", origin, "10d")
    

    I've filed an issue requesting to add a factory method for distance_feature in QueryBuilders.