0

How to multiply a result coming from Elasticsearch by a certain factor. For example the result from Elasticsearch without multiplying would look like this:

{ [ 1 => { score: 10 }, 2 => { score: 20 } ] }

I want the score to be multiplied by 4. The factor (here: 4) should be variable. The result would be:

{ [ 1 => { score: 40 }, 2 => { score: 80 } ] }

Is this possible, and, if yes, how would I do that?

Thanks in advance.

woltob
  • 370
  • 4
  • 13
  • Not sure if I'll have the time to write a proper answer but you probably want something called **boosting** which, apparently, has been replaced by something better called [function score](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html) in newer elasticsearch versions. Also, [this SO question](http://stackoverflow.com/questions/12427449/elasticsearch-boosting-relevance-based-on-field-value) can be of use – Felipe Feb 24 '15 at 01:06

1 Answers1

2

You need to use function score query for this. Inside that , in script function , you can multiple the score value ( available as _score inside the script )

{
  "query": {
    "function_score": {
      "query": { 
              // Query //
       },
      "functions": [
        {
          "script_score": {
            "script": "_score * boostFactor",
            "params": {
              "boostFactor": 4
            }
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace"
    }
  }
}

Use boost_mode as replace to replace the score given by query with the function score we have computed.

Vineeth Mohan
  • 18,633
  • 8
  • 63
  • 77