Search code examples
elasticsearchfieldboosting

Elasticsearch Query: Boosting specific field


I am using Elasticsearch 2.4.3 and want to boost specific fields in my query. Is this possible? I only see how I can boost an index.

Greetings!

UPDATE

Mapping:

"firstName":{"type":"string",
     "analyzer":"customNGram"
},
"lastName":{
     "type":"string",
     "analyzer":"customNGram"
},
"note":{
     "type":"string",
     "analyzer":"customNGram"
}

Query (Java API):

    QueryBuilder qb = new BoolQueryBuilder()
        .must(QueryBuilders.matchQuery("_all", term)
                .analyzer("atsCustomSearchAnalyzer")
                .operator(Operator.AND));

    SearchRequestBuilder searchRequestBuilder = elasticsearchClient.prepareSearch("persons", "activities").setTypes("person", "activity")
            .setQuery(qb)
            .addHighlightedField("*").setHighlighterRequireFieldMatch(false)
            .setHighlighterOrder("score")
            .setHighlighterFragmentSize(150)
            .setHighlighterForceSource(true)
            .setSize(100)
            .addIndexBoost("persons", 200)
            .setFrom(offset); 

    return searchRequestBuilder.execute().get();

Solution

  • If you split up your match-query to match individual fields, eg using a multi match query (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html), you can boost the field you like. So something like:

    QueryBuilder qb = new BoolQueryBuilder()
        .must(QueryBuilders.multiMatchQuery(term, "firstName^3",
        "lastName^3", "note")
                .analyzer("atsCustomSearchAnalyzer")
                .operator(Operator.AND));
    

    should boost firstName and lastName 3 times relative to the note field.