# Better Performance of Elastic Query

**URL:** <https://discuss.elastic.co/t/better-performance-of-elastic-query/54753>\
**Category:** Elasticsearch\
**Created:** [July 5, 2016, 2:58pm UTC](https://discuss.elastic.co/t/better-performance-of-elastic-query/54753 "2016-07-05T14:58:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KaranPanchal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karanpanchal/32/10477_2.png) [@KaranPanchal](https://discuss.elastic.co/u/KaranPanchal)\
**Post date:** [July 5, 2016, 2:58pm UTC](https://discuss.elastic.co/t/better-performance-of-elastic-query/54753/1 "2016-07-05T14:58:51Z")

</div>

Hi, I am using Elastic search 2.2.0 for analytics in a high scale environment. I am facing a problem to choose a better query code which will give me a better performance.

I am also having 10-15 other queries which will be using same type of filtering, but the aggregations will be on different fields.

Should I use "filter" in query clause for filtering or should filter documents using "must"?

**Please give me suggestions to get better performance when query is executed.**

Herewith, I am placing a query with two sample formats:

1. Format using 'must' to filter the data in query clause

> {  
> "size": 0,  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "match": {  
> "type": "type1"  
> }  
> },  
> {  
> "exists": {  
> "field": "sample\_field"  
> }  
> }  
> ]  
> }  
> },  
> "aggs": {  
> "group\_by\_state": {  
> "terms": {  
> "field": "grouping\_field",  
> "size": 0  
> },  
> "aggs": {  
> "\_time": {  
> "terms": {  
> "field": "data\_time",  
> "size": 0  
> },  
> "aggs": {  
> "Max\_value": {  
> "max": {  
> "field": "field2"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }

2.Format using 'filter' and 'must' to filter the data in query clause

> {  
> "size": 0,  
> "query": {  
> "bool": {  
> "filter": [  
> {  
> "term": {  
> "type": "type1"  
> }  
> },  
> {  
> "exists": {  
> "field": "sample\_field"  
> }  
> }  
> ],  
> }  
> },  
> "aggs": {  
> "group\_by\_state": {  
> "terms": {  
> "field": "grouping\_field",  
> "size": 0  
> },  
> "aggs": {  
> "\_time": {  
> "terms": {  
> "field": "data\_time",  
> "size": 0  
> },  
> "aggs": {  
> "Max\_value": {  
> "max": {  
> "field": "field2"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [July 6, 2016, 4:08am UTC](https://discuss.elastic.co/t/better-performance-of-elastic-query/54753/2 "2016-07-06T04:08:41Z")

</div>

> [@KaranPanchal](#):
>
> Herewith, I am placing a query with two sample formats:

Which one is faster in your tests?

---

<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:** [July 5, 2017, 10:37pm UTC](https://discuss.elastic.co/t/better-performance-of-elastic-query/54753/3 "2017-07-05T22:37:44Z")

</div>


