ElasticSearch bool should_not filter

booleanelasticsearchrange

I'm a newbie in elasticsearch, so my question is:

There are 3 sections in bool filter:

must
    All of these clauses must match. The equivalent of AND. 
must_not
    All of these clauses must not match. The equivalent of NOT. 
should
    At least one of these clauses must match. The equivalent of OR. 

How do I perform a "should_not" query?

Thanks in advance 🙂

Best Answer

In order to get something like "should_not" please try the following query:

GET /bank/account/_search
{
   "size": 2000,
   "query": {
      "bool": {
          "must": {
             "match_all": {}
          }, 
         "should": {
            "bool": {
               "must_not": {
                  "match": {
                     "city": "XYZ"
                  }
               }
            }
         }
      }
   }
}

In this case the clause "must" is required to retrieve the all results (the result with the city "XYZ" too) but with decreased score for this specific one.

Related Topic