Kemal Can Kara
Kemal Can Kara

Reputation: 416

Elasticsearch - 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
            }
          }]
      }
    }
  ],
  "filter": [
    {
      "bool": {
        "must": [
          {
            "bool": {
              "must": [
                {
                  "term": {
                    "bla": {
                      "value": ""
                    }
                  }
                }
              ]
            }
          }, {
            "term": {
              "active": {
                "value": "true"
              }
            }
          },
          {
            "range": {
              "closingDate": {
                "gte": "201710310000",
                "lte": "999912312359"
              }
            }
          },

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.

Upvotes: 0

Views: 478

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Using function_score you can do something around these lines:

{
  "query": {
    "bool": {
      "must": [
        {
          "function_score": {
            "query": {
              "bool": {
                "should": [
                  {
                    "multi_match": {
                      "type": "best_fields",
                      "query": "bla",
                      "fields": [
                        "exampleField1^0",
                        "exampleField2^50",
                        "exampleField3^10",
                        "exampleField4^10",
                        "exampleField5^5"
                      ],
                      "boost": 50
                    }
                  }
                ]
              }
            },
            "functions": [
              {
                "filter": {
                  "ids": {
                    "values": [
                      1,
                      5
                    ]
                  }
                },
                "weight": 10
              }
            ],
            "score_mode": "max",
            "boost_mode": "multiply"
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "bla": {
                          "value": ""
                        }
                      }
                    }
                  ]
                }
              },
              {
                "term": {
                  "active": {
                    "value": "true"
                  }
                }
              },
              {
                "range": {
                  "closingDate": {
                    "gte": "201710310000",
                    "lte": "999912312359"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions