Search code examples
elasticsearch

Combining must_not in ElasticSearch Query


I'm currently struggling with an ElastSearch query which currently looks the following:

...
"query": {
    "bool": {
        "must_not": [
            {
                "term": {
                    "bool-facet.criteria1": {
                        "value": false
                    }
                }
            },
            {
                "term": {
                    "bool-facet.criteria2": {
                        "value": false
                    }
                }
            }
        ]
    }
}
...

So now when either criteria1 OR criteria2 matches, the documents are ignored. How must the query look like so that only documents that match criteria1 AND criteria2 are ignored?


Solution

  • Since updating elasticsearch version was not possible I had to find another solution. This is what worked for me:

    "query": {
        "bool": {
            "must_not" : [
                {
                    "query": {
                        "bool": {
                            "must": [
                                {
                                   "term": {
                                     "bool-facet.criteria1": false
                                   }
                                },
                                {
                                   "term": {
                                     "bool-facet.criteria2": false
                                   }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }