# Elasticsearch Nested Aggregation

**URL:** <https://discuss.elastic.co/t/elasticsearch-nested-aggregation/143198>\
**Category:** Elasticsearch\
**Created:** [August 6, 2018, 5:24pm UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregation/143198 "2018-08-06T17:24:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ercancavusoglu](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@ercancavusoglu](https://discuss.elastic.co/u/ercancavusoglu)\
**Post date:** [August 6, 2018, 5:24pm UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregation/143198/1 "2018-08-06T17:24:13Z")

</div>

I have some documents in Elasticsearch. I want to aggregate with tags equals 5 and sort by evaluationCount in aggregates data.

I used min query at busEvaluationCount, but I want to aggregating for tags:5. I dont got it how can I do this?

```
      id:1
     {"date":"2017-12-24","lastUpdatedAt":"2017-12-24 00:04:46","origin":"LON","destination":"LHR","price":1685.44,"originCity":"LON","globalRank":4,"tags":[{"evaluationCount":199965,"tag":1},{"evaluationCount":199930,"tag":5},],"destinationCity":"LON",}

      id:2
      {"date":"2017-12-24","lastUpdatedAt":"2017-12-24 00:04:46","origin":"LON","destination":"LHR","price":1285.44,"originCity":"LON","globalRank":4,"tags":[{"evaluationCount":1000,"tag":1},{"evaluationCount":2000,"tag":5},],"destinationCity":"LON",}

```

This here the search section.

```
      {"aggs":{"groupByCity":{"aggs":{"busEvaluationCount":{"min":{"field":"tags.evaluationCount"}},"busGlobalRankCount":{"min":{"field":"globalRank"}},"busPrice":{"min":{"field":"price"}},"sourceDocument":{"aggs":{"dedupDocs":{"top_hits":{"size":1,"sort":[{"tags.evaluationCount":"desc"},{"price":"asc"}]}}},"terms":{"field":"destinationCity.keyword","size":1}}},"terms":{"field":"destinationCity.keyword","order":{"busEvaluationCount":"desc"},"size":1000000}}},"query":{"bool":{"must":[{"match":{"originCity":"LON"}},{"query_string":{"query":"tags.tag:5"}},{"range":{"date":{"gte":"2018-08-06","include_lower":true,"include_upper":true,"lte":"2019-02-06"}}}]}},"size":0,"sort":{"price":{"order":"asc"}}}
```

---

<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:** [September 3, 2018, 5:24pm UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregation/143198/2 "2018-09-03T17:24:19Z")

</div>

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