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 :)
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With