Home > Software design >  How to filter multiple fields along with geo_distance in opensearch
How to filter multiple fields along with geo_distance in opensearch

Time:07-04

I have a elastic-search query currently which only filters based on km radius with geo-distance. I want to search multiple fields along with geo-distance filter.

here is my query:

{
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "distance": "5km",
          "coordinate": {
            "lat": 3.131779652034938,
            "lon": 101.6846340901202
          }
        }
      }
    }
  }
}

Here is reponse i got from elatic-search:

{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 11,
            "relation": "eq"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "drivers",
                "_id": "Oj07uYEBZP3C5zS1xL7e",
                "_score": 0.0,
                "_source": {
                    "id": 1,
                    "name": "Dwayne Johnson",
                    "area": "Brick Fields",
                    "coordinate": {
                        "lat": 3.130143699375071,
                        "lon": 101.68389642814743
                    },
                    "vehicles": [
                        {
                            "vehicle_type": "Car",
                            "weight_capacity": "500",
                            "vehicle_number": "WM3219"
                        },
                        {
                            "vehicle_type": "Large Truck",
                            "weight_capacity": "1000",
                            "vehicle_number": "VAV3234"
                        }
                    ],
                    "deposit": "1500"
                }
            }]
      }
}

I want to filter deposit value from driver object along with geo-distance using elastic search query. Can anyone please help me with the query.

CodePudding user response:

If you want an additional filter as I understand from the question

{
   "query":{
      "bool":{
         "must":[
            {
               "term":{
                  "deposit":"<some value>"
               }
            }
         ],
         "filter":{
            "geo_distance":{
               "distance":"5km",
               "coordinate":{
                  "lat":3.131779652034938,
                  "lon":101.6846340901202
               }
            }
         }
      }
   }
}
  • Related