Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 215
kcm Avatar asked Jul 28 '16 15:07

kcm


People also ask

How do I merge two queries in Elasticsearch?

You can combine the queries using bool query. Based on your requirement you can use 'should' or 'must' inside the bool clauses. You may want to schearch for both the field you want, and then aggregate by the most important field.

Should minimum should match?

Minimum Should Match is another search technique that allows you to conduct a more controlled search on related or co-occurring topics by specifying the number of search terms or phrases in the query that should occur within the records returned.

What is the difference between must and should in Elasticsearch?

It is the opposite of the must clause. Using must_not tells Elasticsearch that document matches cannot include any of the queries that fall under the must_not clause. should – It would be ideal for the matching documents to include all of the queries in the should clause, but they do not have to be included.

What is bool query in Elasticsearch?

The bool query is a go-to query because it allows you to construct an advanced query by chaining together several simple ones. The results must match the queries in this clause. If you have multiple queries, every single one must match. Acts as an and operator.


3 Answers

If you want simple AND-behavior, then just nest another bool query inside of it:

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

Note that by using the filter (as it's a yes/no question with no scoring needed, but if you wanted scoring, then you would use must instead of filter) you get the desired AND behavior. This changes the question to "not(any document that has criteria1 == false AND criteria2 == false)".

like image 188
pickypg Avatar answered Oct 15 '22 10:10

pickypg


The following arrangement worked for me on a similar query in 5.5.1:

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

The other answers may have been correct for other versions, but did not work for me in 5.5.1.

like image 26
Mnebuerquo Avatar answered Oct 15 '22 12:10

Mnebuerquo


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
                               }
                            }
                        ]
                    }
                }
            }
        ]
    }
}
like image 4
kcm Avatar answered Oct 15 '22 12:10

kcm