# How to combine multiple fields and aggregate docs from it?

**URL:** https://discuss.elastic.co/t/how-to-combine-multiple-fields-and-aggregate-docs-from-it/40463
**Category:** Elasticsearch
**Created:** [January 29, 2016, 10:14am UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-and-aggregate-docs-from-it/40463 "2016-01-29T10:14:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dynamicscope](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@dynamicscope](https://discuss.elastic.co/u/dynamicscope)
#### Post date: [January 29, 2016, 10:14am UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-and-aggregate-docs-from-it/40463/1 "2016-01-29T10:14:39Z")

</div>

I am trying to combine two fields and aggregate for doc count from the combined field.

Let's say I have two fields as following.

> {  
> "width": 320,  
> "height": 480  
> }

I would like to combine these two fields and create a _ **temporarily** _ field called

> {  
> "resolution": "320x480"  
> }

And then aggregate the document counts based on the "resolution" and sort by it.

I couldn't find any solution for this.  
I can only think of creating a new field inside original doc.

Does anyone has a brilliant idea?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [January 29, 2016, 10:45am UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-and-aggregate-docs-from-it/40463/2 "2016-01-29T10:45:36Z")

</div>

I think you could use that: [https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script](https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script)

That said it will be more efficient if you generate this field at index time.

---

<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 5, 2017, 11:20pm UTC](https://discuss.elastic.co/t/how-to-combine-multiple-fields-and-aggregate-docs-from-it/40463/3 "2017-07-05T23:20:24Z")

</div>


