首页 > 解决方案 > Elasticsearch 嵌套地理形状查询

问题描述

假设我有以下映射:

  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "location": {
          "type": "nested",
          "properties": {
            "point": {
              "type": "geo_shape"
            }
          }
        }
      }
    }
  }
}

索引中有一个文档:

POST /example/doc?refresh
{
    "name": "Wind & Wetter, Berlin, Germany",
    "location": {            
        "type": "point",
        "coordinates": [13.400544, 52.530286]
    }
}

如何进行嵌套地理形状查询?文档中常见的地理形状查询示例(可以跳过“bool”块):

 {
        "query":{
            "bool": {
                "must": {
                    "match_all": {}
                },
                "filter": {
                    "geo_shape": {
                        "location": {
                            "shape": {
                                "type": "envelope",
                                "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
                            },
                            "relation": "within"
                        }
                    }
                }
            }
        }
    }

嵌套查询的示例是:

{
    "query": {
        "nested" : {
            "path" : "obj1",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"obj1.name" : "blue"} },
                    { "range" : {"obj1.count" : {"gt" : 5}} }
                    ]
                }
            }
        }
    }
}

现在如何组合它们?在文档中提到嵌套过滤器已被嵌套查询替换。它表现为“查询上下文”中的查询和“过滤上下文”中的过滤器。

如果我尝试查询与该点相交:

    {
  "query": {
    "nested": {
      "path": "location",
      "query": {
        "geo_shape": {
          "location.point": {
            "shape": {
              "type": "point",
              "coordinates": [
                13.400544,
                52.530286
              ]
            },
            "relation": "disjoint"
          }
        }
      }
    }
  }
}

即使关系“不相交”,我仍然会取回文档,所以它不正确。我尝试了不同的组合,使用“bool”和“filter”等,但查询被忽略,返回整个索引。也许这种类型的映射是不可能的?

显然我在这里遗漏了一些东西。有人可以帮我解决这个问题吗?任何帮助是极大的赞赏。

标签: elasticsearchelasticsearch-geo-shapeelasticsearch-nested

解决方案


推荐阅读