我正在使用Elasticsearch V6.7.1.
我创建了几个索引并用数据填充它们。每个索引都有一个字段gps_coords
与lat and
lon`值(坐标)
我想要做的是写一个查询,我传递一个多边形并检查某个点是否属于该多边形,这可能吗?
这是我已经尝试过的查询:
{
"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
]
]
];
有人可以指出我正确的方向。 谢谢 !
首先,在查询示例中,您使用location
字段而不是gps_coords
,如评论中所述。但我认为这只是一个错字,因为这不是错误的来源。
您收到解析异常的原因是您在geo_shape
查询中缺少多边形定义中的一对括号。看到正确的形式here。正确的形式是(只是相关部分):
"shape": {
"type": "polygon",
"coordinates" : [
[[25.0245351, 54.5693374],
[25.0245351, 54.83232],
[25.4815808, 54.83232],
[25.4815808, 54.5693374],
[25.0245351, 54.5693374]]
]
}
是的,对不起,我的不好,只是从ES文档复制了示例而不是我的代码。 Okey,会尝试添加这些括号,看看它是否有帮助,谢谢!