当在嵌套位置进行筛选时,我在获取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"
}
}
}
}
}
}
}
}
没有结果。。
我还删除了嵌套映射,因为我认为这可能是问题所在,但一旦"点"字段位于对象类型字段中,我就不会得到任何结果。。
你觉得我在这里做错了什么吗??
谢谢。
我在这里看到了几个问题:
- 看起来您想要两个级别的嵌套(除非这是错误的),所以如果您希望能够在查询中使用
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