elasticsearchboostterm-query

Apply different boosting values when searching for phrase in Elasticsearch?


I want to search for a phrase to Elasticsearch like "personal tax date". I want the returned results to give more weight to the term "tax".

So far I know how to boost entire index or boost for different fields but still don't know how to boost different terms? Any help??


Solution

  • Using function score we can boost by fields

    GET <index_name>/_search
    {
        "query": {
            "function_score": {
               "query": {
                "query_string": {
                   "query": "*personal tax date*",
                   "fields": [
                    "field_1",
                    "field_2"
                   ]
                }
                },
                "boost": "5",
                   "functions": [
                              {
                      "filter": { "match": { "field": "tax" } },
                      "weight": 30
                  },
                  {
                      "filter": { "term": { "ent_name": "tax" } },
                      "weight": 25
                  }
              ],
                 "score_mode": "multiply",
                "boost_mode": "sum"
            }
        }