# Top\_hits aggregation performance problem

**URL:** <https://discuss.elastic.co/t/top-hits-aggregation-performance-problem/264336>\
**Category:** Elasticsearch\
**Created:** [February 15, 2021, 5:06pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-performance-problem/264336 "2021-02-15T17:06:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ozgun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ozgun/32/83011_2.png) [@ozgun](https://discuss.elastic.co/u/ozgun)\
**Post date:** [February 15, 2021, 5:06pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-performance-problem/264336/1 "2021-02-15T17:06:55Z")

</div>

Hello everyone,

I've a query which returns response in 3k-4k ms. When i remove top\_hits aggregations it reduces to 60-70 ms. Why is top\_hits aggregation so slow? How can i speed up my query? Is there any alternative to top\_hits aggregation?

That's my query btw:

```auto
POST payments/_search?size=0
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "currency.keyword": {
            "value": "EUR"
          }
        }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "2019-01-01",
              "lte": "2019-01-07"
            }
          }
        }
      ]
    }
  }, 
 "aggs": {
   "date_his": {
     "date_histogram": {
       "field": "@timestamp",
       "fixed_interval": "1m"
     },
     "aggs": {
       "volume": {
         "sum": {
           "field": "price"
         }
       },
       "max_price": {
         "max": {
           "field": "price"
         }
       },
       "min_price": {
         "min": {
           "field": "price"
         }
       },
       "first":{
         "top_hits": {
           "size": 1,
           "sort": [{"@timestamp":{"order":"asc"}}],
           "_source": {"includes": "price"}
         }
       },
       "last": {
         "top_hits": {
           "size": 1,
           "sort": [{"@timestamp": {"order":"desc"}}],
           "_source": {"includes": "price"}
         }
       }
     }
   }
 }
}

```

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [February 16, 2021, 8:31am UTC](https://discuss.elastic.co/t/top-hits-aggregation-performance-problem/264336/2 "2021-02-16T08:31:51Z")

</div>

I am wondering if this is the typical case where using top\_metrics instead of top\_hits should help:

> **[Top metrics aggregation | Elasticsearch Reference \[7.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-metrics.html)**

---

<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:** [March 16, 2021, 8:32am UTC](https://discuss.elastic.co/t/top-hits-aggregation-performance-problem/264336/3 "2021-03-16T08:32:26Z")

</div>

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