Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to boost search based on index type in elasticsearch or lucene?

I have three food type indices "Italian", "Spanish", "American". When the user searches "Cheese", documents from "Italian" appear to come up at the top. Is it possible to "boost" the results if I were to give preference to say "Spanish"? (I should still get results for Italian, but based on some numeric boost value for index type "Spanish", the ordering of the documents returned in the results give preference to the "Spanish" index. Is this possible in user input lucene and/or ES query? If so, how?

like image 296
Rolando Avatar asked Jul 18 '13 15:07

Rolando


3 Answers

If querying several indices at once, it is possible to specify indices boost at the top level of object passed to Search API:

curl -XGET localhost:9200/italian,spanish,american/_search -d '
{
    "query":{"term":{"food_type":"cheese"}},
    "indices_boost" : {
        "ilalian" : 1.4,
        "spanish" : 1.3,
        "american" : 1.1
    }
}'

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-index-boost.html#search-request-index-boost

like image 187
Alexey Tigarev Avatar answered Oct 03 '22 11:10

Alexey Tigarev


Add a term query with a boost for either the _type field or the _index (or both).

like image 30
Jilles van Gurp Avatar answered Oct 03 '22 09:10

Jilles van Gurp


Use a script_score as part of the function score query:

function_score: {
  script_score: {
    script: "doc['_type'].value == '<your _type>' ? _score * <boost_factor> : _score"
  }
}
like image 27
user2205763 Avatar answered Oct 03 '22 11:10

user2205763