Continue from section 14
7), filter [Result filtering]
Not all queries need to generate scores, especially those documents that are only used for "fitering" (filtering). In order not to calculate the score, Elasticsearch will automatically check the scene and optimize the execution of the query.
The query specified under the filter element has no effect on the score-the score is returned as 0. The score is only affected by the specified query.
To must
query as an example:
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gte": 18,
"lte": 30
}
}}
]
}
}
}
Use filter
instead of must
queries should be noted that the use of filter
check out the results and must
check out the result is the same, only difference is that there is no correlation score:
GET /bank/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
}
}
}
So we should
can add after filter
adding a filter:
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "Mill"
}
}
],
"must_not": [
{
"match": {
"age": 30
}}
],
"should": [
{
"match": {
"lastname": "Holland"
}}
],
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
}
}
}
reference:
Getting started with the full-text search engine Elasticsearch