# Most efficient query for counting

**URL:** <https://discuss.elastic.co/t/most-efficient-query-for-counting/11928>\
**Category:** Elasticsearch\
**Created:** [May 13, 2013, 5:07am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928 "2013-05-13T05:07:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)\
**Post date:** [May 13, 2013, 5:07am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/1 "2013-05-13T05:07:18Z")

</div>

I have a 180 million records index, the only queries that I need to do  
online are counting queries, over ranges and terms, there are no  
analyzed fields.

What is the most efficient query I can use, I have a machine with 16G, and  
the load is very small but when a request hits it must go as fast as  
possible

Here is an example of a query:

{"query":  
{"constant\_score":  
{"filter": {"bool": {"must": [  
{"term": {"state": "ct"}},  
{"term": {"most\_recent\_lender": "020"}},  
{"range": {"exact\_age": {"gte": "37", "lt": "198"}}},  
{"range": {"mortgage\_purchase\_price": {"gte": "92000", "lt":  
"450000"}}},  
{"range": {"second\_most\_recent\_mortgage\_amount": {"gte": "30000", "lt":  
"500000"}}},  
{"range": {"purchase\_second\_mortgage\_amount": {"gte": "0", "lt":  
"64000"}}},  
{"range": {"most\_recent\_mortgage\_date": {"gte": "1980", "lt": "4022"}}}  
]}}}}}

The number of fields may be up to 30 but the average will be 5-10

Are there any suggestions

Thank you

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [May 13, 2013, 6:26am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/2 "2013-05-13T06:26:53Z")

</div>

Hello David,

If you only need to count the matches of the query, you can use the Count  
API:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

Or is it something else you're looking for?

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Mon, May 13, 2013 at 8:07 AM, David MZ [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com) wrote:

> I have a 180 million records index, the only queries that I need to do  
> online are counting queries, over ranges and terms, there are no  
> analyzed fields.
> 
> What is the most efficient query I can use, I have a machine with 16G, and  
> the load is very small but when a request hits it must go as fast as  
> possible
> 
> Here is an example of a query:
> 
> {"query":  
> {"constant\_score":  
> {"filter": {"bool": {"must": [  
> {"term": {"state": "ct"}},  
> {"term": {"most\_recent\_lender": "020"}},  
> {"range": {"exact\_age": {"gte": "37", "lt": "198"}}},  
> {"range": {"mortgage\_purchase\_price": {"gte": "92000", "lt":  
> "450000"}}},  
> {"range": {"second\_most\_recent\_mortgage\_amount": {"gte": "30000",  
> "lt": "500000"}}},  
> {"range": {"purchase\_second\_mortgage\_amount": {"gte": "0", "lt":  
> "64000"}}},  
> {"range": {"most\_recent\_mortgage\_date": {"gte": "1980", "lt": "4022"}}}  
> ]}}}}}
> 
> The number of fields may be up to 30 but the average will be 5-10
> 
> Are there any suggestions
> 
> Thank you
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)\
**Post date:** [May 13, 2013, 6:34am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/3 "2013-05-13T06:34:13Z")

</div>

My question is more about performance rather then syntax

Any tips?

I am sure that here are things to speed up constant score type of queries

On May 13, 2013, at 9:26, Radu Gheorghe [radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com) wrote:

> Hello David,
> 
> If you only need to count the matches of the query, you can use the Count API:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count/)
> 
> Or is it something else you're looking for?
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Mon, May 13, 2013 at 8:07 AM, David MZ [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com) wrote:
> 
> > I have a 180 million records index, the only queries that I need to do online are counting queries, over ranges and terms, there are no analyzed fields.
> > 
> > What is the most efficient query I can use, I have a machine with 16G, and the load is very small but when a request hits it must go as fast as possible
> > 
> > Here is an example of a query:
> > 
> > {"query":  
> > {"constant\_score":  
> > {"filter": {"bool": {"must": [  
> > {"term": {"state": "ct"}},  
> > {"term": {"most\_recent\_lender": "020"}},  
> > {"range": {"exact\_age": {"gte": "37", "lt": "198"}}},  
> > {"range": {"mortgage\_purchase\_price": {"gte": "92000", "lt": "450000"}}},  
> > {"range": {"second\_most\_recent\_mortgage\_amount": {"gte": "30000", "lt": "500000"}}},  
> > {"range": {"purchase\_second\_mortgage\_amount": {"gte": "0", "lt": "64000"}}},  
> > {"range": {"most\_recent\_mortgage\_date": {"gte": "1980", "lt": "4022"}}}  
> > ]}}}}}
> > 
> > The number of fields may be up to 30 but the average will be 5-10
> > 
> > Are there any suggestions
> > 
> > Thank you
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US).  
> To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [May 13, 2013, 7:16am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/4 "2013-05-13T07:16:48Z")

</div>

Ah, OK. I'd use a filter directly, without wrapping it in a constant\_score  
query. Also, since you have only "must" clauses in your filter, you might  
want to try the AND  
filter[http://www.elasticsearch.org/guide/reference/query-dsl/and-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/and-filter/).  
Not sure if it's any faster than the bool filter, though.

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Mon, May 13, 2013 at 9:34 AM, David Mz [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com) wrote:

> My question is more about performance rather then syntax
> 
> Any tips?
> 
> I am sure that here are things to speed up constant score type of queries
> 
> On May 13, 2013, at 9:26, Radu Gheorghe [radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)  
> wrote:
> 
> Hello David,
> 
> If you only need to count the matches of the query, you can use the Count  
> API:  
> [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/api/count/)
> 
> Or is it something else you're looking for?
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Mon, May 13, 2013 at 8:07 AM, David MZ [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com)wrote:
> 
> > I have a 180 million records index, the only queries that I need to do  
> > online are counting queries, over ranges and terms, there are no  
> > analyzed fields.
> > 
> > What is the most efficient query I can use, I have a machine with 16G,  
> > and the load is very small but when a request hits it must go as fast as  
> > possible
> > 
> > Here is an example of a query:
> > 
> > {"query":  
> > {"constant\_score":  
> > {"filter": {"bool": {"must": [  
> > {"term": {"state": "ct"}},  
> > {"term": {"most\_recent\_lender": "020"}},  
> > {"range": {"exact\_age": {"gte": "37", "lt": "198"}}},  
> > {"range": {"mortgage\_purchase\_price": {"gte": "92000", "lt":  
> > "450000"}}},  
> > {"range": {"second\_most\_recent\_mortgage\_amount": {"gte": "30000",  
> > "lt": "500000"}}},  
> > {"range": {"purchase\_second\_mortgage\_amount": {"gte": "0", "lt":  
> > "64000"}}},  
> > {"range": {"most\_recent\_mortgage\_date": {"gte": "1980", "lt":  
> > "4022"}}}  
> > ]}}}}}
> > 
> > The number of fields may be up to 30 but the average will be 5-10
> > 
> > Are there any suggestions
> > 
> > Thank you
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 13, 2013, 11:06am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/5 "2013-05-13T11:06:00Z")

</div>

Radu - re the `and` filter: it won't be better than bool in this case. It's  
good for combining non-bitset filters (eg geo filters) but the bool filter  
is better for combing bitset filters, like all of the filters listed in the  
OP.

David: Currently you're using a "top-level" filter, ie the query executes,  
and only then is the filter applied. I'd rewrite the query to use a  
"filtered" query:

```
{
  query: {
    filtered: {
      query: { match_all: {}},
      filter: { bool: {...... }}
    }
  }
}

```

On Mon, May 13, 2013 at 9:16 AM, Radu Gheorghe  
[radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)wrote:

> Ah, OK. I'd use a filter directly, without wrapping it in a constant\_score  
> query. Also, since you have only "must" clauses in your filter, you might  
> want to try the AND filter[http://www.elasticsearch.org/guide/reference/query-dsl/and-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/and-filter/).  
> Not sure if it's any faster than the bool filter, though.
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Mon, May 13, 2013 at 9:34 AM, David Mz [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com)wrote:
> 
> > My question is more about performance rather then syntax
> > 
> > Any tips?
> > 
> > I am sure that here are things to speed up constant score type of queries
> > 
> > On May 13, 2013, at 9:26, Radu Gheorghe [radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)  
> > wrote:
> > 
> > Hello David,
> > 
> > If you only need to count the matches of the query, you can use the Count  
> > API:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count/)
> > 
> > Or is it something else you're looking for?
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > 
> > On Mon, May 13, 2013 at 8:07 AM, David MZ [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com)wrote:
> > 
> > > I have a 180 million records index, the only queries that I need to do  
> > > online are counting queries, over ranges and terms, there are no  
> > > analyzed fields.
> > > 
> > > What is the most efficient query I can use, I have a machine with 16G,  
> > > and the load is very small but when a request hits it must go as fast as  
> > > possible
> > > 
> > > Here is an example of a query:
> > > 
> > > {"query":  
> > > {"constant\_score":  
> > > {"filter": {"bool": {"must": [  
> > > {"term": {"state": "ct"}},  
> > > {"term": {"most\_recent\_lender": "020"}},  
> > > {"range": {"exact\_age": {"gte": "37", "lt": "198"}}},  
> > > {"range": {"mortgage\_purchase\_price": {"gte": "92000", "lt":  
> > > "450000"}}},  
> > > {"range": {"second\_most\_recent\_mortgage\_amount": {"gte": "30000",  
> > > "lt": "500000"}}},  
> > > {"range": {"purchase\_second\_mortgage\_amount": {"gte": "0", "lt":  
> > > "64000"}}},  
> > > {"range": {"most\_recent\_mortgage\_date": {"gte": "1980", "lt":  
> > > "4022"}}}  
> > > ]}}}}}
> > > 
> > > The number of fields may be up to 30 but the average will be 5-10
> > > 
> > > Are there any suggestions
> > > 
> > > Thank you
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)\
**Post date:** [May 13, 2013, 11:22am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/6 "2013-05-13T11:22:07Z")

</div>

Can you elaborate on the difference or point me where I Can read more about  
this issue  
Should this query be much faster? and do I need more memory for such  
queries? how are the bounded I/O or Memory

On Mon, May 13, 2013 at 2:06 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> Radu - re the `and` filter: it won't be better than bool in this case.  
> It's good for combining non-bitset filters (eg geo filters) but the bool  
> filter is better for combing bitset filters, like all of the filters listed  
> in the OP.
> 
> David: Currently you're using a "top-level" filter, ie the query executes,  
> and only then is the filter applied. I'd rewrite the query to use a  
> "filtered" query:
> 
> ```
> {
> query: {
> filtered: {
> query: { match_all: {}},
> filter: { bool: {...... }}
> }
> }
> }
> 
> ```
> 
> On Mon, May 13, 2013 at 9:16 AM, Radu Gheorghe \<[radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)
> 
> > wrote:
> 
> > Ah, OK. I'd use a filter directly, without wrapping it in a  
> > constant\_score query. Also, since you have only "must" clauses in your  
> > filter, you might want to try the AND filter[http://www.elasticsearch.org/guide/reference/query-dsl/and-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/and-filter/).  
> > Not sure if it's any faster than the bool filter, though.
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > 
> > On Mon, May 13, 2013 at 9:34 AM, David Mz [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com)wrote:
> > 
> > > My question is more about performance rather then syntax
> > > 
> > > Any tips?
> > > 
> > > I am sure that here are things to speed up constant score type of  
> > > queries
> > > 
> > > On May 13, 2013, at 9:26, Radu Gheorghe [radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)  
> > > wrote:
> > > 
> > > Hello David,
> > > 
> > > If you only need to count the matches of the query, you can use the  
> > > Count API:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count/)
> > > 
> > > Or is it something else you're looking for?
> > > 
> > > ## Best regards, Radu
> > > 
> > > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > > 
> > > On Mon, May 13, 2013 at 8:07 AM, David MZ [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com)wrote:
> > > 
> > > > I have a 180 million records index, the only queries that I need to do  
> > > > online are counting queries, over ranges and terms, there are no  
> > > > analyzed fields.
> > > > 
> > > > What is the most efficient query I can use, I have a machine with 16G,  
> > > > and the load is very small but when a request hits it must go as fast as  
> > > > possible
> > > > 
> > > > Here is an example of a query:
> > > > 
> > > > {"query":  
> > > > {"constant\_score":  
> > > > {"filter": {"bool": {"must": [  
> > > > {"term": {"state": "ct"}},  
> > > > {"term": {"most\_recent\_lender": "020"}},  
> > > > {"range": {"exact\_age": {"gte": "37", "lt": "198"}}},  
> > > > {"range": {"mortgage\_purchase\_price": {"gte": "92000", "lt":  
> > > > "450000"}}},  
> > > > {"range": {"second\_most\_recent\_mortgage\_amount": {"gte": "30000",  
> > > > "lt": "500000"}}},  
> > > > {"range": {"purchase\_second\_mortgage\_amount": {"gte": "0", "lt":  
> > > > "64000"}}},  
> > > > {"range": {"most\_recent\_mortgage\_date": {"gte": "1980", "lt": "4022  
> > > > "}}}  
> > > > ]}}}}}
> > > > 
> > > > The number of fields may be up to 30 but the average will be 5-10
> > > > 
> > > > Are there any suggestions
> > > > 
> > > > Thank you
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to the Google  
> > > > Groups "elasticsearch" group.  
> > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US)  
> > > .  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [May 14, 2013, 6:56am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/7 "2013-05-14T06:56:38Z")

</div>

On Mon, May 13, 2013 at 2:06 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> Radu - re the `and` filter: it won't be better than bool in this case.  
> It's good for combining non-bitset filters (eg geo filters) but the bool  
> filter is better for combing bitset filters, like all of the filters listed  
> in the OP.

Thanks, Clint! I didn't know that. And since I wanted to learn more, I  
found this very helpful article (note also the references at the bottom):  
[http://euphonious-intuition.com/2013/05/all-about-elasticsearch-filter-bitsets/](http://euphonious-intuition.com/2013/05/all-about-elasticsearch-filter-bitsets/)

Putting the link here in case it helps other people as well 🙂

Best regards,  
Radu

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)\
**Post date:** [May 14, 2013, 7:04am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/8 "2013-05-14T07:04:42Z")

</div>

I am still confused about the difference between using a filter and using a  
filtered query  
Can shed some light on this please

On Tue, May 14, 2013 at 9:56 AM, Radu Gheorghe  
[radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)wrote:

> On Mon, May 13, 2013 at 2:06 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:
> 
> > Radu - re the `and` filter: it won't be better than bool in this case.  
> > It's good for combining non-bitset filters (eg geo filters) but the bool  
> > filter is better for combing bitset filters, like all of the filters listed  
> > in the OP.
> 
> Thanks, Clint! I didn't know that. And since I wanted to learn more, I  
> found this very helpful article (note also the references at the bottom):
> 
> [http://euphonious-intuition.com/2013/05/all-about-elasticsearch-filter-bitsets/](http://euphonious-intuition.com/2013/05/all-about-elasticsearch-filter-bitsets/)
> 
> Putting the link here in case it helps other people as well 🙂
> 
> Best regards,  
> Radu
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 14, 2013, 7:27am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/9 "2013-05-14T07:27:19Z")

</div>

Hey,

Filtered queries are using the filter along side query execution (what you  
want in most cases).  
Top level filters (the "filter" field in your query) are executed after  
query execution. This allows you to get back these filtered results in the  
"hits" field, but also to have facets operating on the query without the  
filter being applied. In practice you have a top level filter for field  
'age' between 20 and 25, which means, your "hits" array will only include  
these results, but if you are facetting on the age field in the same search  
request, the facet calculation will be done without the filter being  
applied.

Hope this helps. If not, just ask further question.

--Alex

On Tue, May 14, 2013 at 9:04 AM, David MZ [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com) wrote:

> I am still confused about the difference between using a filter and using  
> a filtered query  
> Can shed some light on this please
> 
> On Tue, May 14, 2013 at 9:56 AM, Radu Gheorghe \<[radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)
> 
> > wrote:
> 
> > On Mon, May 13, 2013 at 2:06 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:
> > 
> > > Radu - re the `and` filter: it won't be better than bool in this case.  
> > > It's good for combining non-bitset filters (eg geo filters) but the bool  
> > > filter is better for combing bitset filters, like all of the filters listed  
> > > in the OP.
> > 
> > Thanks, Clint! I didn't know that. And since I wanted to learn more, I  
> > found this very helpful article (note also the references at the bottom):
> > 
> > [http://euphonious-intuition.com/2013/05/all-about-elasticsearch-filter-bitsets/](http://euphonious-intuition.com/2013/05/all-about-elasticsearch-filter-bitsets/)
> > 
> > Putting the link here in case it helps other people as well 🙂
> > 
> > Best regards,  
> > Radu
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)\
**Post date:** [May 14, 2013, 7:43am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/10 "2013-05-14T07:43:14Z")

</div>

So if my intention in to count the docs, I will get best performance from  
the filtered query

I must say that my original query is very fast, how much boost are we  
talking about, heuristically?

On Tue, May 14, 2013 at 10:27 AM, Alexander Reelsen [alr@spinscale.de](mailto:alr@spinscale.de)wrote:

> Hey,
> 
> Filtered queries are using the filter along side query execution (what you  
> want in most cases).  
> Top level filters (the "filter" field in your query) are executed after  
> query execution. This allows you to get back these filtered results in the  
> "hits" field, but also to have facets operating on the query without the  
> filter being applied. In practice you have a top level filter for field  
> 'age' between 20 and 25, which means, your "hits" array will only include  
> these results, but if you are facetting on the age field in the same search  
> request, the facet calculation will be done without the filter being  
> applied.
> 
> Hope this helps. If not, just ask further question.
> 
> --Alex
> 
> On Tue, May 14, 2013 at 9:04 AM, David MZ [david.mazvovsky@gmail.com](mailto:david.mazvovsky@gmail.com)wrote:
> 
> > I am still confused about the difference between using a filter and using  
> > a filtered query  
> > Can shed some light on this please
> > 
> > On Tue, May 14, 2013 at 9:56 AM, Radu Gheorghe \<  
> > [radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)\> wrote:
> > 
> > > On Mon, May 13, 2013 at 2:06 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:
> > > 
> > > > Radu - re the `and` filter: it won't be better than bool in this case.  
> > > > It's good for combining non-bitset filters (eg geo filters) but the bool  
> > > > filter is better for combing bitset filters, like all of the filters listed  
> > > > in the OP.
> > > 
> > > Thanks, Clint! I didn't know that. And since I wanted to learn more, I  
> > > found this very helpful article (note also the references at the bottom):
> > > 
> > > [http://euphonious-intuition.com/2013/05/all-about-elasticsearch-filter-bitsets/](http://euphonious-intuition.com/2013/05/all-about-elasticsearch-filter-bitsets/)
> > > 
> > > Putting the link here in case it helps other people as well 🙂
> > > 
> > > Best regards,  
> > > Radu
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US)  
> > > .  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/AIURb0tfVHI/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:36am UTC](https://discuss.elastic.co/t/most-efficient-query-for-counting/11928/11 "2017-07-06T02:36:51Z")

</div>


