# Aggregation: Custom score for ordering via sub aggregation

**URL:** <https://discuss.elastic.co/t/aggregation-custom-score-for-ordering-via-sub-aggregation/356663>\
**Category:** Elasticsearch\
**Tags:** aggregations\
**Created:** [April 3, 2024, 10:16am UTC](https://discuss.elastic.co/t/aggregation-custom-score-for-ordering-via-sub-aggregation/356663 "2024-04-03T10:16:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Berlin](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Martin\_Berlin](https://discuss.elastic.co/u/Martin_Berlin)\
**Post date:** [April 3, 2024, 10:16am UTC](https://discuss.elastic.co/t/aggregation-custom-score-for-ordering-via-sub-aggregation/356663/1 "2024-04-03T10:16:29Z")

</div>

I have an Array of Objects with keywords:

```auto
"keywords":[
   {
      "name":"Testing Equipment",
      "score":0.999
   },
   {
      "name":"Film Shrinkage Tester",
      "score":0.666
   },
   {
      "name":"Universal Tensile Tester",
      "score":0.332
   },
   {
      "name":"Hydraulic Pressure Testing Machine",
      "score":0.122
   }
]

```

with the following mapping:

```auto
{
   "keywords":{
      "properties":{
         "score":{
            "type":"float"
         },
         "name":{
            "type":"text",
            "fields":{
               "keyword":{
                  "ignore_above":256,
                  "type":"keyword"
               }
            }
         }
      }
   }
}

```

I want the aggregation of the keywords to be sorted by the sum of its score.

I'm trying like this:

```auto
{
   "aggs":{
      "keywords":{
         "terms":{
            "size":10,
            "field":"keywords.name.keyword",
            "order":{
               "keywords_score.value":"desc"
            }
         },
         "aggs":{
            "keywords_score":{
               "sum":{
                  "field":"keywords.score"
               }
            }
         }
      }
   }
}

```

But it seems like the sub-aggregation keywords\_score takes all of the scores related to all keywords into account

For testing I changed the sum sub aggregation to terms:

```auto
{
   "aggs":{
      "keywords":{
         "terms":{
            "size":10,
            "field":"keywords.name.keyword"
         },
         "aggs":{
            "keywords_score":{
               "terms":{
                  "field":"keywords.name.keyword"
               }
            }
         }
      }
   }
}

```

with this output (just to see how what the sum function would take into account) - I would expect that there is only one keyword (the one found in the upper aggregation)

```auto
{
   "buckets":[
      {
         "key":"Testing Equipment",
         "doc_count":18707,
         "keywords_score":{
            "doc_count_error_upper_bound":1754,
            "sum_other_doc_count":1701567,
            "buckets":[
               {
                  "key":"Film Shrinkage Tester",
                  "doc_count":18707
               },
               {
                  "key":"Universal Tensile Tester",
                  "doc_count":4305
               },
               {
                  "key":"Hydraulic Pressure Testing Machine",
                  "doc_count":3647
               },
               .....
            ]
         }
      }
   }

```

That's about the same way the sum get's aggregated - the sum of all other keyword score properties and not only the one associated with the parent aggregation.  
Is there a way to **do a custom ranking via sub aggregation which takes only the sum of the scores associated with the parent keyword** into account?

Thanks for helping!

---

<div class="post-metadata">

**Author:** ![Martin\_Berlin](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Martin\_Berlin](https://discuss.elastic.co/u/Martin_Berlin)\
**Post date:** [April 17, 2024, 1:21pm UTC](https://discuss.elastic.co/t/aggregation-custom-score-for-ordering-via-sub-aggregation/356663/2 "2024-04-17T13:21:56Z")

</div>

ping please 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 15, 2024, 1:22pm UTC](https://discuss.elastic.co/t/aggregation-custom-score-for-ordering-via-sub-aggregation/356663/3 "2024-05-15T13:22:47Z")

</div>

Closed on May 15, 2024, 1:22pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
