Elasticsearch查询geo_polygon是否在多多边形内

shstlldc  于 2023-03-29  发布在  ElasticSearch
关注(0)|答案(2)|浏览(311)

我正在检查一个地理点是否位于一个多边形内使用弹性。我能够让它工作简单的“多边形”,但“多多边形”不工作。

有效(多边形):

{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "geo" : {
                        "points" : [
                            [-131.602021, 55.117982],
                            [-131.569159, 55.28229],
                            [-131.355558, 55.183705],
                            [-131.38842, 55.01392],
                            [-131.645836, 55.035827],
                            [-131.602021, 55.117982]
                        ]
                    }
                }
            }
        }
    }
}

不支持(多面):

{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "geo" : {
                        "points" : [
                            [
                                    [-131.602021, 55.117982],
                                    [-131.569159, 55.28229],
                                    [-131.355558, 55.183705],
                                    [-131.38842, 55.01392],
                                    [-131.645836, 55.035827],
                                    [-131.602021, 55.117982]
                            ],
                            [
                                    [-131.832052, 55.42469],
                                    [-131.645836, 55.304197],
                                    [-131.749898, 55.128935],
                                    [-131.832052, 55.189182],
                                    [-131.832052, 55.42469]
                            ]
                         ]
                    }
                }
            }
        }
    }
}

我的理解是,我可能需要对每个单独的多边形进行某种布尔查询-但是,任何指导都会很好。

idfiyjo8

idfiyjo81#

@Nate是对的--geo_polygon查询中不支持多多边形,但有一个recently-active PR可以在geo_point类型上启用geo_shape查询--这将非常适合您的用例。
在此期间,你将不得不求助于分割你的multipolygon并使用bool-should:

{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "should": [
            {
              "geo_polygon": {
                "geo": {
                  "points": [
                          [-131.602021, 55.117982],
                          [-131.569159, 55.28229],
                          [-131.355558, 55.183705],
                          [-131.38842, 55.01392],
                          [-131.645836, 55.035827],
                          [-131.602021, 55.117982]
                   ]
                }
              }
            },
            {
              "geo_polygon": {
                "geo": {
                  "points": [
                          [-131.832052, 55.42469],
                          [-131.645836, 55.304197],
                          [-131.749898, 55.128935],
                          [-131.832052, 55.189182],
                          [-131.832052, 55.42469]
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }
}
v6ylcynt

v6ylcynt2#

上面的答案对我不起作用,我试了下面的方法,效果很好:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "geo_polygon": {
                        "_name": "location.geo_coordinates",
                        "location.geo_coordinates": {
                          "points": []
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "geo_polygon": {
                        "_name": "location.geo_coordinates",
                        "location.geo_coordinates": {
                          "points": []
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
    }
  }
}

minimum_should_match就可以做到这一点。为了不影响其他的should查询,我把它 Package 成单独的bool。

相关问题