【问题标题】:Elasticsearch query for values that exist in multiple geo locationsElasticsearch 查询存在于多个地理位置的值
【发布时间】:2019-05-08 15:53:36
【问题描述】:

我有一个包含如下文档的索引:

{
    "name": "some name",
    "location: { "type": "Point", "coordinates": [3.295, 6.673]}
}

我可以进行地理多边形查询没问题。但现在我想搜索两个地理多边形并让它只返回那些在两者中具有相同“名称”值的文档。因此,如果两个地理多边形中都存在 "name":"Jason Smith",则将返回这些文档。但是如果“Jason Smith”只存在于一个,那么它就不会被返回。我不知道如何编写这个查询,所以任何帮助将不胜感激。

这是我现在正在使用的查询:

{
    "query":{
        "bool":{
            "filter":{
                "bool":{
                    "should":[
                        {"geo_distance": {"coordinates":[32.4809, 12.0422],"distance": "100 m"}},
                        {"geo_distance": {"coordinates":[33.4827, 13.0438],"distance": "100 m"}}
                    ]
                }
            }
        }
    },
    "aggs":{
        "found":{
            "terms":{
                "field": "name"
            }
        }
    }
}

“必须”和“应该”我都试过了。 Must 将仅返回在两个区域中具有 lat/lng 坐标的文档,而 Should 返回两个区域中的所有文档。我需要查询返回在两个区域中具有相同“名称”的文档,而不是坐标。

【问题讨论】:

  • 请加入您的查询示例。似乎您正在制作 AND(布尔)而不是 OR。
  • 很抱歉。我添加了我正在使用的查询。
  • 请尝试更改坐标格式 "coordinates": { "lat": 32.4809, "lon": 12.0422 },
  • 我在我这边试过了,类似的数据完全没有问题。也许使用名称过滤器发布您的完整查询。
  • 这就是我卡住的地方。我可以从每个位置/区域取回文档,但我不知道如何过滤它以仅返回在两个位置/区域中具有相同名称/值的文档

标签: elasticsearch filter geolocation


【解决方案1】:

好的,所以我编辑了。我们应该按名称 agg,然后使用请求的 aera 之一按区域工作。在我们使用 bucket_selector 发布过滤器聚合后,_bucket_count 获取 subaggeo 聚合的数组大小,因此如果他的大小 == 2,则 name 在两个 aera 上。请告诉我它是否有效;)

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "geo_distance": {
                "coordinates": {
                  "lat": 48.858837,
                  "lon": 2.27702
                },
                "distance": "10km"
              }
            },
            {
              "geo_distance": {
                "coordinates": {
                  "lat": 40.697149,
                  "lon": -74.2598
                },
                "distance": "10km"
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "found": {
      "terms": {
        "field": "name",
        "min_doc_count": 2
      },
      "aggs": {
        "subagggeo": {
          "geo_distance": {
            "field": "coordinates",
            "origin": "48.858837, 2.27702",
            "unit": "m",
            "ranges": [
              {
                "to": 10000
              },
              {
                "from": 10001
              }
            ]
          }
        },
        "min_bucket_selector": {
      "bucket_selector": {
        "buckets_path": {
          "hits": "subagggeo._bucket_count"
        },
        "script": {
          "inline": "params.hits > 1"
        }
      }
    }
      }
    }
  }
}

【讨论】:

  • 此查询正在搜索特定名称或值。我想要做的是获取这两个区域中存在的所有值。
  • 好的抱歉,误会了,周一试试。我认为你必须使用子聚合。
  • 谢谢!非常感谢您的帮助!
  • 我认为它有效。你能解释一下子聚合部分发生了什么吗?
  • Aggs 就像一个 groupby,第一个(找到的)group by name。你有一个数组(桶)“Key = name”,“doc_count” = 发生。您可以在内部添加子聚合以获取每个聚合的其他信息。在我的示例中,对于每个名称,我想知道其中有多少是 。因此,我使用 Buckets 为每个名称子聚合(一个 geo_distance 一个),根据它们的距离拆分文档。最后一部分,bucker 选择器一个是“聚合结果的后过滤器”,这里我表示要处理 geo_distance aggr
猜你喜欢
  • 1970-01-01
  • 2019-12-21
  • 2014-07-02
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多