Patrick
Patrick

Reputation: 2719

Elasticsearch: how to to calculate avg _score on each bucket?

I have a match query and I want to do a terms aggregation (say on field doctype) to calculate on each bucket the average _score of the query. I don't find how to access the _score field within the aggregation. Someone can help? thanks, Patrick

Upvotes: 0

Views: 1299

Answers (1)

keety
keety

Reputation: 17441

To access score you would need to have scripting enabled. Then you can access the score in document as shown in example below:

"aggs": {
      "type_bucket": {
         "terms": {
            "field": "_type"
         },
        "aggs": {
            "avg_score": {
                "avg": {
                    "script": "_score"
                }
            }
        }
      }
    }

Upvotes: 4

Related Questions