Erkethan
Erkethan

Reputation: 15

Normalize Elasticsearch score with subfield length

I have some documents in ElasticSearch, and each document has one or many appendices. When I do a search on the text of the appendices, I often got scores greater than 1. When I do searches with really common patterns, I realize that more the document has appendices, greater its score is. So, I aim to 'normalize' the scores, by dividing each document score by the number of appendices it has.

For example, for this query:

"query": {
  "match": {
    "document.appendices.text" = "What is love?"
  }
}

I wish to do score = score / length(document.appendices) but I don't know how to express it.

It that possible, or all the appendices has to be indexed as document to achieve this?

Many thanks community!

Upvotes: 0

Views: 428

Answers (1)

Tuhina Chatterjee
Tuhina Chatterjee

Reputation: 11

Use a function_score query to alter the already calculated score _score

GET /_search
{
    "query": {
        "function_score": {
            "query": {
                "match": { "document.appendices.text": "What is love?" }
            },
            "script_score" : {
                "script" : {
                  "source": "_score/doc['document.appendices'].length"
                }
            }
        }
    }
}

Refer https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#score-functions

Upvotes: 0

Related Questions