Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between must and filter in Query DSL in elasticsearch?

I am new to elastic search and I am confused between must and filter. I want to perform an and operation between my terms, so I did this

POST /xyz/_search

{     "query": {         "bool": {             "must": [                 {                     "term": {                         "city": "city1"                     }                 },                 {                     "term": {                         "saleType": "sale_type1"                     }                 }             ]         }     } } 

which gave me the required results matching both the terms, and on using filter like this

POST /xyz/_search

{     "query": {         "bool": {             "must": [                 {                     "term": {                         "city": "city1"                     }                 }             ],             "filter": {                 "term": {                     "saleType": "sale_type1"                 }             }         }     } } 

I get the same result, so when should I use must and when should I use filter? What is the difference?

like image 451
Krash Avatar asked Apr 11 '17 14:04

Krash


People also ask

Should and must query Elasticsearch?

must means: Clauses that must match for the document to be included. should means: If these clauses match, they increase the _score ; otherwise, they have no effect. They are simply used to refine the relevance score for each document. Yes you can use multiple filters inside must .

What is query DSL in Elasticsearch?

Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses: Leaf query clauses. Leaf query clauses look for a particular value in a particular field, such as the match , term or range queries. These queries can be used by themselves.

What are filters in Elasticsearch?

Frequently used filters will be cached automatically by Elasticsearch, to speed up performance. Filter context is in effect whenever a query clause is passed to a filter parameter, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation.


1 Answers

must contributes to the score. In filter, the score of the query is ignored.

In both must and filter, the clause(query) must appear in matching documents. This is the reason for getting same results.

You may check this link

Score

The relevance score of each document is represented by a positive floating-point number called the _score. The higher the _score, the more relevant the document.

A query clause generates a _score for each document.

To know how score is calculated, refer this link

like image 152
Vijay Avatar answered Oct 12 '22 13:10

Vijay