# Does elastic search allow aggregations within a document? Or is there another way to do same

**URL:** <https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637>\
**Category:** Elasticsearch\
**Created:** [July 2, 2020, 12:58pm UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637 "2020-07-02T12:58:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![charvi23](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charvi23/32/103607_2.png) [@charvi23](https://discuss.elastic.co/u/charvi23)\
**Post date:** [July 2, 2020, 12:58pm UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637/1 "2020-07-02T12:58:24Z")

</div>

I am new to elastic search, I have a elastic search index example as follows: Category is my index and insight is field having array of elements.

"category":  
{  
"insight":  
{  
[  
{  
"topic":"xyz",  
"id":"10"  
} ,  
{  
"topic":"xyz",  
"id":"14"  
}  
,{  
"topic":"ihn",  
"id":"19"  
}  
]  
}  
}

I want to find the count of those elements within this document that have topic as "xyz". Is this possible using elastic search aggregation or does it work only with multiple documents.

---

<div class="post-metadata">

**Author:** ![defalt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/defalt/32/71379_2.png) [@defalt](https://discuss.elastic.co/u/defalt)\
**Post date:** [July 2, 2020, 1:07pm UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637/2 "2020-07-02T13:07:26Z")

</div>

I am not 100% sure what you mean. If you want to query all documents with the topic xyz you should set the topic as a `keyword` and use a terms query. It will give you back all your documents with xyz.

```auto
GET /_search
{
    "query" : {
        "terms" : {
            "topic" : ["xyz"]
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![charvi23](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charvi23/32/103607_2.png) [@charvi23](https://discuss.elastic.co/u/charvi23)\
**Post date:** [July 2, 2020, 1:20pm UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637/3 "2020-07-02T13:20:46Z")

</div>

Will this terms query or any other elastic search aggregation look for values within 1 document? If 1 document itself has topic xyz mentioned 10 times, will it count that as 10 ?

Index is as follows:

PUT /data\_level/\_doc/1?pretty

{  
"category\_id": 1,  
"category\_name": "yogurt",  
"insights": [  
{  
"insight\_id": 1,  
"insight\_min\_date": "11-12-2019",  
"insight\_max\_date": "31-12-2019",  
"topic": [  
"Meal Replacement",  
"Sustainable"  
},  
{  
"insight\_id": 2,  
"insight\_min\_date": "1-12-2019",  
"insight\_max\_date": "30-12-2019",  
"topic": [  
"Meal Replacement"  
}  
]

I want to result as:

Meal Replacement: 2  
Sustainable:1

---

<div class="post-metadata">

**Author:** ![charvi23](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charvi23/32/103607_2.png) [@charvi23](https://discuss.elastic.co/u/charvi23)\
**Post date:** [July 2, 2020, 1:21pm UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637/4 "2020-07-02T13:21:26Z")

</div>

This is all within 1 document. No separate documents here.

---

<div class="post-metadata">

**Author:** ![defalt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/defalt/32/71379_2.png) [@defalt](https://discuss.elastic.co/u/defalt)\
**Post date:** [July 2, 2020, 2:35pm UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637/5 "2020-07-02T14:35:17Z")

</div>

Not familiar with this type of query. Sorry

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [July 7, 2020, 12:27am UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637/6 "2020-07-07T00:27:50Z")

</div>

Aggregations work across documents, not within them.

You will probably need to split your documents out so that you have one for each topic for a given time range. So in your last example, you'd actually have 3 documents, that you'd then aggregate across/

---

<div class="post-metadata">

**Author:** ![charvi23](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charvi23/32/103607_2.png) [@charvi23](https://discuss.elastic.co/u/charvi23)\
**Post date:** [July 7, 2020, 7:46am UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637/7 "2020-07-07T07:46:58Z")

</div>

Yes, that's correct. I already did that and it worked correctly. Thanks for clarifying.

---

<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 4, 2020, 7:47am UTC](https://discuss.elastic.co/t/does-elastic-search-allow-aggregations-within-a-document-or-is-there-another-way-to-do-same/239637/8 "2020-08-04T07:47:01Z")

</div>

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