首页 > 解决方案 > 多边形查询中的 Elasticsearch 点

问题描述

我在用着Elasticsearch V6.7.1.

我创建了几个索引并用数据填充它们。每个索引都有一个 gps_coords带有lat andlon 值(坐标)的字段

我想要做的是编写一个查询,我在其中传递一个多边形并检查某个点是否落入该多边形,这可能吗?

这是我已经尝试过的查询:

{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
             "filter": {
                "geo_shape": {
                    "location": {
                        "shape": {
                            "type": "polygon",
                            "coordinates" : [
                                [25.0245351, 54.5693374], 
                                [25.0245351, 54.83232],
                                [25.4815808, 54.83232],
                                [25.4815808, 54.5693374],
                                [25.0245351, 54.5693374]
                              ]
                        },
                        "relation": "within"
                    }
                }
            }
        }
    }
}

但它返回此错误:

{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "Invalid LinearRing found. Found a single coordinate when expecting a coordinate array"
      }
    ],
    "type": "parse_exception",
    "reason": "Invalid LinearRing found. Found a single coordinate when expecting a coordinate array"
  },
  "status": 400
}

这是我的索引映射:

[
            'index' => 'places',
            'body' => [
                'mappings' => [
                    'place' => [
                        "properties" => [
                            "gps_coords" => [
                                "ignore_malformed" => true,
                                "type" => "geo_shape"
                            ]
                        ]
                    ],

                ],
                "number_of_replicas" => 0
            ]
        ]
    ];

有人可以指出我正确的方向。
谢谢 !

标签: elasticsearchgeospatialspatialelastic-stackspatial-query

解决方案


首先,在查询示例中,您使用locationfield 而不是gps_coords,如评论中所述。但我相信这只是一个错字,因为这不是错误的根源。

geo_shape您收到解析异常的原因是您在查询的多边形定义中缺少一对括号。在这里查看正确的表格。正确的形式是(只是相关部分):

"shape": {
    "type": "polygon",
    "coordinates" : [
        [[25.0245351, 54.5693374], 
         [25.0245351, 54.83232],
         [25.4815808, 54.83232],
         [25.4815808, 54.5693374],
         [25.0245351, 54.5693374]]
    ]
}

推荐阅读