# Return multiple fields when performing aggregations

**URL:** <https://discuss.elastic.co/t/return-multiple-fields-when-performing-aggregations/262502>\
**Category:** Elasticsearch\
**Created:** [January 28, 2021, 1:13pm UTC](https://discuss.elastic.co/t/return-multiple-fields-when-performing-aggregations/262502 "2021-01-28T13:13:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArifAlili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arifalili/32/81463_2.png) [@ArifAlili](https://discuss.elastic.co/u/ArifAlili)\
**Post date:** [January 28, 2021, 1:13pm UTC](https://discuss.elastic.co/t/return-multiple-fields-when-performing-aggregations/262502/1 "2021-01-28T13:13:57Z")

</div>

Hi all,

Is there a way to return other fields besides "key" and "doc\_count" when performing aggregations? I don't want to do aggregation on multiple fields, I just want multiple fields to be returned in Aggregation results.

Below is my current setup, "attributes" is mapped as nested field.

I want to group by "searchid" as it is done now, but I also want the other fields inside "attributes" to be returned in the aggregation results, like "description.NL", "searchable", "public" etc.

Document looks like this

```auto
    {

        "id": "123990",
        "name": "Copy printer paper",
        "description": "Test product",
        "shortdescription": "",
        "keywords": "COPY,PRINTER",
        "keywords_custom": "",
        "attributes": [
            {
                "id": "ATTR_COLOR",
                "searchid": "attr_color",
                "description": {
                    "NL": "Product color"
                },
                "searchable": false,
                "public": false,
                "hidden": false,
                "type": "text",
                "textvalue": {
                    "NL": [
                        "WHITE"
                    ]
                }
            },
            {
                "id": "ATTR_FORMAT",
                "searchid": "attr_format",
                "description": {
                    "NL": "Product format"
                },
                "searchable": false,
                "public": false,
                "hidden": false,
                "type": "text",
                "textvalue": {
                    "NL": [
                        "A4"
                    ]
                }
            },
            {
                "id": "ATTR_PROMOTION",
                "searchid": "attr_promotion",
                "description": {
                    "NL": "Promotion"
                },
                "searchable": false,
                "public": false,
                "hidden": false,
                "type": "enum",
                "enumvalue": [
                    "N"
                ]
            }
        ]
    }

```

Query:

```auto
    "aggs":{
       "attributes":{
          "nested":{
             "path":"attributes"
          },
          "aggs":{
             "key":{
                "terms":{
                   "field":"attributes.searchid.keyword"
                },
                "aggs":{
                   "value":{
                      "terms":{
                         "field":"attributes.textvalue.NL.keyword"
                      }
                   }
                }
             }
          }
       }
    }

```

Current aggregation result:

```auto

    "attributes": {
        "doc_count": 10,
        "key": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 72,
            "buckets": [ {
                "key": "attr_color",
                "doc_count": 12,
                "value": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "WHITE",
                            "doc_count": 1
                        }
                    ]
                }
            },
            {
                "key": "attr_format",
                "doc_count": 12,
                "value": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [{
                            "key": "A4",
                            "doc_count": 1
                        }
                    ]
                }
            }]
        }
    }

```

Aggregation result that I want:

```auto
 {
    "attributes": {
        "doc_count": 10,
        "key": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 72,
            "buckets": [ {
                "key": "attr_color",
                ++ "desription.NL": "Product color",
                ++ "searchable": false,
                ++ "public": false,
                ++ "hidden": false,
                "doc_count": 12,
                "value": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "WHITE",
                            "doc_count": 1
                        }
                    ]
                }
            },
        }
    }
}

```

---

<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:** [February 25, 2021, 1:14pm UTC](https://discuss.elastic.co/t/return-multiple-fields-when-performing-aggregations/262502/2 "2021-02-25T13:14:10Z")

</div>

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