Pass each matched record value to filter in Elasticsearch

For geo_distance query I'm using a constant value for distance. I need to make it dynamic. So I want to pass the above matched record radius value to distance.

Here's the code:

let searchRadius = '12KM'
        query: {
            bool: {
                must: {
                    match: {
                        companyName: {
                            query: req.text
                        }
                    }
                },
                filter: {
                    geo_distance: {
                        distance: searchRadius,//here I want to pass doc['radius']
                        location: {
                            lat: parseFloat(req.lat),
                            lon: parseFloat(req.lon)
                        }
                    }
                },
            }
        }

For each record, I have a different radius value. I want to pass doc['radius'] instead of constant searchRadius value.

I can hit two queries then iterate the values but it's not optimal. Can anyone suggest how can I pass each record value to geo_distance filter?

any suggestions plz...

I resolved like this

{
            query: {
              bool: {
                must: [
                  {
                    match: {
                      companyName: {
                        query: req.text
                      }
                    }
                  },
                  {
                    script: {
                      script: {
                        params: {
                          lat: parseFloat(req.lat),
                          lon: parseFloat(req.lon)
                        },
                        source: "doc['location'].arcDistance(params.lat, params.lon) / 1000 < doc['searchRadius'].value",
                        lang: "painless"
                      }
                    }
                  }
                ]
              }
            },
            sort: [
                {
                    _geo_distance: {
                        location: {
                            lat: parseFloat(req.lat),
                            lon: parseFloat(req.lon)
                        },
                        order: "asc",
                        unit:"km"
                    }
                }
            ],

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