# How to get unique combination of multiple fields in elastic search in 1 bucket

**URL:** <https://discuss.elastic.co/t/how-to-get-unique-combination-of-multiple-fields-in-elastic-search-in-1-bucket/214719>\
**Category:** Elasticsearch\
**Created:** [January 12, 2020, 10:19am UTC](https://discuss.elastic.co/t/how-to-get-unique-combination-of-multiple-fields-in-elastic-search-in-1-bucket/214719 "2020-01-12T10:19:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pinky\_Agarwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pinky_agarwal/32/49330_2.png) [@Pinky\_Agarwal](https://discuss.elastic.co/u/Pinky_Agarwal)\
**Post date:** [January 12, 2020, 10:19am UTC](https://discuss.elastic.co/t/how-to-get-unique-combination-of-multiple-fields-in-elastic-search-in-1-bucket/214719/1 "2020-01-12T10:19:49Z")

</div>

I have an index with fields like that  
"\_source": {  
"ProductID": "7ab9772e-1e92-48c9-bfbb-a6d200b42fbe",  
"productName": "ABC",  
"theme\_id":"dsadsad",  
"theme\_name":"sdsd"  
}  
I want to get the unique key value pair like productid and corresponding product name , how can i achieve this

I tried something like this  
{  
"aggs":{"Theme":{"terms":{"script":{"source":"[doc['theme\_id'].value , doc['theme\_name'].value]"},"size":10000}}},"query":{"bool":{"must":[{"terms":{"globalId":["af897ece-6b99-4c23-aa73-a6d200b354f3"]}}]}},"size":0

but with this approach i m getting unique values in multiple bucket , different bucket for themeid and theme name .please help me to figure out this

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 13, 2020, 1:58pm UTC](https://discuss.elastic.co/t/how-to-get-unique-combination-of-multiple-fields-in-elastic-search-in-1-bucket/214719/2 "2020-01-13T13:58:28Z")

</div>

The script in a terms agg looks like the correct solution - which likely means I misunderstood the question.

Can you maybe describe how you want to buckets to look like compared to how the look now? Thank you!

---

<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:** [February 10, 2020, 1:58pm UTC](https://discuss.elastic.co/t/how-to-get-unique-combination-of-multiple-fields-in-elastic-search-in-1-bucket/214719/3 "2020-02-10T13:58:35Z")

</div>

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