Correction of score results

I would like to know if it is possible to implement this in elastic. Let's say I have documents of this structure.

{
 "title": "text1 text2 text3",
 "score_adj":[
  {"text":"text1", "adj": -2},
  {"text":"text3", "adj": -4}
 ]
}

And I will look for documents where the title contains the word text1, I will find this document, and also, since this word is in score_adj and it has -2, I will decrease the final score by this number. Is it possible to implement such logic? I know that there is script_score that allows you to change the score, but I don’t understand how you can repeat this situation.

Upvotes: 1

Views: 895

Answers (1)

Joe - Check out my books
Joe - Check out my books

Reputation: 16943

Here's one possibility:

{
  "query": {
    "bool": {
      "must": [
       
        {
          "function_score": {
            "query": {
              "match": {
                "title": "text1"
              }
            },
            "functions": [
              {
                "script_score": {
                  "script": {
                    "source": """
                    def found = params._source['score_adj'].find(item -> item.text==params.text);
                    if (found != null) {
                      // adj is always negative so use plus
                      return _score + found.adj
                    }
                    
                    return _score;
                  """,
                    "params": {
                      "text": "text1"
                    }
                  }
                }
              }
            ],
            "boost_mode": "replace"
          }
        }
      ]
    }
  }
}

but it may result in the error

script score function must not produce negative scores, but got [-1.7123179137706757]

so you'll have to take that into account. Maybe first multiply the original _score by 10 or something so you're guaranteed not go negative.

I think it'd be more reasonable to convert the adjs to percentages, not concrete values...

Upvotes: 1

Related Questions