Elasticsearch: should bool query with filters

In the process of migration from Elasticsearch version 2 to Elasticsearch version 5, we also want to change the traditional order from “the most recent” to more accurate relevance search, using the score that use elasticsearch.

The new search its a should bool query, basically, this type of query score each hit of the bool query with a score:


  query:  {
    bool:  {
     should: [
      should_query1,
      should_query2,
      should_query3
     ]
   }
 }

In this type of query when a doc match any of the should_query it gets a score. What happens if you want to filter the docs? you simple add a filter to the query:


  query:  {
    bool:  {
     should: [
      should_query1,
      should_query2,
      should_query3
     ],
     filter: [ 
        myfilter
     ]
   }
 }

The problem is that the filter returns all the fields that match the filter with a score of 0, not only the ones that match any of the should query. So if the filter returns, say, 1000 docs, and the should query only scores 100 of them, you has 1000 records, 900 of them with score 0. Also if you return a lot of docs, the query is really slow.

To solve this problem, elasticsearch has the option “minimum_should_match” that indicates the minimum query in the should bool query that need to match in order to take into account in the result.

 

Leave a Reply

Your email address will not be published. Required fields are marked *