我是Elasticsearch的新手,我试图创建一个过滤器来检索具有特定属性的文档。Elasticsearch嵌套对象过滤器
的属性定义为嵌套对象的映射,像这样:
"attributes": {
"type": "nested",
"properties" : {
"id": {
"type": "integer"
},
...
}
}
}
我想要的形式执行复杂查询:
(attribute.id == 1 OR attribute.id == 2) AND (attribute.id == 3 OR attribute.id == 4)
据我所阅读到目前为止我已创建以下查询以es:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "attributes",
"filter": {
"bool": {
"must": [
{ "bool" : {
"should" : [
{ "term": { "attributes.id": 1 }},
{ "term": { "attributes.id": 2 }}
]
}},
{ "bool" : {
"should" : [
{ "term": { "attributes.id": 3 }},
{ "term": { "attributes.id": 4 }}
]
}}
]
}
}
}
}
}
},
"sort": {
"date": { "order": "desc" }
}
}
但是,这不会返回任何重新sults。如果我删除must
块中的两个bools
中的一个,它会正确过滤文档。
存在同样的问题(没有结果),如果我更改查询(用于测试目的)到:
"must": [
{ "term": { "attributes.id": 3 }},
{ "bool" : {
"should" : [
{ "term": { "attributes.id": 1 }},
{ "term": { "attributes.id": 2 }}
]
}}
]
这在我的理解转化为attributes.id == 3 AND (attributes.id == 1 OR attributes.id == 2)
这是elasticsearch 2.x版本我做错了什么?
也许每个'bool'子句可以在它自己的'nested'子句中?而不是将它们全部包装在一起。 –
@EvaldasBuinauskas我会尽力回复你。 – mobius
你可以发布一个你期望被这个查询返回的示例文档吗? – goalie7960