# Term aggregations from union of nested list

**URL:** <https://discuss.elastic.co/t/term-aggregations-from-union-of-nested-list/143463>\
**Category:** Elasticsearch\
**Created:** [August 8, 2018, 9:22am UTC](https://discuss.elastic.co/t/term-aggregations-from-union-of-nested-list/143463 "2018-08-08T09:22:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Csaba\_Peller](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@Csaba\_Peller](https://discuss.elastic.co/u/Csaba_Peller)\
**Post date:** [August 8, 2018, 9:22am UTC](https://discuss.elastic.co/t/term-aggregations-from-union-of-nested-list/143463/1 "2018-08-08T09:22:06Z")

</div>

Hi All,

I would like to do a simple count aggregation from a distinct list of my union of two nested list. But I have no idea, how can i do this.  
This is my sample structure:  
"\_source": {  
"productId": 1,  
"materialMain": [  
{  
"name": "Wood",  
"id": 15  
},  
{  
"name": "Oak",  
"id": 151  
}],  
"materialSecondary": [  
{  
"name": "Wood",  
"id": 15  
},  
{  
"name": "Spruce",  
"id": 152  
}]  
}

The accepted result should be:  
Wood, Id:15, count = 1  
Oak, Id:151, count = 1  
Spruce, Id: 152, count = 1

Thank you for your help in advance!  
Csaba

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 10, 2018, 12:37pm UTC](https://discuss.elastic.co/t/term-aggregations-from-union-of-nested-list/143463/2 "2018-08-10T12:37:17Z")

</div>

All the terms live in one doc.  
The `terms` aggregation returns the term values eg "Wood" and a doc\_count. The doc\_count is the number of docs that contain a term, not the total number of times the term is mentioned in the docs.

The only complication I see is that you're getting the terms from \>1 field, in which case you'd need to use a script to merge the sets of values from the 2 fields before returning them to the terms aggregation.

---

<div class="post-metadata">

**Author:** ![Csaba\_Peller](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@Csaba\_Peller](https://discuss.elastic.co/u/Csaba_Peller)\
**Post date:** [August 10, 2018, 12:50pm UTC](https://discuss.elastic.co/t/term-aggregations-from-union-of-nested-list/143463/3 "2018-08-10T12:50:33Z")

</div>

Okey, I am going to try script to merge arrays.

Thank you for your answer

---

<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:** [September 7, 2018, 12:50pm UTC](https://discuss.elastic.co/t/term-aggregations-from-union-of-nested-list/143463/4 "2018-09-07T12:50:34Z")

</div>

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