首页 > 解决方案 > 如何搜索多个 geo_distance 查询

问题描述

我想使用 geo_distance 查询(使用 ES 7.3)搜索多个位置。例如:

在柏林搜索 +10km

搜索慕尼黑 +10km

并给我一个综合结果。

基本上我在这里试过这个:

一个阵列geo_distance不起作用。很想在这方面得到一些帮助:)

标签: elasticsearch

解决方案


搜索多个位置映射的简单示例:

PUT location
{
    "mappings": {
        "properties" : {
            "pin" : {
                "type" : "geo_point"
            }
        }
    }
}

数据:

[
      {
        "_index" : "location",
        "_type" : "_doc",
        "_id" : "rhn8f20BIb7c4jbYhr3Z",
        "_score" : 1.0,
        "_source" : {
          "pin" : {
            "lat" : 40.73,
            "lon" : -74.1
          }
        }
      },
      {
        "_index" : "location",
        "_type" : "_doc",
        "_id" : "rxn8f20BIb7c4jbYz709",
        "_score" : 1.0,
        "_source" : {
          "pin" : {
            "lat" : 40.717,
            "lon" : -73.99
          }
        }
      }
    ]

询问:

GET location/_search
{
  "query": {
    "bool": {
      "should": [    ---> multiple filters in should clause , either one of these has to be true
        {
          "geo_distance" : {
                "distance" : "1km",
                "pin" : {
                    "lat" : 40.73,
                    "lon" : -74.1
                }
            }
        },
        {
          "geo_distance" : {
                "distance" : "1km",
                "pin" : {
                    "lat" : 40.717,
                    "lon" : -73.99
                }
            }
        }
      ]
    }
  }
}

推荐阅读