Boosting results based on selected types

I have different types indexed in elastic search. but, if I want to boost my results on some selected types then what should I do? I could use type filter in boosting query, but type filter allows me only one type to be used in filter. I need results to be boosted on the basis of multiple types.

Hi Ganesh,

One of the ways of getting the desired functionality is by wrapping your query inside a bool query and then make use of the should clause, in order to boost certain documents

Small example:

POST test/person
{
  "title": "london elise moore"
}

POST test/event
{
  "title" : "london is a great city"
}

Without boost:

GET test/_search
{
  "query": {
     "match": {
        "title": "london"
     }
   }
}

With has following response:

"hits": {
    "total": 2,
    "max_score": 0.2972674,
    "hits": [
      {
        "_index": "test",
        "_type": "person",
        "_id": "AVVx621GYvUb9aQn6r5X",
        "_score": 0.2972674,
        "_source": {
          "title": "london elise moore"
        }
      },
      {
        "_index": "test",
        "_type": "event",
        "_id": "AVVx63LrYvUb9aQn6r5Y",
        "_score": 0.26010898,
        "_source": {
          "title": "london is a great city"
        }
      }
    ]
  }

And now with the added should clause:

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "london"
          }
        }
      ],
      "should": [
        {
          "term": {
            "_type": {
              "value": "event",
              "boost": 2
            }
          }
        }
      ]
    }
  }
}

Which gives back the following response:

"hits": {
    "total": 2,
    "max_score": 1.0326607,
    "hits": [
      {
        "_index": "test",
        "_type": "event",
        "_id": "AVVx63LrYvUb9aQn6r5Y",
        "_score": 1.0326607,
        "_source": {
          "title": "london is a great city"
        }
      },
      {
        "_index": "test",
        "_type": "person",
        "_id": "AVVx621GYvUb9aQn6r5X",
        "_score": 0.04235228,
        "_source": {
          "title": "london elise moore"
        }
      }
    ]
  }

You could even leave out the extra boost in the should clause, cause if the should clause matches it will boost the result :slight_smile:

Hope this helps!

Thanks Byron. yes,Should clause could boost the results..but, I want to use the same in filters because I don't want to modify existing queries, rather i would add one filter which would boost the results depending on selected ES types. is it possible? if yes, then how?

Hey Ganesh,

I don't see any way of getting that functionality with just the use of filters. Filters narrow down your result set, they're not there to boost certain documents. Not sure if someone else has an idea on this.
Maybe you can post your query so that we'd be able to come up with a better alternative??

Kind regards,