# Terms Aggregation return multiple fields (min\_doc\_count: 0)

**URL:** <https://discuss.elastic.co/t/terms-aggregation-return-multiple-fields-min-doc-count-0/168033>\
**Category:** Elasticsearch\
**Created:** [February 12, 2019, 12:57pm UTC](https://discuss.elastic.co/t/terms-aggregation-return-multiple-fields-min-doc-count-0/168033 "2019-02-12T12:57:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gertjanwytynck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gertjanwytynck/32/40831_2.png) [@gertjanwytynck](https://discuss.elastic.co/u/gertjanwytynck)\
**Post date:** [February 12, 2019, 12:57pm UTC](https://discuss.elastic.co/t/terms-aggregation-return-multiple-fields-min-doc-count-0/168033/1 "2019-02-12T12:57:25Z")

</div>

I'm making a [Terms Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html) but I want to return multiple fields. I want a user to select buckets via "slug" (my-name), but show the actual "name" (My Name).

At this moment I'm making a [TopHits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) SubAggregation like this:

```auto
"organisation": {
    "aggregations": {
        "label": {
            "top_hits": {
                "_source": {
                    "includes": [
                        "organisations.name"
                    ]
                },
                "size": 1
            }
        }
    },
    "terms": {
        "field": "organisations.slug",
        "min_doc_count": 0,
        "size": 20
    }
}

```

This gives the desired result when my whole query actually find some buckets/results.

You see I've set the `min_doc_count` to `0` which will return buckets with a doc count of 0. The problem I'm facing here is that my TopHits response is empty, which results of not being able to render the proper name to the client.

Example response:

```auto
"organisation": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
    {
        "key": "my-name",
        "doc_count": 27,
        "label": {
            "hits": {
                "total": 27,
                "max_score": 1,
                "hits": [
                {
                    "_index": "users",
                    "_type": "doc",
                    "_id": "4475",
                    "_score": 1,
                    "_source": {
                    "organisations": [
                        {
                            "name": "My name"
                        }]
                    }
                }]
            }
        }
    },
    {
        "key": "my-name-2",
        "doc_count": 0,
        "label": {
            "hits": {
                "total": 0,
                "max_score": null,
                "hits": []
            }
        }
    },
    .....

```

Anyone has accomplished this desired result? I feel like TopHits won't help me here. It should always fetch the name.

What I've also tried:

- Working with a terms sub aggregation. (same result)
- Working with a significant terms sub aggregation. (same result)

What I think could be a solution, but feels dirty:

- Index a new field with "`organisations.slug`\_\_\_`organisations.name`" and work the magic via this.
- Manual query the name field where the count is 0 (read TopHits is empty)

Kind regards,  
Thanks in advance

---

<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:** [March 12, 2019, 12:57pm UTC](https://discuss.elastic.co/t/terms-aggregation-return-multiple-fields-min-doc-count-0/168033/2 "2019-03-12T12:57:32Z")

</div>

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