elasticsearch OpenSearch:如何在给定的边界框内找到多边形?

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

我尝试过:

GET thing/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "location": {
            "top_left": {
              "lat": 47.7328,
              "lon": -122.448
            },
            "bottom_right": {
              "lat": 47.468,
              "lon": -122.0924
            }
          }
        }
      }
    }
  }
}

...但得到以下错误:

"failed_shards" : [
  {
    "shard" : 0,
    "index" : "geofence",
    "node" : "rLGHWGhqRTqDxDlhjA3hhA",
    "reason" : {
      "type" : "query_shard_exception",
      "reason" : "failed to find geo_point field [location]",
      "index" : "geofence",
      "index_uuid" : "sBbXbANQROeQ15IzmBRf3g"
    }
  }
]

......因此,我补充说:

PUT /thing/_mapping
{
  "mappings": {
    "properties": {
      "point": {
        "type": "geo_point"
      }
    }
  }
}

...返回以下错误:

"type" : "mapper_parsing_exception",
"reason" : "Root mapping definition has unsupported parameters:  [mappings : {properties={point={type=geo_point}}}]"

此查询适用于:

GET geofence/_search
{
  "query": {
    "geo_shape": {
      "geometry": {
        "shape": {
          "type": "envelope",
          "coordinates": [ [ -90.0, 90.0], [ 90.0, -90.0] ]
        },
        "relation": "WITHIN"
      }
    }
  }
}

...但这不是边界框,对于-90.0以外的任何值(如-122.0)都将失败
给定以下几何形状:

"geometry" : {
        "type" : "Polygon",
        "coordinates" : [
          [
            [
              -122.661806,
              45.596338
            ],
            [
              -122.661324,
              45.597164
            ],
            [
              -122.661865,
              45.597326
            ],
            [
              -122.662461,
              45.597532
            ],
            [
              -122.662868,
              45.597652
            ],
            [
              -122.663405,
              45.597825
            ],
            [
              -122.664381,
              45.598088
            ],
            [
              -122.66421,
              45.598846
            ],
            [
              -122.663995,
              45.599551
            ],
            [
              -122.663974,
              45.599672
            ],
            [
              -122.664135,
              45.599844
            ],
            [
              -122.663898,
              45.600662
            ],
            [
              -122.664574,
              45.60088
            ],
            [
              -122.665272,
              45.601008
            ],
            [
              -122.665808,
              45.601128
            ],
            [
              -122.666162,
              45.599799
            ],
            [
              -122.665304,
              45.599582
            ],
            [
              -122.665411,
              45.599046
            ],
            [
              -122.66599,
              45.597269
            ],
            [
              -122.66378,
              45.596884
            ],
            [
              -122.661806,
              45.596338
            ]
          ]
        ]
      },

查询边界框内多边形列表的正确方法是什么?

n6lpvg4x

n6lpvg4x1#

错误状态
找不到geo_point字段[位置]
因此,您需要在Map中添加geo_point类型的location字段,如下所示:

PUT /thing/_mapping
{
    "properties": {
      "location": {
        "type": "geo_point"
      }
  }
}

然后你的第一个查询就可以工作了,只要你确保把数据索引到新的location字段中,但是你的第一个查询只会在给定的边界框内找到“点”。
如果要查找边界框内的面,则需要在geo_shape字段上使用geo_shape query

jxct1oxe

jxct1oxe2#

GET thing/_search
  {
    "size": 1000,
    "query": {
      "bool": {
        "filter": [
          {
            "terms":{
              ...
            }
          }, {
            "geo_shape": {
              "geometry": {
                "shape": {
                  "type": "Polygon",
                  "coordinates" : [[
                    [-180, 75],
                    [-50, 75],
                    [-50, 0],
                    [-180, 0],
                    [-180, 75]
                  ]]
                },
              "relation": "within"
              }
            }
          }
        ]
      }
    }
  }

这给出了一个实际的基于坐标的查找与bbox(或任何信封实际上是)...
上面的坐标在北美洲周围画了一个方框。

相关问题