我试图在文档上应用嵌套过滤器,但是,当我通过其中一个嵌套属性进行特定过滤时,我总是得到0个结果。例如,使用如下映射:
"PARTNER": {
"properties": {
"Addresses": {
"properties": {
"CountyCode": {
"type": "string"
},
"EntityCode": {
"type": "string"
}, [...]
}
}, "type": "nested"
}, [...]
}
我能够对CountyCode属性执行过滤,但如果我在EntityCode上过滤,我总是得到零结果:
{
"from": 0,
"size": 10,
"query": {
"filtered": {
"query": {
"bool": {
"must": {
"match_all": {}
}
}
},
"filter": {
"nested": {
"filter": {
"term": {
"EntityCode": "201"
}
},
"path": "Addresses"
}
}
}
}
}
该查询得到零结果,如果我用CountyCode替换该字段,它会给我正确的结果。 我在ES 1.4.2上的基于Web的控制台上执行查询。
嵌套文档的格式为:
{
"CountyCode": "516",
"EntityCode": "203",
[...]
}
为什么一个字段什么都没有返回?
谢谢!
答案 0 :(得分:0)
您的"type": "nested"
声明似乎位于错误的位置。它应该在"Addresses"
块内。您可能还需要在"nested"
过滤器中指定完整路径(尽管如果没有太多嵌套字段,ES有足够的智能可以在不执行此操作的情况下工作)。
所以为了测试,我创建了一个这样的索引:
PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"PARTNER": {
"properties": {
"Addresses": {
"type": "nested",
"properties": {
"CountyCode": {
"type": "string"
},
"EntityCode": {
"type": "string"
}
}
}
}
}
}
}
然后添加了几个文档:
POST /test_index/PARTNER/_bulk
{"index":{"_id":1}}
{"Addresses":[{"CountyCode": "516","EntityCode": "203"}, {"CountyCode": "516","EntityCode": "201"}]}
{"index":{"_id":2}}
{"Addresses":[{"CountyCode": "517","EntityCode": "204"}]}
现在,以下查询返回我期望的内容:
POST /test_index/_search
{
"from": 0,
"size": 10,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "Addresses",
"filter": {
"term": {
"Addresses.EntityCode": "201"
}
}
}
}
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "PARTNER",
"_id": "1",
"_score": 1,
"_source": {
"Addresses": [
{
"CountyCode": "516",
"EntityCode": "203"
},
{
"CountyCode": "516",
"EntityCode": "201"
}
]
}
}
]
}
}
以下是我用来测试的所有代码:
http://sense.qbox.io/gist/d677144a58b168d46c5d7e9cc516013b20613013