# Remove buckets from being returned in Aggregation

**URL:** https://discuss.elastic.co/t/remove-buckets-from-being-returned-in-aggregation/287107
**Category:** Elasticsearch
**Created:** [October 19, 2021, 2:37pm UTC](https://discuss.elastic.co/t/remove-buckets-from-being-returned-in-aggregation/287107 "2021-10-19T14:37:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![EricPSU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ericpsu/32/21110_2.png) [@EricPSU](https://discuss.elastic.co/u/EricPSU)
#### Post date: [October 19, 2021, 2:37pm UTC](https://discuss.elastic.co/t/remove-buckets-from-being-returned-in-aggregation/287107/1 "2021-10-19T14:37:27Z")

</div>

Is there a way to prevent the `buckets` array from being returned as a part of the aggregation result? I am using a `sum_bucket` to total all of the buckets and I don't need all of the buckets to be returned. I'm just using `key` and `displayCount`. I worry that with my dataset, there could be hundreds of buckets returned for hundreds of keys.

Meaning, if my default result is below:

```auto
{
  "aggregations" : {
    "Sports" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Football",
          "doc_count" : 5,
          "States" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "Maryland",
                "doc_count" : 1,
                "countStates" : {
                  "value" : 1
                }
              },
              {
                "key" : "New York",
                "doc_count" : 1,
                "countStates" : {
                  "value" : 1
                }
              },
              {
                "key" : "Ohio",
                "doc_count" : 1,
                "countStates" : {
                  "value" : 1
                }
              },
              {
                "key" : "Pennsylvania",
                "doc_count" : 1,
                "countStates" : {
                  "value" : 1
                }
              },
              {
                "key" : "Virginia",
                "doc_count" : 1,
                "countStates" : {
                  "value" : 1
                }
              }
            ]
          },
          "displayCount" : {
            "value" : 5.0
          }
        },
        {
          "key" : "Baseball",
          "doc_count" : 3,
          "States" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "Maryland",
                "doc_count" : 1,
                "countStates" : {
                  "value" : 1
                }
              },
              {
                "key" : "Ohio",
                "doc_count" : 1,
                "countStates" : {
                  "value" : 1
                }
              },
              {
                "key" : "Pennsylvania",
                "doc_count" : 1,
                "countStates" : {
                  "value" : 1
                }
              }
            ]
          },
          "displayCount" : {
            "value" : 3.0
          }
        }
	}
}

```

Can I remove the `buckets` array within the fields to only return the following:

```auto
{
  "aggregations" : {
    "Sports" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Football",
          "doc_count" : 5,
          "States" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0
          }
          "displayCount" : {
            "value" : 5.0
          }
        },
        {
          "key" : "Baseball",
          "doc_count" : 3,
          "States" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
          },
          "displayCount" : {
            "value" : 3.0
          }
        }
  }
}

```

---

<div class="post-metadata">

### Author: ![EricPSU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ericpsu/32/21110_2.png) [@EricPSU](https://discuss.elastic.co/u/EricPSU)
#### Post date: [October 19, 2021, 2:52pm UTC](https://discuss.elastic.co/t/remove-buckets-from-being-returned-in-aggregation/287107/2 "2021-10-19T14:52:25Z")

</div>

I'll add that I am aware I could use [filter\_paths](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#common-options-response-filtering) list to get what I want, but that feels like overkill for removing just one array from the entire result set. Also I am using the Java API, which filter\_paths is not available.

---

<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: [October 19, 2021, 3:10pm UTC](https://discuss.elastic.co/t/remove-buckets-from-being-returned-in-aggregation/287107/3 "2021-10-19T15:10:24Z")

</div>

Hi Eric,  
I'm not sure what produces the displayCount in your response?

I suspect you may be interested in the `cardinality` aggregation

---

<div class="post-metadata">

### Author: ![EricPSU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ericpsu/32/21110_2.png) [@EricPSU](https://discuss.elastic.co/u/EricPSU)
#### Post date: [October 19, 2021, 3:35pm UTC](https://discuss.elastic.co/t/remove-buckets-from-being-returned-in-aggregation/287107/4 "2021-10-19T15:35:15Z")

</div>

I'm not familiar enough with `cardinality` to know if it work with my use case, but I am more than happen to try if it does. Essentially I am creating a faceted search and I want my categories to display a count of potential results if that facet is selected. I can accomplish this with an `aggs` query in my `aggregation` like shown below.

Can `cardinality` do something similar? I am working with around 2 million documents so I worry if the solution is to run a script that performance would suffer.

```auto
{
  "query": {
    "match_all": {}
  },
  "post_filter": {
    "bool": {
      "must": [
        {
          "terms": {
            "sport.name.keyword": [
              "Football"
            ]
          }
        }
      ]
    }
  },
  "aggregations": {
    "States": {
      "terms": {
        "field": "state.name.keyword"
      },
      "aggs": {
        "Sports": {
          "terms": {
            "field": "sport.name.keyword",
            "include": [
              "Football"
            ],
            "size": 10000
          },
          "aggs": {
            "countSports": {
              "value_count": {
                "field": "_id"
              }
            }
          }
        },
        "displayCount": {
          "sum_bucket": {
            "buckets_path": "Sports>countSports"
          }
        }
      }
    }
  }
}

```

---

<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: [October 19, 2021, 3:44pm UTC](https://discuss.elastic.co/t/remove-buckets-from-being-returned-in-aggregation/287107/5 "2021-10-19T15:44:58Z")

</div>

When you swap a terms agg for a cardinality agg you’re effectively asking for a count of how many terms buckets would match without seeing the string values of any of them.

---

<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: [November 16, 2021, 3:45pm UTC](https://discuss.elastic.co/t/remove-buckets-from-being-returned-in-aggregation/287107/6 "2021-11-16T15:45:51Z")

</div>

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