Continue to section 13
6), bool [Compound query]
bool is used to do compound queries:
It is important to understand that compound statements can be combined with any other query statements, including compound statements.
This means that compound statements can be nested within each other and can express very complex logic.
- must: Must meet all the conditions listed in must
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "F"
}
},
{
"match": {
"address": "Mill"
}
}
]
}
}
}
- must_not: clause (query) must not appear in matching documents
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "F"
}
},
{
"match": {
"address": "Mill"
}
}
],
"must_not": [
{
"match": {
"age": 30
}}
]
}
}
}
- should: The clause (query) should appear in the matching document. (Should means it’s best to have it, it’s okay to have none)
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "Mill"
}
}
],
"must_not": [
{
"match": {
"age": 30
}}
],
"should": [
{
"match": {
"lastname": "Holland"
}}
]
}
}
}
Boolean query
Queries that match documents that match Boolean combinations of other queries. Boolean queries are mapped to Lucene BooleanQuery
. It is constructed using one or more Boolean clauses, and each clause has an occurrence of a type. The type of occurrence is:
occur | description |
---|---|
must |
The clause (query) must appear in the matching document and will contribute to the score. |
filter |
The clause (query) must appear in the matching document. But unlike must scores of the query will be ignored. The Filter clause is executed in the filter context , which means that scoring is ignored and the clause is considered for caching. |
should |
The clause (query) should appear in the matching document. |
must_not |
The clause (query) must not appear in matching documents. The clause is executed in the filter context , which means that the scoring is ignored and the clause is considered to be used for caching. Since scoring is ignored, 0 the scores for all documents will be returned. |
The next section we look at filter
the results of the filter.
reference:
Getting started with the full-text search engine Elasticsearch