# Nested Aggregation Top Hits and sort

**URL:** <https://discuss.elastic.co/t/nested-aggregation-top-hits-and-sort/143275>\
**Category:** Elasticsearch\
**Created:** [August 7, 2018, 8:16am UTC](https://discuss.elastic.co/t/nested-aggregation-top-hits-and-sort/143275 "2018-08-07T08:16:25Z")\
**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 7, 2018, 8:16am UTC](https://discuss.elastic.co/t/nested-aggregation-top-hits-and-sort/143275/1 "2018-08-07T08:16:26Z")

</div>

I have a nested structure at ES. Tags parameters is type equals nested. I want to sort by evaluationCount in tags and min price. after I wanna getting top\_hits. How can I do this?

example data is here:

```
   {"price":185,"tags":[{"evaluationCount":46,"tag":6},{"evaluationCount":824,"tag":7}],"destinationCity":"IST",},
{"price":285,"tags":[{"evaluationCount":16,"tag":6},{"evaluationCount":864,"tag":7}],"destinationCity":"LON",}

```

My query

```
  {
    "size": 0,
    "sort": [
      {
        "price": "asc"
      },
      {
        "tags.evaluationCount": "desc"
      }
    ],
    "aggs": {
      "tags-nested": {
        "nested": {
          "path": "tags"
        },
        "aggs": {
          "inner": {
            "filter": {
              "terms": {
                "tags.tag": [
                  1,
                  2
                ]
              }
            },
            "aggs": {
              "tag_id_term": {
                "terms": {
                  "field": "tags.tag",
                  "order": {
                    "_count": "desc"
                  }
                },
                "aggs": {
                  "by_top_hit": {
                    "top_hits": {
                      "size": 1
                    }
                  }
                }
              },
              "events.name_count": {
                "max": {
                  "field": "tags.evaluationCount"
                }
              }
            }
          }
        }
      }
    }
  }

```

Response

```
  "aggregations": {
    "tags-nested": {
      "doc_count": 120,
      "inner": {
        "doc_count": 8,
        "events.name_count": {
          "value": 1199965
        },
        "tag_id_term": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": 2,
              "doc_count": 6,
              "by_top_hit": {
                "hits": {
                  "total": 6,
                  "max_score": 1,
                  "hits": [
                    {
                      "_nested": {
                        "field": "tags",
                        "offset": 1
                      },
                      "_score": 1,
                      "_source": {
                        "evaluationCount": 2849,
                        "tag": 2
                      }
                    }
                  ]
                }
              }
            },
            {
              "key": 1,
              "doc_count": 2,
              "by_top_hit": {
                "hits": {
                  "total": 2,
                  "max_score": 1,
                  "hits": [
                    {
                      "_nested": {
                        "field": "tags",
                        "offset": 0
                      },
                      "_score": 1,
                      "_source": {
                        "evaluationCount": 564,
                        "tag": 1
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }
```

---

<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 4, 2018, 8:16am UTC](https://discuss.elastic.co/t/nested-aggregation-top-hits-and-sort/143275/2 "2018-09-04T08:16:29Z")

</div>

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