# Top\_hits aggregation distinct nested documents

**URL:** <https://discuss.elastic.co/t/top-hits-aggregation-distinct-nested-documents/68922>\
**Category:** Elasticsearch\
**Created:** [December 13, 2016, 6:21pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-distinct-nested-documents/68922 "2016-12-13T18:21:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![buinauskas\_evaldas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buinauskas_evaldas/32/31439_2.png) [@buinauskas\_evaldas](https://discuss.elastic.co/u/buinauskas_evaldas)\
**Post date:** [December 13, 2016, 6:21pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-distinct-nested-documents/68922/1 "2016-12-13T18:21:35Z")

</div>

Is there a way to remove dupes from nested `top_hits` aggregation? My current query:

```
GET /analysis_elasticsearch/_search
{
  "size": 0,
  "aggs": {
    "Survey": {
      "nested": {
        "path": "Survey"
      },
      "aggs": {
        "TopHits": {
          "top_hits": {
            "size": 10,
            "_source": ["Survey.SurveyID", "Survey.SurveyName"], 
            "sort": "Survey.SurveyID"
          }
        }
      }
    }
  }
}

```

which returns following result:

```
{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "Survey": {
      "doc_count": 1000,
      "TopHits": {
        "hits": {
          "total": 1000,
          "max_score": null,
          "hits": [
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            }
          ]
        }
      }
    }
  }
}

```

I'd like to keep only unique nested records. I know I could use terms aggregation to get unique SurveyID and then do terms aggregation once more to get their names, but this doesn't feel right.

Is there a way to get this done using `top_hits` agg?

Elasticsearch version: 5.1.1  
I'm fine with using Painless to get desired result.

---

<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 10, 2017, 6:21pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-distinct-nested-documents/68922/2 "2017-01-10T18:21:34Z")

</div>

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