Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch bool should_not filter

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 :)

like image 374
nogo0d Avatar asked Jan 28 '16 12:01

nogo0d


People also ask

What is bool 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.

Should and must not Elasticsearch?

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. Scoring is used to rank the matches.

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 Boolean query?

The term “Boolean” refers to a system of logic developed by the mathematician and early computer pioneer, George Boole. Boolean searching includes three key Boolean operators: AND, OR, and NOT. • An AND operator narrows your search. Between two keywords it results in a search for posts containing both of the words.


2 Answers

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.

like image 96
Przemek Nowak Avatar answered Sep 21 '22 15:09

Przemek Nowak


That depends on how exactly you are wishing a "should_not" to function.

Since should is roughly equivalent to a boolean OR (i.e. return documents where A or B or C is true), then one way to think of "should_not" would be roughly equivalent to NOT (A OR B OR C). In boolean logic, this is the same as NOT A AND NOT B AND NOT C. In this case, "should_not" behavior would be accomplished by simply adding all your clauses to the must_not section of the bool filter.

like image 39
BrookeB Avatar answered Sep 17 '22 15:09

BrookeB