# Using Nested Field in Composite Aggregation

**URL:** https://discuss.elastic.co/t/using-nested-field-in-composite-aggregation/329359
**Category:** Elasticsearch
**Created:** [April 4, 2023, 11:57pm UTC](https://discuss.elastic.co/t/using-nested-field-in-composite-aggregation/329359 "2023-04-04T23:57:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![michael-jaxsta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael-jaxsta/32/98320_2.png) [@michael-jaxsta](https://discuss.elastic.co/u/michael-jaxsta)
#### Post date: [April 4, 2023, 11:57pm UTC](https://discuss.elastic.co/t/using-nested-field-in-composite-aggregation/329359/1 "2023-04-04T23:57:01Z")

</div>

This question has been asked a few times in this community but have not got a response so hopefully someone reads this and helps me out!

I'm trying to do a composite aggregation on documents which have nested fields. Specifically I want one of the nested fields to be part of the composite bucket.

My documents are related to musical charts where each document is a specific song on a specific chart for a specific date and look similar to the example below

```auto
{ 
   "provider": "Billboard",
   "start_date": "2023-03-23",
   "title": "Shake it Off",
   "artist": "Taylor Swift",
   "position": 1,
   "credits": 
   [
      { "name": "Jack Antonoff", "role": "Producer"},
      { "name": "Max Martin", "role": "Songwriter"},
   ]
}       

```

I want to be able to create a composite bucket aggregation including provider, title, artist, credits.name and credits.role. There is then a metric subaggregation for MIN(position) i.e. the best chart position each of the credited people achieved for a given song on a given chart.

The query below works but as soon as I add the nested field I get no hits.

```auto
{
  "aggs": {
    "my_buckets": {
      "composite": {
        "size": 1000,
        "sources": [
          { "title": { "terms": { "field": "title"}}},
          { "artist": { "terms": { "field": "artist"}}},
          { "provider": { "terms": { "field": "provider"}}},
        ]
      },
      "aggregations": {
        "peak_position": { "min": { "field": "position" }
        }
      }
    }
  }
}

```

Thanks!

Michael

---

<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 2, 2023, 11:57pm UTC](https://discuss.elastic.co/t/using-nested-field-in-composite-aggregation/329359/2 "2023-05-02T23:57:44Z")

</div>

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