Boosting specific documents in every search

I'm very new to Elasticsearch. I'm using it to filtering and also boosting some fields at query time. This is the code part for boosting and filtering:

    "query": {
"bool": {
  "must": [
    {
      "bool": {
        "should": [
          {
            "multi_match": {
              "type": "best_fields",
              "query": "exampleKeyword",
              "fields": [
                "exampleField1^0",
                "exampleField2^50",
                "exampleField3^10",
                "exampleField4^10",
                "exampleField5^5"                  
              ],
              "boost": 50
            }
          },

Now I want to boost some specific documents. I'll give an array of integers for example Field6 and if my search results contain the elements of the array, these documents should get boosted with, I dont know, 100 to my scale.

How can I do this? Finally I dont want to expand the result set. Just want to boost more the desired ids if results contain these ids.

You could wrap your query in a function_score query. This query allows you to provide a filter. Any document that matches the filter (in addition to your original query) will get their score modified according to a boost_mode and a weight. By setting boost_mode to multiply and weight to 100, those documents matching the filter will get their score multiplied by 100. Like this:

GET _search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "multi_match": {
                      "type": "best_fields",
                      "query": "exampleKeyword",
                      "fields": [
                        "exampleField1^0",
                        "exampleField2^50",
                        "exampleField3^10",
                        "exampleField4^10",
                        "exampleField5^5"
                      ],
                      "boost": 50
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "terms": {
              "Field6": [
                1,
                2,
                3,
                42
              ]
            }
          },
          "weight": 100
        }
      ],
      "boost_mode": "multiply"
    }
  }
}
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.