Aggregation: Custom score for ordering via sub aggregation

I have an Array of Objects with keywords:

"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:

{
   "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:

{
   "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:

{
   "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)

{
   "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!

ping please :slight_smile:

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