# Get document from doc\_count

**URL:** <https://discuss.elastic.co/t/get-document-from-doc-count/232829>\
**Category:** Elasticsearch\
**Created:** [May 15, 2020, 1:12pm UTC](https://discuss.elastic.co/t/get-document-from-doc-count/232829 "2020-05-15T13:12:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Unpolygon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/unpolygon/32/47173_2.png) [@Unpolygon](https://discuss.elastic.co/u/Unpolygon)\
**Post date:** [May 15, 2020, 1:12pm UTC](https://discuss.elastic.co/t/get-document-from-doc-count/232829/1 "2020-05-15T13:12:24Z")

</div>

Is there a way to get document after composite aggregation?  
Assume after I aggregated and I get doc\_count = 5.  
I want to know these 5 documents in doc\_count because I have to analyze these documents.

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [May 15, 2020, 1:21pm UTC](https://discuss.elastic.co/t/get-document-from-doc-count/232829/2 "2020-05-15T13:21:05Z")

</div>

Would it be possible to share the whole query and the response? You can hide the fields and values.

---

<div class="post-metadata">

**Author:** ![Unpolygon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/unpolygon/32/47173_2.png) [@Unpolygon](https://discuss.elastic.co/u/Unpolygon)\
**Post date:** [May 15, 2020, 2:22pm UTC](https://discuss.elastic.co/t/get-document-from-doc-count/232829/3 "2020-05-15T14:22:27Z")

</div>

I want to count how many user ant,bird,cat,elep have bought product a.  
Assume my data look like these

```auto
{"Date":"20200515","product":["a","b","a","a","c"],"user":"ant","rank":"silver"}
{"Date":"20200515","product":["a","b","c","e","f"],"user":"ant","rank":"silver"}
{"Date":"20200515","product":["d","a","c","a","c"],"user":"bird","rank":"silver"}
{"Date":"20200515","product":["a","c","a","d","e"],"user":"cat","rank":"silver"}
{"Date":"20200515","product":["a","b","a","a","f"],"user":"cat","rank":"silver"}
{"Date":"20200515","product":["a","b","a","c","d"],"user":"elep","rank":"silver"}

```

And my query look like this

```auto
{
  "aggs":{
      "comp":{
         "composite":{
            "sources":[
               {
                  "log_date":{
                     "terms":{
                        "field":"Date.keyword"
                     }
                  }
               },
               {
                  "product":{
                     "terms":{
                        "field":"product.keyword",
                        "missing_bucket":true
                     }
                  }
               },
               {
                  "rank":{
                     "terms":{
                        "field":"rank.keyword",
                        "missing_bucket":true
                     }
                  }
               },
               {
                  "user":{
                     "terms":{
                        "field":"user.keyword",
                        "missing_bucket":true
                     }
                  }
               }
            ]
         }
      }
  }
}

```

and this is my result

```auto
Date user rank product doc_count
20200515 ant silver a 2
20200515 bird silver a 1  
20200515 cat silver a 2
20200515 elep silver a 1
...

```

And this is my expect result

```auto
Date user rank product doc_count amount
20200515 ant silver a 2 4
20200515 bird silver a 1 2
20200515 cat silver a 2 5
20200515 elep silver a 1 2

```

---

<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:** [June 12, 2020, 2:22pm UTC](https://discuss.elastic.co/t/get-document-from-doc-count/232829/4 "2020-06-12T14:22:49Z")

</div>

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