I am new to Elasticsearch and I am trying to create a filter to retrieve documents that have specific attributes.
The attributes are defined as nested objects in the mapping like so:
"attributes": {
"type": "nested",
"properties" : {
"id": {
"type": "integer"
},
...
}
}
}
I am trying to perform a complex query in the form of:
(attribute.id == 1 OR attribute.id == 2) AND (attribute.id == 3 OR attribute.id == 4)
According to what I've read so far I've created the following query to 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" }
}
}
However this doesn't return any results. If I remove one of the two bools
in the must
block it filters the documents correctly.
Same problem exists (no results) if I change the query (for testing purposes) to:
"must": [
{ "term": { "attributes.id": 3 }},
{ "bool" : {
"should" : [
{ "term": { "attributes.id": 1 }},
{ "term": { "attributes.id": 2 }}
]
}}
]
which to my understanding translates to attributes.id == 3 AND (attributes.id == 1 OR attributes.id == 2)
This is elasticsearch 2.x. What I am doing wrong?
bool
clause could be in its ownnested
clause? Instead of having it all wrapped up in one. – Evaldas Buinauskas