# Get unique values from a field of all docs matching a top\_hits aggregation

**URL:** <https://discuss.elastic.co/t/get-unique-values-from-a-field-of-all-docs-matching-a-top-hits-aggregation/193638>\
**Category:** Elasticsearch\
**Created:** [August 3, 2019, 3:23pm UTC](https://discuss.elastic.co/t/get-unique-values-from-a-field-of-all-docs-matching-a-top-hits-aggregation/193638 "2019-08-03T15:23:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rf91](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@rf91](https://discuss.elastic.co/u/rf91)\
**Post date:** [August 3, 2019, 3:23pm UTC](https://discuss.elastic.co/t/get-unique-values-from-a-field-of-all-docs-matching-a-top-hits-aggregation/193638/1 "2019-08-03T15:23:44Z")

</div>

Hi,  
I'm new to ElasticSearch and am stuck with a query problem.

I have docs of the following form (simplified):

```auto
{   
    ...
    "mailProvider": "gmail.com",
    "mxHost1": {
      "name": "aspmx.l.google.com",
      ...
    },
    "mxHost2": {
      "name": "aspmx2.googlemail.com",
      ...
    },
    "mxHost3": {
      "name": "aspmx3.googlemail.com",
      ...
    }
}

```

With the following query, I get the top values for the `mailProvider` field:

```auto
POST /results/_search?size=0
{
    "aggs": {
        "top_providers": {
            "terms": {
                "field": "mailProvider.keyword",
                "size": 3
            }
        }
    }
}

```

This query results in:

```auto
{
  ...
  "aggregations" : {
    "top_providers" : {
        "buckets" : [
        {
          "key" : "gmail.com",
          "doc_count" : 138
        },
        {
          "key" : "outlook.com",
          "doc_count" : 43
        },
        {
          "key" : "secureserver.net",
          "doc_count" : 29
        }
      ]
    }
  }
}

```

So far so good. But now to my actual problem...

I need to get all **unique values** of `mxHost1.name`, `mxHost2.name` and `mxHost3.name` from all docs that have the `mailProvider` field set to _one of the **top\_hits** aggregation results_.

Any help is appreciated!

---

<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:** [August 31, 2019, 3:23pm UTC](https://discuss.elastic.co/t/get-unique-values-from-a-field-of-all-docs-matching-a-top-hits-aggregation/193638/2 "2019-08-31T15:23:46Z")

</div>

Closed on August 31, 2019, 3:23pm UTC

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