Search Results in Elasticsearch using Boosting

Posted By : Tarun Tyagi | 30-Jun-2016

Boosting is process of enhancing the document relevancy.

Means if we have three fields in database like name,address,username and i want to get username result first after that get other results or we can say we’re more interested in the username clauses than we are in the name and address clauses. We need to tune the query to make the username clauses relatively more important.

 

The simplest weapon in our tuning arsenal is the boost parameter. To increase the weight of the username fields, give them a boost value higher than 1.

 

The boosting functionality allows you to alter the score of a document. You can choose to place a document on top and can also give negative boost value to come last in the result set.

 

You can either boost at index time or query time. I usually prefer query time boosting even though it makes queries a little bit slower, otherwise I'd need to reindex every time I want to change my boosting factors, which usally need fine-tuning and need to be pretty flexible.


There are different ways to apply query time boosting using the elasticsearch query DSL:

  • Boosting Query
  • Custom Filters Score Query
  • Custom Boost Factor Query
  • Custom Score Query

Boosting query:

a. Boosting with query string

{
    "query_string" : {
        "fields" : ["name","address","username^5"],
        "query" : "rajeev"
    }
}

the username is boosted by 5 using ^5 notation

b. Boosting with match query

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "username": {
              "query": "rajeev",
              "boost": 2 
            }
          }
        },
        {
          "match": { 
            "name": "rajeev"
          }
        },
        {
          "match": { 
            "address": "rajeev"
          }
        }
      ]
    }
  }
}

here username is boosted by 2 using boost

you can use boosting in all type of query

 

Thanks

 

About Author

Author Image
Tarun Tyagi

Tarun is a bright Java developer with experience in MEAN stack and Grails frameworks. Other than programming his area of interest are listening music and reading novels.

Request for Proposal

Name is required

Comment is required

Sending message..