4
votes

I am new to ES and trying to search using java apis. I am unable to figure out how I can provide filed specific boosting using the java apis. Here is the example: My index document looks like:

_source": {

"th_id": 1,
"th_name": "test name",
"th_description": "test desc",
"th_image": "test-img",
"th_slug": "Make-Me-Smart",
"th_show_title": "Coast Tech Podcast",
"th_sh_category": "Alternative Health

}

When i search for keywords I want to boost the results higher if they found in the "th_name" compared to they're found in some other fields. Currently I am using below code to do search:

QueryBuilder qb1 = QueryBuilders.multiMatchQuery(keyword, "th_name", "th_description", "th_show_title", "th_sh_category");
SearchResponse response = client.prepareSearch("talk").setTypes("themes")
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(qb1)
        .setFrom(start).setSize(maxRows)
        .setExplain(true).execute().actionGet();

Is there anything I can do at query time to boost the document if the keyword is found in "th_name" field compared to found in other fields?

3

3 Answers

16
votes

The accepted answer did not work me. ES version I am using is 6.2.4.

QueryBuilders.multiMatchQuery(keyword)
                            .field("th_name" ,2.0f)
                            .field("th_description")
                            .field("th_show_title")
                            .field("content")

Hope it helps someone else.

9
votes

Edit: This has changed and does no longer work in ES 6.x and upwards.

You should also be able to boost a field directly in the Multi-match query:

"The multi_match query supports field boosting via ^ notation in the fields json field.

{
  "multi_match" : {
    "query" : "this is a test",
    "fields" : [ "subject^2", "message" ]
  } 
}

In the above example hits in the subject field are 2 times more important than in the message field."

In the java-api, just use the MultiMatchQueryBuilder:

MultiMatchQueryBuilder builder = 
new MultiMatchQueryBuilder( keyword, "th_name^2", "th_description", "th_show_title", "th_sh_category" );

Disclaimer: Not tested