Home > OS >  How elasticsearch decides must and filter terms in same query?
How elasticsearch decides must and filter terms in same query?

Time:08-13

The following query includes must and filter contexts in same query.

GET /_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }},
        { "match": { "content": "Elasticsearch" }}
      ],
      "filter": [ 
        { "term":  { "status": "published" }}
      ]
    }
  }
}

if there are some documents that matches with must logic but not matches with filer logic, so I am getting no result? Is filter most important? How works this query order? First must logic and then filter logic?

CodePudding user response:

Here it will match all the conditions of MUST & FILTERS. You can see MUST & FILTER something like AND query. So the query will go like title="search" AND content="Elasticsearch" AND status="published".

The only difference is MUST contributes to scoring where FILTER is not.

So if you hit same query like below,

GET /_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }},
        { "match": { "content": "Elasticsearch" }},
        { "term":  { "status": "published" }}
      ]
    }
  }
}

You will see the different _score value with same result.

So simple is if you want to ignore scoring just use filter.

Mostly filters are use with conditions like <,>,<=,>=,= or where you don't want score.

Note : Frequently used filters will be cached automatically by Elasticsearch, to speed up performance.

Check more in doc

  • Related