# I have a question about field aggregation

**URL:** <https://discuss.elastic.co/t/i-have-a-question-about-field-aggregation/69377>\
**Category:** Elasticsearch\
**Created:** [December 19, 2016, 2:22am UTC](https://discuss.elastic.co/t/i-have-a-question-about-field-aggregation/69377 "2016-12-19T02:22:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zplzpl](https://avatars.discourse-cdn.com/v4/letter/z/d6d6ee/32.png) [@zplzpl](https://discuss.elastic.co/u/zplzpl)\
**Post date:** [December 19, 2016, 2:22am UTC](https://discuss.elastic.co/t/i-have-a-question-about-field-aggregation/69377/1 "2016-12-19T02:22:43Z")

</div>

Category is a nested field, there are two fields, one is category\_id, one is name., I now use aggregate statement, according to the ID polymerization, then the polymerization results inside can only see the key value is category\_id, if I want to show the other word segment under key, such as name fields. How to write?

```
"category": [
              {
                 "category_id": 1,
                 "name": "category_name_1"
              },
              {
                 "category_id": 2,
                 "name": "category_name_2"
              },
              {
                 "category_id": 2,
                 "name": "category_name_3"
              }
           ]

```

The results:

```
"category": {
     "doc_count": 675,
     "category_id": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
           {
              "key": 2,
              "doc_count": 450
           },
           {
              "key": 1,
              "doc_count": 225
           }
        ]
     }
  }

```

How to read simultaneously to category name?

---

<div class="post-metadata">

**Author:** ![askids](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/askids/32/22252_2.png) [@askids](https://discuss.elastic.co/u/askids)\
**Post date:** [December 19, 2016, 3:25am UTC](https://discuss.elastic.co/t/i-have-a-question-about-field-aggregation/69377/2 "2016-12-19T03:25:54Z")

</div>

Assuming that id and category name will have a 1 to 1 mapping, just do a sub aggregation on category name under id. It will give count of unique list of category under that id. For this to work, I believe that category name will have to be defined as keyword instead of text. Just check that once.

---

<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:** [January 16, 2017, 3:26am UTC](https://discuss.elastic.co/t/i-have-a-question-about-field-aggregation/69377/3 "2017-01-16T03:26:06Z")

</div>

Closed on January 16, 2017, 3:26am UTC

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