将每个匹配的记录值传递到 Elasticsearch 中的过滤器

发布于 2025-01-10 21:37:14 字数 969 浏览 1 评论 0原文

对于 geo_distance 查询,我使用距离常量值。我需要让它变得动态。所以我想将上面匹配的记录半径值传递给距离

代码如下:

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)
                        }
                    }
                },
            }
        }

对于每条记录,我都有不同的半径 > 价值。我想传递 doc['radius'] 而不是常量 searchRadius 值。

我可以点击两个查询然后迭代这些值,但这不是最佳的。谁能建议我如何将每个记录值传递给 geo_distance 过滤器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2025-01-17 21:37:14

我已经从这个答案中解决了。

下面是使用脚本查询的代码

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"
                      }
                    }
                  }
                ]
              }
            },

,来自更多详细信息:

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/query-dsl-script-query.html

I have resolved from this answer.

Heres the code

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"
                      }
                    }
                  }
                ]
              }
            },

Using script Query, from more details:

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/query-dsl-script-query.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文