# Aggregate on array objects

**URL:** <https://discuss.elastic.co/t/aggregate-on-array-objects/138122>\
**Category:** Elasticsearch\
**Created:** [July 2, 2018, 6:21am UTC](https://discuss.elastic.co/t/aggregate-on-array-objects/138122 "2018-07-02T06:21:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksarsecha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ksarsecha/32/32852_2.png) [@ksarsecha](https://discuss.elastic.co/u/ksarsecha)\
**Post date:** [July 2, 2018, 6:21am UTC](https://discuss.elastic.co/t/aggregate-on-array-objects/138122/1 "2018-07-02T06:21:54Z")

</div>

I want to aggregate document based on entire array object and not single value inside array object.  
For example, given are the following documents

{  
"took": 15,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 10,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "user\_v1",  
"\_type": "user",  
"\_id": "1",  
"\_score": 1,  
"\_source": {  
"userName" : "testuser1",  
"field2": ["value1", "value2", "value3"]  
}  
},  
{  
"\_index": "user\_v1",  
"\_type": "user",  
"\_id": "2",  
"\_score": 1,  
"\_source": {  
"userName" : "testuser1",  
"field2": ["value1", "value2"]  
}  
},  
{  
"\_index": "user\_v1",  
"\_type": "user",  
"\_id": "3",  
"\_score": 1,  
"\_source": {  
"userName" : "testuser1",  
"field2": ["value3"]  
}  
},  
{  
"\_index": "user\_v1",  
"\_type": "user",  
"\_id": "4",  
"\_score": 1,  
"\_source": {  
"userName" : "testuser1",  
"field2": ["value1", "value2"]  
}  
},  
]  
}  
}

I want to aggregate on "field2" and expected response  
bucket 1 -\> ["value1", "value2", "value3"], count: 1  
bucket 2 -\> ["value1", "value2"], count: 2  
bucket 3 -\> ["value3"], count: 1

What is the best way to achieve this using aggregations?

---

<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 30, 2018, 6:21am UTC](https://discuss.elastic.co/t/aggregate-on-array-objects/138122/2 "2018-07-30T06:21:55Z")

</div>

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