# Elasticsearch sub aggregation not working with max

**URL:** <https://discuss.elastic.co/t/elasticsearch-sub-aggregation-not-working-with-max/242852>\
**Category:** Elasticsearch\
**Created:** [July 28, 2020, 7:48am UTC](https://discuss.elastic.co/t/elasticsearch-sub-aggregation-not-working-with-max/242852 "2020-07-28T07:48:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raza\_Jamil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raza_jamil/32/48235_2.png) [@Raza\_Jamil](https://discuss.elastic.co/u/Raza_Jamil)\
**Post date:** [July 28, 2020, 7:48am UTC](https://discuss.elastic.co/t/elasticsearch-sub-aggregation-not-working-with-max/242852/1 "2020-07-28T07:48:24Z")

</div>

I have nested data of shape: albums have tracks, tracks have versions.

```auto
album: {
   tracks: [
       {
         id: 1,
         mainTrackID: 1, // mainTrackID = id means main track
         versions: [
            {
               id: 2,
               mainTrackID: 1 // mainTrackID != id means it's a version
            }
         ]
     }
   ]
}

```

I want to aggregate the results of my query by albums, tracks, versions. Album with the highest track/version score should be on the top. Then within each album I want to group by mainTrackID with tracks/versions sorted by doc score desc.

I have this query that works _album\_track\_max\_score_ is set to _sum_ but not _max_ .

```auto
 "aggs": {
    "by_albumID": {
      "terms": {
        "field": "album.albumID",
        "size": 5,
            "order": [
                { "album_track_max_score": "desc" }
            ]
      },
      "aggs": {
        "by_mainTrackID": {
          "terms": {
            "field": "mainTrackID",
            "size": 10,
                "order": [
                    { "track_score": "desc" }
                ]
          },
          "aggs": {
            "by_top_hits": {
              "top_hits": {
                "_source": {
                  "include": [
                    "trackID",
                        "mainTrackID",
                        "album.albumDisplayTitle",
                        "trackDisplayTitle",
                        "releaseDate"
                  ]
                },
                "size": 10,
                "sort": [
                    { "_score": {"order": "desc"}}
                ]
              }
            },
            "track_score": {
            "max": {
                "script": "_score"
                }
            }
          }
        },
        "album_track_max_score": {
        "max": {
            "script": "_score"
            }
        }
      }
    }
  }

```

When _album\_track\_max\_score_ is set to _max_ the _by\_top\_hits_ agg only returns 1 track. When I set it to _sum_ everything works perfectly (it returns all versions grouped by mainTrackID) except the top album isn't the 1 with the highest score but one with the highest combined score of all tracks which does not fulfill the requirement.

I'm not sure how an aggregation in parent bucket is causing things to change in the child? I've tried using _track\_score_ in the track/version agg but it doesn't seem to do anything. I have tried many other permutations but it seems I'm missing some key concept here.

Any guidance is much appreciated.

---

<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:** [August 25, 2020, 7:48am UTC](https://discuss.elastic.co/t/elasticsearch-sub-aggregation-not-working-with-max/242852/2 "2020-08-25T07:48:25Z")

</div>

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