# How to get docs in aggregated format in ElasticSearch aggregation query?

**URL:** <https://discuss.elastic.co/t/how-to-get-docs-in-aggregated-format-in-elasticsearch-aggregation-query/335292>\
**Category:** Elasticsearch\
**Created:** [June 6, 2023, 7:27am UTC](https://discuss.elastic.co/t/how-to-get-docs-in-aggregated-format-in-elasticsearch-aggregation-query/335292 "2023-06-06T07:27:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![maulik\_trapasiya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maulik_trapasiya/32/121816_2.png) [@maulik\_trapasiya](https://discuss.elastic.co/u/maulik_trapasiya)\
**Post date:** [June 6, 2023, 7:27am UTC](https://discuss.elastic.co/t/how-to-get-docs-in-aggregated-format-in-elasticsearch-aggregation-query/335292/1 "2023-06-06T07:27:25Z")

</div>

My query

```auto
{
  "aggs": {
    "distinct_colours": {
      "terms": {
        "field": "colour"
      }
    }
  }
}

```

Required Result:

```auto
{
  "took" : 2037,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "distinct_colours" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 34,
      "buckets" : [
        {
          "key" : "BLACK",
          "docs" : [{
            "id": "block_doc_key",
            "avialability": true
          }]
        },
        {
          "key" : "GREEN",
          "doc" : [{
            "id": "green_doc_key",
            "avialability": false
          }]
        }
      ]
    }
  }
}

```

Can I get the result docs corresponding to each bucket as I have mentioned above (in "Required Result")? I am not able to find the correct query for this. please help.

---

<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:** [July 4, 2023, 7:27am UTC](https://discuss.elastic.co/t/how-to-get-docs-in-aggregated-format-in-elasticsearch-aggregation-query/335292/2 "2023-07-04T07:27:33Z")

</div>

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