# Multiple cardinality aggregations

**URL:** <https://discuss.elastic.co/t/multiple-cardinality-aggregations/91027>\
**Category:** Elasticsearch\
**Created:** [June 27, 2017, 9:39pm UTC](https://discuss.elastic.co/t/multiple-cardinality-aggregations/91027 "2017-06-27T21:39:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SamA](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@SamA](https://discuss.elastic.co/u/SamA)\
**Post date:** [June 27, 2017, 9:39pm UTC](https://discuss.elastic.co/t/multiple-cardinality-aggregations/91027/1 "2017-06-27T21:39:40Z")

</div>

I am trying to create sub aggregations that will return the number of items in each category. For example:

```
{
  "aggs":{
    "stores":{
      "terms":{
        "field": "store_code",
        "order": {
          "_term": "asc"
        }
      },
      "aggs":{
        "item_count":{
          "cardinality":{
            "field": "items"
          }
        }
      },
      "aggs":{
        "item_count":{
          "cardinality":{
            "field": "franchises"
          }
        }
    }
  }
}

```

So the end product should be the store\_code's number of distinct items and franchises. However, I get an error saying duplicate key aggs. How can I make it so the sub aggregations display?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [June 28, 2017, 8:52am UTC](https://discuss.elastic.co/t/multiple-cardinality-aggregations/91027/2 "2017-06-28T08:52:32Z")

</div>

This is invalid JSON since you cannot have the same key twice in a JSON object (hence the error you are getting). To add multiple subaggregation to an aggregation you just need to include them both in the same `aggs` object. You also need to give your cardinality aggregations different names so that 1) its valid JSON but also more importantly 2) so you can distinguish because the aggregations in the response. Try the following:

```auto
{  
   "aggs":{  
      "stores":{  
         "terms":{  
            "field":"store_code",
            "order":{  
               "_term":"asc"
            }
         },
         "aggs":{  
            "item_count":{  
               "cardinality":{  
                  "field":"items"
               }
            },
            "franchise_count":{  
               "cardinality":{  
                  "field":"franchises"
               }
            }
         }
      }
   }
}

```

---

<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 26, 2017, 8:52am UTC](https://discuss.elastic.co/t/multiple-cardinality-aggregations/91027/3 "2017-07-26T08:52:39Z")

</div>

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