# Query on \_search not working for \_count

**URL:** <https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357>\
**Category:** Elasticsearch\
**Created:** [April 16, 2012, 6:07pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357 "2012-04-16T18:07:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JP\_Toto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jp_toto/32/2512_2.png) [@JP\_Toto](https://discuss.elastic.co/u/JP_Toto)\
**Post date:** [April 16, 2012, 6:07pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/1 "2012-04-16T18:07:46Z")

</div>

Hey guys! Hoping you can see what's going on with this more quickly than I  
can.

This query:

{"filter":{"and":[{"term":{"SID":"49"}},{"term":{"BI":"true"}},{"term":{"TA":"Tag  
Test"}},{"term":{"TY":"2"}},{"query":{"query\_string":{"fields":["RE","SU"],"query":""john@test.com"","default\_operator":"AND"}}}]}}

Works for \_search but not for \_count. Thoughts??

Count throws a No query registered for [filter]] error.

---

<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:** [April 16, 2012, 6:14pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/2 "2012-04-16T18:14:20Z")

</div>

Hiya

> {"filter":{"and":[{"term":{"SID":"49"}},{"term":{"BI":"true"}},{"term":{"TA":"Tag Test"}},{"term":{"TY":"2"}},{"query":{"query\_string":{"fields":["RE","SU"],"query":""john@test.com"","default\_operator":"AND"}}}]}}

Look at the docs for search  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body.html) and for count [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count.html)  
and you'll soon see the difference

clint

---

<div class="post-metadata">

**Author:** ![JP\_Toto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jp_toto/32/2512_2.png) [@JP\_Toto](https://discuss.elastic.co/u/JP_Toto)\
**Post date:** [April 16, 2012, 6:33pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/3 "2012-04-16T18:33:55Z")

</div>

Thanks, Clint! Yeah I've been checking those as best I can today. I'm a  
little new to ES so it's not jumping out at me at the moment ☹

On Monday, April 16, 2012 2:14:20 PM UTC-4, Clinton Gormley wrote:

> Hiya
> 
> > 
> 
> {"filter":{"and":[{"term":{"SID":"49"}},{"term":{"BI":"true"}},{"term":{"TA":"Tag  
> Test"}},{"term":{"TY":"2"}},{"query":{"query\_string":{"fields":["RE","SU"],"query":""  
> [john@test.com](mailto:john@test.com)"","default\_operator":"AND"}}}]}}
> 
> Look at the docs for search  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body.htmland) for count  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count.html)  
> and you'll soon see the difference
> 
> clint

---

<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:** [April 16, 2012, 6:39pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/4 "2012-04-16T18:39:57Z")

</div>

On Mon, 2012-04-16 at 11:33 -0700, JP Toto wrote:

> Thanks, Clint! Yeah I've been checking those as best I can today. I'm  
> a little new to ES so it's not jumping out at me at the moment ☹

The search API accepts 'query' and 'filter' parameters, eg:

{  
query: { match\_all: {}  
}

The count API accepts a query, eg:  
{  
match\_all: {}  
}

Inconsistent, I know... but that's how it is at the moment

clint

> On Monday, April 16, 2012 2:14:20 PM UTC-4, Clinton Gormley wrote:  
> Hiya  
> \>  
> \>  
> {"filter":{"and":[{"term":{"SID":"49"}},{"term":{"BI":"true"}},{"term":{"TA":"Tag Test"}},{"term":{"TY":"2"}},{"query":{"query\_string":{"fields":["RE","SU"],"query":""john@test.com"","default\_operator":"AND"}}}]}}
> 
> ```
> Look at the docs for search
> http://www.elasticsearch.org/guide/reference/api/search/request-body.html and for count http://www.elasticsearch.org/guide/reference/api/count.html
> and you'll soon see the difference
>     
> clint
> 
> ```

---

<div class="post-metadata">

**Author:** ![JP\_Toto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jp_toto/32/2512_2.png) [@JP\_Toto](https://discuss.elastic.co/u/JP_Toto)\
**Post date:** [April 16, 2012, 7:05pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/5 "2012-04-16T19:05:25Z")

</div>

Ohhh I see. Gotcha. I appreciate the tip. Just have to figure out how to  
rewrite this now for \_count. Thanks!

On Monday, April 16, 2012 2:39:57 PM UTC-4, Clinton Gormley wrote:

> On Mon, 2012-04-16 at 11:33 -0700, JP Toto wrote:
> 
> > Thanks, Clint! Yeah I've been checking those as best I can today. I'm  
> > a little new to ES so it's not jumping out at me at the moment ☹
> 
> The search API accepts 'query' and 'filter' parameters, eg:
> 
> {  
> query: { match\_all: {}  
> }
> 
> The count API accepts a query, eg:  
> {  
> match\_all: {}  
> }
> 
> Inconsistent, I know... but that's how it is at the moment
> 
> clint
> 
> > On Monday, April 16, 2012 2:14:20 PM UTC-4, Clinton Gormley wrote:  
> > Hiya  
> > \>  
> > \>
> 
> {"filter":{"and":[{"term":{"SID":"49"}},{"term":{"BI":"true"}},{"term":{"TA":"Tag  
> Test"}},{"term":{"TY":"2"}},{"query":{"query\_string":{"fields":["RE","SU"],"query":""  
> [john@test.com](mailto:john@test.com)"","default\_operator":"AND"}}}]}}
> 
> > ```
> > Look at the docs for search
> > 
> > ```
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body.htmland) for count  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count.html)
> 
> > ```
> > and you'll soon see the difference
> >     
> > clint
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![JP\_Toto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jp_toto/32/2512_2.png) [@JP\_Toto](https://discuss.elastic.co/u/JP_Toto)\
**Post date:** [April 16, 2012, 7:43pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/6 "2012-04-16T19:43:47Z")

</div>

Clint,

Is it even possible to filter "queries", then, for \_count?

On Monday, April 16, 2012 3:05:25 PM UTC-4, JP Toto wrote:

> Ohhh I see. Gotcha. I appreciate the tip. Just have to figure out how to  
> rewrite this now for \_count. Thanks!
> 
> On Monday, April 16, 2012 2:39:57 PM UTC-4, Clinton Gormley wrote:
> 
> > On Mon, 2012-04-16 at 11:33 -0700, JP Toto wrote:
> > 
> > > Thanks, Clint! Yeah I've been checking those as best I can today. I'm  
> > > a little new to ES so it's not jumping out at me at the moment ☹
> > 
> > The search API accepts 'query' and 'filter' parameters, eg:
> > 
> > {  
> > query: { match\_all: {}  
> > }
> > 
> > The count API accepts a query, eg:  
> > {  
> > match\_all: {}  
> > }
> > 
> > Inconsistent, I know... but that's how it is at the moment
> > 
> > clint
> > 
> > > On Monday, April 16, 2012 2:14:20 PM UTC-4, Clinton Gormley wrote:  
> > > Hiya  
> > > \>  
> > > \>
> > 
> > {"filter":{"and":[{"term":{"SID":"49"}},{"term":{"BI":"true"}},{"term":{"TA":"Tag  
> > Test"}},{"term":{"TY":"2"}},{"query":{"query\_string":{"fields":["RE","SU"],"query":""  
> > [john@test.com](mailto:john@test.com)"","default\_operator":"AND"}}}]}}
> > 
> > > ```
> > > Look at the docs for search
> > > 
> > > ```
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body.htmland) for count  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count.html)
> > 
> > > ```
> > > and you'll soon see the difference
> > >     
> > > clint
> > > 
> > > ```

---

<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:** [April 17, 2012, 9:03am UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/7 "2012-04-17T09:03:07Z")

</div>

On Mon, 2012-04-16 at 12:43 -0700, JP Toto wrote:

> Clint,
> 
> Is it even possible to filter "queries", then, for \_count?

Yes, that's what the filtered query

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

and the constant score query

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

are used for.

The 'filter' param on the search API is used for filtering results AFTER  
facets have been calculated. For other uses of filters, it is better to  
use the filtered or constant score queries instead.

clint

> On Monday, April 16, 2012 3:05:25 PM UTC-4, JP Toto wrote:  
> Ohhh I see. Gotcha. I appreciate the tip. Just have to figure  
> out how to rewrite this now for \_count. Thanks!
> 
> ```
> On Monday, April 16, 2012 2:39:57 PM UTC-4, Clinton Gormley
> wrote:
> On Mon, 2012-04-16 at 11:33 -0700, JP Toto wrote:
> > Thanks, Clint! Yeah I've been checking those as best
> I can today. I'm
> > a little new to ES so it's not jumping out at me at
> the moment :-(
>             
> The search API accepts 'query' and 'filter'
> parameters, eg:
>             
> {
> query: { match_all: {}
> }
>             
> The count API accepts a query, eg:
> {
> match_all: {}
> }
>             
> Inconsistent, I know... but that's how it is at the
> moment
>             
> clint
> > 
> > On Monday, April 16, 2012 2:14:20 PM UTC-4, Clinton
> Gormley wrote:
> > Hiya
> > > 
> > >
> >
> {"filter":{"and":[{"term":{"SID":"49"}},{"term":{"BI":"true"}},{"term":{"TA":"Tag Test"}},{"term":{"TY":"2"}},{"query":{"query_string":{"fields":["RE","SU"],"query":"\"john@test.com\"","default_operator":"AND"}}}]}}
> >         
> > Look at the docs for search
> >
> http://www.elasticsearch.org/guide/reference/api/search/request-body.html and for count http://www.elasticsearch.org/guide/reference/api/count.html
> > and you'll soon see the difference
> >         
> > clint
> >         
> 
> ```

---

<div class="post-metadata">

**Author:** ![JP\_Toto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jp_toto/32/2512_2.png) [@JP\_Toto](https://discuss.elastic.co/u/JP_Toto)\
**Post date:** [April 17, 2012, 7:24pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/8 "2012-04-17T19:24:20Z")

</div>

Thanks so much, Clint! Appreciate your patience 🙂

On Tuesday, April 17, 2012 5:03:07 AM UTC-4, Clinton Gormley wrote:

> On Mon, 2012-04-16 at 12:43 -0700, JP Toto wrote:
> 
> > Clint,
> > 
> > Is it even possible to filter "queries", then, for \_count?
> 
> Yes, that's what the filtered query  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)  
> and the constant score query
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html)  
> are used for.
> 
> The 'filter' param on the search API is used for filtering results AFTER  
> facets have been calculated. For other uses of filters, it is better to  
> use the filtered or constant score queries instead.
> 
> clint
> 
> > On Monday, April 16, 2012 3:05:25 PM UTC-4, JP Toto wrote:  
> > Ohhh I see. Gotcha. I appreciate the tip. Just have to figure  
> > out how to rewrite this now for \_count. Thanks!
> > 
> > ```
> > On Monday, April 16, 2012 2:39:57 PM UTC-4, Clinton Gormley
> > wrote:
> > On Mon, 2012-04-16 at 11:33 -0700, JP Toto wrote:
> > > Thanks, Clint! Yeah I've been checking those as best
> > I can today. I'm
> > > a little new to ES so it's not jumping out at me at
> > the moment :-(
> >             
> > The search API accepts 'query' and 'filter'
> > parameters, eg:
> >             
> > {
> > query: { match_all: {}
> > }
> >             
> > The count API accepts a query, eg:
> > {
> > match_all: {}
> > }
> >             
> > Inconsistent, I know... but that's how it is at the
> > moment
> >             
> > clint
> > > 
> > > On Monday, April 16, 2012 2:14:20 PM UTC-4, Clinton
> > Gormley wrote:
> > > Hiya
> > > > 
> > > >
> > >
> > 
> > ```
> 
> {"filter":{"and":[{"term":{"SID":"49"}},{"term":{"BI":"true"}},{"term":{"TA":"Tag  
> Test"}},{"term":{"TY":"2"}},{"query":{"query\_string":{"fields":["RE","SU"],"query":""  
> [john@test.com](mailto:john@test.com)"","default\_operator":"AND"}}}]}}
> 
> > ```
> > >         
> > > Look at the docs for search
> > >
> > 
> > ```
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body.htmland) for count  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/count.html)
> 
> > ```
> > > and you'll soon see the difference
> > >         
> > > clint
> > >         
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 19, 2012, 1:35pm UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/9 "2012-04-19T13:35:02Z")

</div>

One more thing, you can use search\_type set to count in the search request  
URI, and execute regular search requests and get back just the count.  
Effectively, it will be as performant as the count API.

On Tue, Apr 17, 2012 at 10:24 PM, JP Toto [james.p.toto@gmail.com](mailto:james.p.toto@gmail.com) wrote:

> Thanks so much, Clint! Appreciate your patience 🙂
> 
> On Tuesday, April 17, 2012 5:03:07 AM UTC-4, Clinton Gormley wrote:
> 
> > On Mon, 2012-04-16 at 12:43 -0700, JP Toto wrote:
> > 
> > > Clint,
> > > 
> > > Is it even possible to filter "queries", then, for \_count?
> > 
> > Yes, that's what the filtered query  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**guide/reference/query-dsl/)\*\*  
> > filtered-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)  
> > and the constant score query  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**guide/reference/query-dsl/)\*\*  
> > constant-score-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html)  
> > are used for.
> > 
> > The 'filter' param on the search API is used for filtering results AFTER  
> > facets have been calculated. For other uses of filters, it is better to  
> > use the filtered or constant score queries instead.
> > 
> > clint
> > 
> > > On Monday, April 16, 2012 3:05:25 PM UTC-4, JP Toto wrote:  
> > > Ohhh I see. Gotcha. I appreciate the tip. Just have to figure  
> > > out how to rewrite this now for \_count. Thanks!
> > > 
> > > ```
> > > On Monday, April 16, 2012 2:39:57 PM UTC-4, Clinton Gormley
> > > wrote:
> > > On Mon, 2012-04-16 at 11:33 -0700, JP Toto wrote:
> > > > Thanks, Clint! Yeah I've been checking those as best
> > > I can today. I'm
> > > > a little new to ES so it's not jumping out at me at
> > > the moment :-(
> > > 
> > > The search API accepts 'query' and 'filter'
> > > parameters, eg:
> > > 
> > > {
> > > query: { match_all: {}
> > > }
> > > 
> > > The count API accepts a query, eg:
> > > {
> > > match_all: {}
> > > }
> > > 
> > > Inconsistent, I know... but that's how it is at the
> > > moment
> > > 
> > > clint
> > > >
> > > > On Monday, April 16, 2012 2:14:20 PM UTC-4, Clinton
> > > Gormley wrote:
> > > > Hiya
> > > > >
> > > > >
> > > >
> > > {"filter":{"and":[{"term":{"**
> > > 
> > > ```
> > 
> > SID":"49"}},{"term":{"BI":"**true"}},{"term":{"TA":"Tag  
> > Test"}},{"term":{"TY":"2"}},{"query":{"query\_string":{"  
> > fields":["RE","SU"],"query":"\*\*"[john@test.com](mailto:john@test.com)"","default\_**  
> > operator":"AND"}}}]}}
> > 
> > > ```
> > > >
> > > > Look at the docs for search
> > > >
> > > http://www.elasticsearch.org/**
> > > 
> > > ```
> > 
> > guide/reference/api/search/\*\*request-body.html[http://www.elasticsearch.org/guide/reference/api/search/request-body.html](http://www.elasticsearch.org/guide/reference/api/search/request-body.html)and for count  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**guide/reference/api/count.html)[http://www.elasticsearch.org/guide/reference/api/count.html](http://www.elasticsearch.org/guide/reference/api/count.html)
> > 
> > > ```
> > > > and you'll soon see the difference
> > > >
> > > > clint
> > > >
> > > 
> > > ```

---

<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, 3:31am UTC](https://discuss.elastic.co/t/query-on--search-not-working-for--count/7357/10 "2017-07-06T03:31:59Z")

</div>


