Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch : constant_score query vs bool.filter query

I am trying to achieve an exact match result using Elasticsearch (so I don't care about scoring here)

I see that there are 2 ways to do this :

{
    "query" : {
        "constant_score" : {
            "filter" : {
                "term" : {
                    "exact_match_field" : "hello world !"
                }
            }
        }
    }
}

or

{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "exact_match_field": "hello world !"
        }
      }
    }
  }
}

Both work and gives me the result I want. Whats the difference between them ? Are there performance benefits of using one vs the other ?

(I am using Elasticsearch V 5.6)

Thanks !

like image 559
sharath Avatar asked Jul 02 '18 19:07

sharath


People also ask

What is bool query in Elasticsearch?

The bool query is a go-to query because it allows you to construct an advanced query by chaining together several simple ones. The results must match the queries in this clause. If you have multiple queries, every single one must match. Acts as an and operator.

What is bool query?

A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery . It is built using one or more boolean clauses, each clause with a typed occurrence.


1 Answers

Constant score query gives an equal score to any matching document irrespective of any scoring factors like TF, IDF etc. This can be used when you don't care whether how much a doc matched but just if a doc matched or not and give a score too, unlike filter.

A constant_score query takes a boost argument that is set as the score for every returned document when combined with other queries. By default boost is set to 1.

If you are interested below link will give you more insight

https://www.compose.com/articles/elasticsearch-query-time-strategies-and-techniques-for-relevance-part-ii/

like image 141
Sunder R Avatar answered Sep 24 '22 08:09

Sunder R