Https://es.xiaoleilu.com/080_Structured_Search/10_compoundfilters.html
Here's a complete document.
Combination Filter
The previous two examples show the use of a single filter. In reality, you might want to filter multiple values or fields, for example, would you like to express this SQL in Elasticsearch?
SELECT productFROM productsWHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3") AND (price != 30)
In these cases, you need a bool filter. This is a combination filter with other filters as parameters, combining them into a variety of Boolean combinations.
Boolean filter
boolThe filter consists of three parts:
{ "bool" : { "must" : [], "should" : [], "must_not" : [], }}
must: All clauses must match, with the AND same.
must_not: All clauses must not match, with the NOT same.
should: At least one clause matches, with the OR same.
It's all right! If you need more than one filter, put them in the bool filter on the line.
Tip: bool Each part of the filter is optional (for example, you can keep only one must clause), and each section can contain one or more filters
To replicate the SQL example above, we term put two filters bool under the clause of the filter should and then use another clause to deal with the NOT condition:
GET /my_store/products/_search{ "query" : { "filtered" : { <1> "filter" : { "bool" : { "should" : [ { "term" : {"price" : 20}}, <2> { "term" : {"productID" : "XHDK-A-1293-#fJ3"}} <2> ], "must_not" : { "term" : {"price" : 30} <3> } } } } }}
<1> Note we still need to use the filtered query to wrap all the conditions.
<2> These two term filters are sub- bool nodes of the filter, because they are placed under the should clause, so at least they have to have a condition to match.
<3> if a product value 30 , it will be automatically eliminated, because it matches the must_not clause.
Our search results returned two results, each satisfying the bool different clauses in the filter:
"hits": [{ "_id": "1", "_score": 1.0, " _source ": {" price ": 10, " xhdk-a-1293-#fJ3 "<1>}}, {" _id ": " 2 ", " _score ": "_source": { "price": 20, <2> "ProductID": "Kdke-b-9947-#kL5"}}]
<1> Matching term FiltersproductID = "XHDK-A-1293-#fJ3"
<2> Matching term Filtersprice = 20
Nested Boolean filters
Although bool it is a combination filter and accepts sub-filters, it is necessary to understand that it is still just a filter. This means that you can nest filters in a bool filter bool , allowing you to implement more complex Boolean logic.
The following is the first SQL statement:
SELECT documentFROM productsWHERE productID = "KDKE-B-9947-#kL5" OR ( productID = "JODL-X-1937-#pV7" AND price = 30 )
We can translate it into a pair of nested bool filters:
GET /my_store/products/_search{ "query" : { "filtered" : { "filter" : { "bool" : { "should" : [ { "term" : {"productID" : "KDKE-B-9947-#kL5"}}, <1> { "bool" : { <1> "must" : [ { "term" : {"productID" : "JODL-X-1937-#pV7"}}, <2> { "term" : {"price" : 30}} <2> ] }} ] } } } }}
<1> because term bool It is a peer in the first should clause, at least one of the filters needs to be matched.
mustthere are two lateral clauses in the <2> clause term , so they both need a match.
The result is two documents that match one clause, respectively should :
"hits": [{ "_id": "2", "_score": 1.0, "_source": { "price": 20, "ProductID": " kdke-b-9947-#kL5 "<1>}}, {" _id ": "3", "_score": 1.0, "_source": { "price": 30, <2> "ProductID": "Jodl-x-1937-#pV7" <2>}]
<1> productID matches the bool filter in the first one term .
<2> these two fields match the bool filters in the nested term .
This is a simple example, but it shows how to use Boolean filters to construct complex logical conditions.
[Turn] combination filter