Reputation: 5806
I'm using Elastic Search, with query match_all and filtering. In my situation I want to apply a general filter and filters by condition.
Here in pseudo:
See the following code. I want only apply the "groups" filter if the "groups" field exists! The "exists" filter doesn't take effect in that case.
"query":
{
"filtered":
{
"query":
{
"match_all": {}
},
"filter":
{
"bool":
{
"must":
{
"range":
{
"date": {
"from": "2015-06-01",
"to": "2015-06-30"
}
}
},
"must_not":
{
"term":
{
"e.state": 0
}
}
}
},
"filter":
{
"bool":
{
"must":
{
"exists": {"field": "groups"},
"filter":
{
"bool":
{
"must":
{
"term": {"groups.sex": "w"}
},
"should":
{
"terms": {"groups.categories.id": [7,10]}
}
}
}
}
}
}
}
}
}
Upvotes: 3
Views: 790
Reputation: 52368
Try this
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"range": {
"date": {
"from": "2015-06-01",
"to": "2015-06-30"
}
}
},
{
"bool": {
"should": [
{
"missing": {
"field": "groups"
}
},
{
"bool": {
"must": {
"term": {
"groups.sex": "w"
}
},
"should": {
"terms": {"groups.categories.id": [7,10]}
}
}
}
]
}
}
],
"must_not": {
"term": {
"e.state": 0
}
}
}
}
}
}
}
Upvotes: 2