在嵌套位置上进行过滤时,让我的geo_shape过滤器返回结果时遇到问题。
假设我有以下内容:
PUT test/test/_mapping
{
"properties": {
"name": {
"type": "string"
},
"gatheringEvent": {
"properties": {
"siteCoordinates": {
"type": "nested",
"properties": {
"point": {
"type": "geo_shape"
}
}
}
}
},
"point": {
"type": "geo_shape"
}
}
}
现在,当我索引以下文档时:
POST test/test/1
{
"name": "Bird",
"gatheringEvent.siteCoordinates.point": {
"type": "point",
"coordinates": [
5,
5
]
},
"point": {
"type": "point",
"coordinates": [
5,
5
]
}
}
执行以下查询:(在非嵌套位置使用geo_shape过滤器)
GET test/test/_search
{
"query": {
"filtered": {
"query": {
"match": {
"name": "Bird"
}
},
"filter": {
"geo_shape": {
"point": {
"shape": {
"type": "polygon",
"coordinates": [
[
[0 ,0 ],
[10 ,0],
[10,10],
[0,10 ],
[0 ,0 ]
]
]
},
"relation": "within"
}
}
}
}
}
}
如我所愿,把文件还给我。
但是在嵌套位置执行geo_shape过滤器时:
GET test/test/_search
{
"query": {
"filtered": {
"query": {
"match": {
"name": "Bird"
}
},
"filter": {
"nested": {
"path": "gatheringEvent.siteCoordinates",
"filter": {
"geo_shape": {
"gatheringEvent.siteCoordinates.point": {
"shape": {
"type": "polygon",
"coordinates": [
[
[0 ,0 ],
[10 ,0],
[10,10],
[0,10 ],
[0 ,0 ]
]
]
},
"relation": "within"
}
}
}
}
}
}
}
}
没有结果。
我也删除了嵌套映射,因为我认为这可能是问题所在,但是一旦“point”字段位于对象类型字段中,我就没有结果。
对我在这里做错的任何想法吗?
谢谢。
最佳答案
我在这里看到几个问题:
nested
过滤器,则需要在映射中指定两个级别。 "gatheringEvent.siteCoordinates.point"
的顶级属性,这大概不是您想要的。 您可以通过几种不同的方式进行操作。这就是我能够使其工作的方式。首先,我修改了映射以包括两个嵌套层,并创建了如下索引:
DELETE /test_index
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
PUT /test_index/doc/_mapping
{
"properties": {
"name": {
"type": "string"
},
"gatheringEvent": {
"type": "nested",
"properties": {
"siteCoordinates": {
"type": "nested",
"properties": {
"point": {
"type": "geo_shape"
}
}
}
}
},
"point": {
"type": "geo_shape"
}
}
}
然后,我为两个层次的嵌套使用正确的结构索引了您的文档:
POST /test_index/doc/1
{
"name": "Bird",
"gatheringEvent": [
{
"siteCoordinates": [
{
"point": {
"type": "point",
"coordinates": [5, 5]
}
}
]
}
],
"point": {
"type": "point",
"coordinates": [5, 5]
}
}
我还在边界框外添加了第二个文档,以进行完整性检查:
POST /test_index/doc/2
{
"name": "Bird",
"gatheringEvent": [
{
"siteCoordinates": [
{
"point": {
"type": "point",
"coordinates": [6, 11]
}
}
]
}
],
"point": {
"type": "point",
"coordinates": [6, 11]
}
}
现在,您的两个查询都能按预期工作:
POST /test_index/doc/_search
{
"query": {
"filtered": {
"query": {
"match": {
"name": "Bird"
}
},
"filter": {
"nested": {
"path": "gatheringEvent.siteCoordinates",
"filter": {
"geo_shape": {
"gatheringEvent.siteCoordinates.point": {
"shape": {
"type": "polygon",
"coordinates": [
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0]
]
]
},
"relation": "within"
}
}
}
}
}
}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.6931472,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1.6931472,
"_source": {
"name": "Bird",
"gatheringEvent": [
{
"siteCoordinates": [
{
"point": {
"type": "point",
"coordinates": [
5,
5
]
}
}
]
}
],
"point": {
"type": "point",
"coordinates": [
5,
5
]
}
}
}
]
}
}
如果您实际上只想要一个嵌套级别,那就更简单了。我也可以根据需要添加该代码。
这是我使用的代码:
http://sense.qbox.io/gist/e61259626d5f8525ee41ce7b049af25089bfb8f6
关于elasticsearch - Elasticsearch geo_shape过滤器无结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28498494/