# What is the best approach to combine multiple aggregations based in same key

**URL:** <https://discuss.elastic.co/t/what-is-the-best-approach-to-combine-multiple-aggregations-based-in-same-key/274094>\
**Category:** Elasticsearch\
**Created:** [May 26, 2021, 4:28pm UTC](https://discuss.elastic.co/t/what-is-the-best-approach-to-combine-multiple-aggregations-based-in-same-key/274094 "2021-05-26T16:28:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasl](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@jasl](https://discuss.elastic.co/u/jasl)\
**Post date:** [May 26, 2021, 4:28pm UTC](https://discuss.elastic.co/t/what-is-the-best-approach-to-combine-multiple-aggregations-based-in-same-key/274094/1 "2021-05-26T16:28:09Z")

</div>

This is the query I am playing with:

```auto
    {
    	"aggs": {
    		"by_foo": {
    			"terms": {
    				"field": "foo.keyword",
    				"size": 100,
    				"exclude": ["Unknown"]
    			}
    		},
    		"by_bar": {
    			"terms": {
    				"field": "bar.keyword",
    				"size": 100,
    				"exclude": ["Unknown"]
    			}
    		}
    	},
    	"query": {
    		"range": {
    			"doc_date": {
    				"gte": "now-1y/d",
    				"lt": "now/d"
    			}
    		}
    	},
    	"size": 0
    }

```

the result is similar to:

```auto
    "aggregations": {
        "by_foo": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 6589,
          "buckets": [
            {
              "key": "A",
              "doc_count": 10
            },
            {
              "key": "B",
              "doc_count": 10
            }
          ]
        },
        "by_bar": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 11427,
          "buckets": [
            {
              "key": "B",
              "doc_count": 10
            },
            {
              "key": "C",
              "doc_count": 10
            }
          ]
        }
      }

```

```
I will like to have something similar to:

```

```auto
     "aggregations": {
        "by_foo_bar": {
          "buckets": [
            {
              "key": "A",
              "doc_count": 10
            },
            {
              "key": "B",
              "doc_count": 20
            },
            {
              "key": "C",
              "doc_count": 10
            },
          ]
        }
      }

```

---

<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:** [June 23, 2021, 4:29pm UTC](https://discuss.elastic.co/t/what-is-the-best-approach-to-combine-multiple-aggregations-based-in-same-key/274094/2 "2021-06-23T16:29:02Z")

</div>

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