Terms Aggregation return multiple fields (min_doc_count: 0)


#1

I'm making a Terms Aggregation 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 SubAggregation like this:

"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:

"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