# Top hits query working very slow while retrieving data

**URL:** <https://discuss.elastic.co/t/top-hits-query-working-very-slow-while-retrieving-data/235165>\
**Category:** Elasticsearch\
**Created:** [June 1, 2020, 12:46pm UTC](https://discuss.elastic.co/t/top-hits-query-working-very-slow-while-retrieving-data/235165 "2020-06-01T12:46:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pooja\_sanga](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pooja\_sanga](https://discuss.elastic.co/u/pooja_sanga)\
**Post date:** [June 1, 2020, 12:46pm UTC](https://discuss.elastic.co/t/top-hits-query-working-very-slow-while-retrieving-data/235165/1 "2020-06-01T12:46:17Z")

</div>

Hi,

I have a nested index with 27627 records. I running the following query and getting the result in 34s if i put size=100. If i run the query for size-2/3 i get the result in about 1s. If i run the same query without the top\_hit and includes, i get the result in less than a second. Can you please help me how I can optimize the query to get results faster for size 100 or above?

Query:  
{  
"\_source" : false,  
"aggs" : {

```
           "sub" : { 
					"terms" : { 
							"field" : "TYPE",
							},
 "aggs": {
            "top_sales_hits": {
                "top_hits": {
				   "size" : 100,
                   "_source": {
                        "includes": ["xyz_number"]
                    }
            }
        }         

```

}}

My problem statement is that i want to perform an aggregation based on the type and then display items(multiple details) that have specific type

Result should look something like this:

"aggregations": {  
"sub": {  
"doc\_count\_error\_upper\_bound": 552,  
"sum\_other\_doc\_count": 22844,  
"buckets": [  
{  
"key": "TYPE\_65",  
"doc\_count": 496,  
"top\_sales\_hits": {  
"hits": {  
"total": 496,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "xyz\_index",  
"\_type": "\_doc",  
"\_id": "1590142100595",  
"\_score": 1,  
"\_source": {  
"xyz\_number": "xyz\_number\_92"  
}  
},  
{  
"\_index": "xyz\_index",  
"\_type": "\_doc",  
"\_id": "1590142169605",  
"\_score": 1,  
"\_source": {  
"xyz\_number": "xyz\_number\_79"  
}  
},  
{  
"\_index": "xyz\_index",  
"\_type": "\_doc",  
"\_id": "1590142145126",  
"\_score": 1,  
"\_source": {  
"xyz\_number": "xyz\_number\_55"  
}  
}  
]  
}  
}  
},  
{  
"key": "TYPE\_64",  
"doc\_count": 495,  
"top\_sales\_hits": {  
"hits": {  
"total": 495,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "xyz\_index",  
"\_type": "\_doc",  
"\_id": "1590142121994",  
"\_score": 1,  
"\_source": {  
"xyz\_number": "xyz\_number\_91"  
}  
},  
{  
"\_index": "xyz\_index",  
"\_type": "\_doc",  
"\_id": "1590142168857",  
"\_score": 1,  
"\_source": {  
"xyz\_number": "xyz\_number\_91"  
}  
},  
{  
"\_index": "xyz\_index",  
"\_type": "\_doc",  
"\_id": "1590142143737",  
"\_score": 1,  
"\_source": {  
"xyz\_number": "xyz\_number\_99"  
}  
}  
]  
}  
}  
}

---

<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:** [June 29, 2020, 12:46pm UTC](https://discuss.elastic.co/t/top-hits-query-working-very-slow-while-retrieving-data/235165/2 "2020-06-29T12:46:30Z")

</div>

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