# Filters vs Queries

**URL:** <https://discuss.elastic.co/t/filters-vs-queries/5032>\
**Category:** Elasticsearch\
**Created:** [August 2, 2011, 5:07pm UTC](https://discuss.elastic.co/t/filters-vs-queries/5032 "2011-08-02T17:07:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [August 2, 2011, 5:07pm UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/1 "2011-08-02T17:07:02Z")

</div>

Hi,

I keep hearing that I should be using filters rather than queries  
because they are much quicker but I'm not sure how I should be using  
them. Up to now I have been using QueryStringQueryBuilder to  
generate Lucene syntax string queries.

Here are my questions:

i) If I do a sort should I always be using filters because the sort  
removes the relevance ranking and so makes the only advantage of  
queries (i.e. relevance score) useless.

ii) Do filters have to be on a basic query to start with?

e.g. basic query ( search term=\_all:smith) + filters (listId="52",  
firstName=smith, lastName=smith, companyName=smith)

If ii) is true, what should be my basic Query - should it be a search  
on all fields, as above, that is then filtered by fields or should I  
also specify the fields in the query?

Best Regards,

David.

---

<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:** [August 2, 2011, 5:36pm UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/2 "2011-08-02T17:36:26Z")

</div>

Hi David

> I keep hearing that I should be using filters rather than queries  
> because they are much quicker

and because their results can be cached. Depending on the filter type,  
some are cached by default, some are not (see Filter Caching on  
[Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/) )

> but I'm not sure how I should be using  
> them. Up to now I have been using QueryStringQueryBuilder to  
> generate Lucene syntax string queries.

You can't use filters on a query string search. You have to use a  
request body search:

> **[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 ...

> i) If I do a sort should I always be using filters because the sort  
> removes the relevance ranking and so makes the only advantage of  
> queries (i.e. relevance score) useless.

If you are not sorting on \_score, then yes, rather use filters.

> ii) Do filters have to be on a basic query to start with?
> 
> e.g. basic query ( search term=\_all:smith) + filters (listId="52",  
> firstName=smith, lastName=smith, companyName=smith)
> 
> If ii) is true, what should be my basic Query - should it be a search  
> on all fields, as above, that is then filtered by fields or should I  
> also specify the fields in the query?

Here are 3 variations:

- Query only:

- Filter only:

- Query and Filter:

## So:

1. You always need wrap your query in a top-level query element
2. A "constant\_score" query says "all docs are equal", so no scoring  
has to happen - just the filter gets applied
3. In the third example, filter reduces the number of docs that  
can be matched (and scored) by the query

There is also a top-level filter argument:

{  
query: { text: { \_all: "foo bar" }},  
filter: { term: { status: "open" }}  
}

For normal usage, you should NOT use this version. It's purpose is  
different from the "filtered" query mentioned above.

This is intended only to be used when you want to:

- run a query
- filter the results
- BUT show facets on the UNFILTERED results

So this filter will be less efficient than the "filtered" query.

clint

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [August 3, 2011, 9:28am UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/3 "2011-08-03T09:28:29Z")

</div>

thanks clint, that's a brilliant explanation.

On Aug 2, 6:36 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> Hi David
> 
> > I keep hearing that I should be using filters rather than queries  
> > because they are much quicker
> 
> and because their results can be cached. Depending on the filter type,  
> some are cached by default, some are not (see Filter Caching onhttp://www.elasticsearch.org/guide/reference/query-dsl/)
> 
> > but I'm not sure how I should be using  
> > them. Up to now I have been using QueryStringQueryBuilder to  
> > generate Lucene syntax string queries.
> 
> You can't use filters on a query string search. You have to use a  
> request body search:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body)....
> 
> > i) If I do a sort should I always be using filters because the sort  
> > removes the relevance ranking and so makes the only advantage of  
> > queries (i.e. relevance score) useless.
> 
> If you are not sorting on \_score, then yes, rather use filters.
> 
> > ii) Do filters have to be on a basic query to start with?
> 
> > e.g. basic query ( search term=\_all:smith) + filters (listId="52",  
> > firstName=smith, lastName=smith, companyName=smith)
> 
> > If ii) is true, what should be my basic Query - should it be a search  
> > on all fields, as above, that is then filtered by fields or should I  
> > also specify the fields in the query?
> 
> Here are 3 variations:
> 
> - Query only:
> 
> - Filter only:
> 
> - Query and Filter:
> 
> ## So:
> 
> 1. You always need wrap your query in a top-level query element
> 2. A "constant\_score" query says "all docs are equal", so no scoring  
> has to happen - just the filter gets applied
> 3. In the third example, filter reduces the number of docs that  
> can be matched (and scored) by the query
> 
> There is also a top-level filter argument:
> 
> {  
> query: { text: { \_all: "foo bar" }},  
> filter: { term: { status: "open" }}
> 
> }
> 
> For normal usage, you should NOT use this version. It's purpose is  
> different from the "filtered" query mentioned above.
> 
> This is intended only to be used when you want to:
> 
> - run a query
> - filter the results
> - BUT show facets on the UNFILTERED results
> 
> So this filter will be less efficient than the "filtered" query.
> 
> clint

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [August 3, 2011, 11:09am UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/4 "2011-08-03T11:09:03Z")

</div>

Is it possible to use a filter to search for an exact value on a field  
as opposed to a simple token match? I used this alot to make joins to  
other entities' ids that are not in the index.

e.g. Many contacts are in many lists, means that for contacts I migh  
have data like this

Contact 1: {"listIds" : 2,3,4,100,125,325}  
Contact 2: { "listIds" : 200,325}

to get all the contacts in list 325, In Lucene syntax I would makle a  
query like this: (listIds:"325")

On Aug 3, 10:28 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:

> thanks clint, that's a brilliant explanation.
> 
> On Aug 2, 6:36 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> 
> > Hi David
> 
> > > I keep hearing that I should be using filters rather than queries  
> > > because they are much quicker
> 
> > and because their results can be cached. Depending on the filter type,  
> > some are cached by default, some are not (see Filter Caching onhttp://www.elasticsearch.org/guide/reference/query-dsl/)
> 
> > > but I'm not sure how I should be using  
> > > them. Up to now I have been using QueryStringQueryBuilder to  
> > > generate Lucene syntax string queries.
> 
> > You can't use filters on a query string search. You have to use a  
> > request body search:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body)....
> 
> > > i) If I do a sort should I always be using filters because the sort  
> > > removes the relevance ranking and so makes the only advantage of  
> > > queries (i.e. relevance score) useless.
> 
> > If you are not sorting on \_score, then yes, rather use filters.
> 
> > > ii) Do filters have to be on a basic query to start with?
> 
> > > e.g. basic query ( search term=\_all:smith) + filters (listId="52",  
> > > firstName=smith, lastName=smith, companyName=smith)
> 
> > > If ii) is true, what should be my basic Query - should it be a search  
> > > on all fields, as above, that is then filtered by fields or should I  
> > > also specify the fields in the query?
> 
> > Here are 3 variations:
> 
> > - Query only:
> 
> > { query: { text: { \_all: "foo bar" }}}
> 
> > - Filter only:
> 
> > { query: {  
> > constant\_score: {  
> > filter: { term: { status: "open" }}  
> > }  
> > }}
> 
> > - Query and Filter:
> 
> > { query: {  
> > filtered: {  
> > query: { text: { \_all: "foo bar"}}  
> > filter: { term: { status: "open" }}  
> > }  
> > }}
> 
> > ## So:
> > 
> > 1. You always need wrap your query in a top-level query element
> > 2. A "constant\_score" query says "all docs are equal", so no scoring  
> > has to happen - just the filter gets applied
> > 3. In the third example, filter reduces the number of docs that  
> > can be matched (and scored) by the query
> 
> > There is also a top-level filter argument:
> 
> > {  
> > query: { text: { \_all: "foo bar" }},  
> > filter: { term: { status: "open" }}
> 
> > }
> 
> > For normal usage, you should NOT use this version. It's purpose is  
> > different from the "filtered" query mentioned above.
> 
> > This is intended only to be used when you want to:
> > 
> > - run a query
> > - filter the results
> > - BUT show facets on the UNFILTERED results
> 
> > So this filter will be less efficient than the "filtered" query.
> 
> > 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:** [August 3, 2011, 11:30am UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/5 "2011-08-03T11:30:54Z")

</div>

You can have a term filter on listIds with a value of 325.

On Wed, Aug 3, 2011 at 2:09 PM, davrob2 [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:

> Is it possible to use a filter to search for an exact value on a field  
> as opposed to a simple token match? I used this alot to make joins to  
> other entities' ids that are not in the index.
> 
> e.g. Many contacts are in many lists, means that for contacts I migh  
> have data like this
> 
> Contact 1: {"listIds" : 2,3,4,100,125,325}  
> Contact 2: { "listIds" : 200,325}
> 
> to get all the contacts in list 325, In Lucene syntax I would makle a  
> query like this: (listIds:"325")
> 
> On Aug 3, 10:28 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> 
> > thanks clint, that's a brilliant explanation.
> > 
> > On Aug 2, 6:36 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> > 
> > > Hi David
> > 
> > > > I keep hearing that I should be using filters rather than queries  
> > > > because they are much quicker
> > 
> > > and because their results can be cached. Depending on the filter type,  
> > > some are cached by default, some are not (see Filter Caching onhttp://  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/))
> > 
> > > > but I'm not sure how I should be using  
> > > > them. Up to now I have been using QueryStringQueryBuilder to  
> > > > generate Lucene syntax string queries.
> > 
> > > You can't use filters on a query string search. You have to use a  
> > > request body search:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body)....
> > 
> > > > i) If I do a sort should I always be using filters because the sort  
> > > > removes the relevance ranking and so makes the only advantage of  
> > > > queries (i.e. relevance score) useless.
> > 
> > > If you are not sorting on \_score, then yes, rather use filters.
> > 
> > > > ii) Do filters have to be on a basic query to start with?
> > 
> > > > e.g. basic query ( search term=\_all:smith) + filters (listId="52",  
> > > > firstName=smith, lastName=smith, companyName=smith)
> > 
> > > > If ii) is true, what should be my basic Query - should it be a search  
> > > > on all fields, as above, that is then filtered by fields or should I  
> > > > also specify the fields in the query?
> > 
> > > Here are 3 variations:
> > 
> > > - Query only:
> > 
> > > { query: { text: { \_all: "foo bar" }}}
> > 
> > > - Filter only:
> > 
> > > { query: {  
> > > constant\_score: {  
> > > filter: { term: { status: "open" }}  
> > > }  
> > > }}
> > 
> > > - Query and Filter:
> > 
> > > { query: {  
> > > filtered: {  
> > > query: { text: { \_all: "foo bar"}}  
> > > filter: { term: { status: "open" }}  
> > > }  
> > > }}
> > 
> > > ## So:
> > > 
> > > 1. You always need wrap your query in a top-level query element
> > > 2. A "constant\_score" query says "all docs are equal", so no scoring  
> > > has to happen - just the filter gets applied
> > > 3. In the third example, filter reduces the number of docs that  
> > > can be matched (and scored) by the query
> > 
> > > There is also a top-level filter argument:
> > 
> > > {  
> > > query: { text: { \_all: "foo bar" }},  
> > > filter: { term: { status: "open" }}
> > 
> > > }
> > 
> > > For normal usage, you should NOT use this version. It's purpose is  
> > > different from the "filtered" query mentioned above.
> > 
> > > This is intended only to be used when you want to:
> > > 
> > > - run a query
> > > - filter the results
> > > - BUT show facets on the UNFILTERED results
> > 
> > > So this filter will be less efficient than the "filtered" query.
> > 
> > > clint

---

<div class="post-metadata">

**Author:** ![Jason\_2](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Jason\_2](https://discuss.elastic.co/u/Jason_2)\
**Post date:** [August 3, 2011, 12:06pm UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/6 "2011-08-03T12:06:22Z")

</div>

Is there a performance difference between using a constant scored  
query and a filtered query? All of our current queries are generated  
as filtered queries and I'm concerned that this might be slower than  
the constant score method. Examples:

- Constant Score:  
{ query: {  
constant\_score: {  
filter: { term: { status: "open" }}  
}  
}}

- Filtered Query  
{ query: {  
filtered: {  
query: { match\_all: {} } ,  
filter: { term: { status: "open" }}  
}  
}}

On Aug 3, 7:30 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> You can have a term filter on listIds with a value of 325.
> 
> On Wed, Aug 3, 2011 at 2:09 PM, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> 
> > Is it possible to use a filter to search for an exact value on a field  
> > as opposed to a simple token match? I used this alot to make joins to  
> > other entities' ids that are not in the index.
> 
> > e.g. Many contacts are in many lists, means that for contacts I migh  
> > have data like this
> 
> > Contact 1: {"listIds" : 2,3,4,100,125,325}  
> > Contact 2: { "listIds" : 200,325}
> 
> > to get all the contacts in list 325, In Lucene syntax I would makle a  
> > query like this: (listIds:"325")
> 
> > On Aug 3, 10:28 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > 
> > > thanks clint, that's a brilliant explanation.
> 
> > > On Aug 2, 6:36 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> 
> > > > Hi David
> 
> > > > > I keep hearing that I should be using filters rather than queries  
> > > > > because they are much quicker
> 
> > > > and because their results can be cached. Depending on the filter type,  
> > > > some are cached by default, some are not (see Filter Caching onhttp://  
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/))
> 
> > > > > but I'm not sure how I should be using  
> > > > > them. Up to now I have been using QueryStringQueryBuilder to  
> > > > > generate Lucene syntax string queries.
> 
> > > > You can't use filters on a query string search. You have to use a  
> > > > request body search:  
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body)....
> 
> > > > > i) If I do a sort should I always be using filters because the sort  
> > > > > removes the relevance ranking and so makes the only advantage of  
> > > > > queries (i.e. relevance score) useless.
> 
> > > > If you are not sorting on \_score, then yes, rather use filters.
> 
> > > > > ii) Do filters have to be on a basic query to start with?
> 
> > > > > e.g. basic query ( search term=\_all:smith) + filters (listId="52",  
> > > > > firstName=smith, lastName=smith, companyName=smith)
> 
> > > > > If ii) is true, what should be my basic Query - should it be a search  
> > > > > on all fields, as above, that is then filtered by fields or should I  
> > > > > also specify the fields in the query?
> 
> > > > Here are 3 variations:
> 
> > > > - Query only:
> 
> > > > { query: { text: { \_all: "foo bar" }}}
> 
> > > > - Filter only:
> 
> > > > { query: {  
> > > > constant\_score: {  
> > > > filter: { term: { status: "open" }}  
> > > > }  
> > > > }}
> 
> > > > - Query and Filter:
> 
> > > > { query: {  
> > > > filtered: {  
> > > > query: { text: { \_all: "foo bar"}}  
> > > > filter: { term: { status: "open" }}  
> > > > }  
> > > > }}
> 
> > > > ## So:
> > > > 
> > > > 1. You always need wrap your query in a top-level query element
> > > > 2. A "constant\_score" query says "all docs are equal", so no scoring  
> > > > has to happen - just the filter gets applied
> > > > 3. In the third example, filter reduces the number of docs that  
> > > > can be matched (and scored) by the query
> 
> > > > There is also a top-level filter argument:
> 
> > > > {  
> > > > query: { text: { \_all: "foo bar" }},  
> > > > filter: { term: { status: "open" }}
> 
> > > > }
> 
> > > > For normal usage, you should NOT use this version. It's purpose is  
> > > > different from the "filtered" query mentioned above.
> 
> > > > This is intended only to be used when you want to:
> > > > 
> > > > - run a query
> > > > - filter the results
> > > > - BUT show facets on the UNFILTERED results
> 
> > > > So this filter will be less efficient than the "filtered" query.
> 
> > > > clint

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [August 3, 2011, 12:21pm UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/7 "2011-08-03T12:21:27Z")

</div>

thanks Shay,

So what Filter should I used if I want a normal tokenized match? Or  
are we then in realm of having to create a query first and then filter  
it?

On Aug 3, 12:30 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> You can have a term filter on listIds with a value of 325.
> 
> On Wed, Aug 3, 2011 at 2:09 PM, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> 
> > Is it possible to use a filter to search for an exact value on a field  
> > as opposed to a simple token match? I used this alot to make joins to  
> > other entities' ids that are not in the index.
> 
> > e.g. Many contacts are in many lists, means that for contacts I migh  
> > have data like this
> 
> > Contact 1: {"listIds" : 2,3,4,100,125,325}  
> > Contact 2: { "listIds" : 200,325}
> 
> > to get all the contacts in list 325, In Lucene syntax I would makle a  
> > query like this: (listIds:"325")
> 
> > On Aug 3, 10:28 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > 
> > > thanks clint, that's a brilliant explanation.
> 
> > > On Aug 2, 6:36 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> 
> > > > Hi David
> 
> > > > > I keep hearing that I should be using filters rather than queries  
> > > > > because they are much quicker
> 
> > > > and because their results can be cached. Depending on the filter type,  
> > > > some are cached by default, some are not (see Filter Caching onhttp://  
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/))
> 
> > > > > but I'm not sure how I should be using  
> > > > > them. Up to now I have been using QueryStringQueryBuilder to  
> > > > > generate Lucene syntax string queries.
> 
> > > > You can't use filters on a query string search. You have to use a  
> > > > request body search:  
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body)....
> 
> > > > > i) If I do a sort should I always be using filters because the sort  
> > > > > removes the relevance ranking and so makes the only advantage of  
> > > > > queries (i.e. relevance score) useless.
> 
> > > > If you are not sorting on \_score, then yes, rather use filters.
> 
> > > > > ii) Do filters have to be on a basic query to start with?
> 
> > > > > e.g. basic query ( search term=\_all:smith) + filters (listId="52",  
> > > > > firstName=smith, lastName=smith, companyName=smith)
> 
> > > > > If ii) is true, what should be my basic Query - should it be a search  
> > > > > on all fields, as above, that is then filtered by fields or should I  
> > > > > also specify the fields in the query?
> 
> > > > Here are 3 variations:
> 
> > > > - Query only:
> 
> > > > { query: { text: { \_all: "foo bar" }}}
> 
> > > > - Filter only:
> 
> > > > { query: {  
> > > > constant\_score: {  
> > > > filter: { term: { status: "open" }}  
> > > > }  
> > > > }}
> 
> > > > - Query and Filter:
> 
> > > > { query: {  
> > > > filtered: {  
> > > > query: { text: { \_all: "foo bar"}}  
> > > > filter: { term: { status: "open" }}  
> > > > }  
> > > > }}
> 
> > > > ## So:
> > > > 
> > > > 1. You always need wrap your query in a top-level query element
> > > > 2. A "constant\_score" query says "all docs are equal", so no scoring  
> > > > has to happen - just the filter gets applied
> > > > 3. In the third example, filter reduces the number of docs that  
> > > > can be matched (and scored) by the query
> 
> > > > There is also a top-level filter argument:
> 
> > > > {  
> > > > query: { text: { \_all: "foo bar" }},  
> > > > filter: { term: { status: "open" }}
> 
> > > > }
> 
> > > > For normal usage, you should NOT use this version. It's purpose is  
> > > > different from the "filtered" query mentioned above.
> 
> > > > This is intended only to be used when you want to:
> > > > 
> > > > - run a query
> > > > - filter the results
> > > > - BUT show facets on the UNFILTERED results
> 
> > > > So this filter will be less efficient than the "filtered" query.
> 
> > > > 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:** [August 4, 2011, 5:54pm UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/8 "2011-08-04T17:54:42Z")

</div>

A filtered query with a match\_all query is automatically converted  
internally to a constant\_score one, so guess which one is better? 🙂

On Wed, Aug 3, 2011 at 3:06 PM, Jason [jason@element84.com](mailto:jason@element84.com) wrote:

> Is there a performance difference between using a constant scored  
> query and a filtered query? All of our current queries are generated  
> as filtered queries and I'm concerned that this might be slower than  
> the constant score method. Examples:
> 
> - Constant Score:  
> { query: {  
> constant\_score: {  
> filter: { term: { status: "open" }}  
> }  
> }}
> 
> - Filtered Query  
> { query: {  
> filtered: {  
> query: { match\_all: {} } ,  
> filter: { term: { status: "open" }}  
> }  
> }}
> 
> On Aug 3, 7:30 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > You can have a term filter on listIds with a value of 325.
> > 
> > On Wed, Aug 3, 2011 at 2:09 PM, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > 
> > > Is it possible to use a filter to search for an exact value on a field  
> > > as opposed to a simple token match? I used this alot to make joins to  
> > > other entities' ids that are not in the index.
> > 
> > > e.g. Many contacts are in many lists, means that for contacts I migh  
> > > have data like this
> > 
> > > Contact 1: {"listIds" : 2,3,4,100,125,325}  
> > > Contact 2: { "listIds" : 200,325}
> > 
> > > to get all the contacts in list 325, In Lucene syntax I would makle a  
> > > query like this: (listIds:"325")
> > 
> > > On Aug 3, 10:28 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > > 
> > > > thanks clint, that's a brilliant explanation.
> > 
> > > > On Aug 2, 6:36 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> > 
> > > > > Hi David
> > 
> > > > > > I keep hearing that I should be using filters rather than queries  
> > > > > > because they are much quicker
> > 
> > > > > and because their results can be cached. Depending on the filter  
> > > > > type,  
> > > > > some are cached by default, some are not (see Filter Caching  
> > > > > onhttp://  
> > > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/))
> > 
> > > > > > but I'm not sure how I should be using  
> > > > > > them. Up to now I have been using QueryStringQueryBuilder to  
> > > > > > generate Lucene syntax string queries.
> > 
> > > > > You can't use filters on a query string search. You have to use a  
> > > > > request body search:  
> > > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body)..  
> > > > > ..
> > 
> > > > > > i) If I do a sort should I always be using filters because the  
> > > > > > sort  
> > > > > > removes the relevance ranking and so makes the only advantage of  
> > > > > > queries (i.e. relevance score) useless.
> > 
> > > > > If you are not sorting on \_score, then yes, rather use filters.
> > 
> > > > > > ii) Do filters have to be on a basic query to start with?
> > 
> > > > > > e.g. basic query ( search term=\_all:smith) + filters  
> > > > > > (listId="52",  
> > > > > > firstName=smith, lastName=smith, companyName=smith)
> > 
> > > > > > If ii) is true, what should be my basic Query - should it be a  
> > > > > > search  
> > > > > > on all fields, as above, that is then filtered by fields or  
> > > > > > should I  
> > > > > > also specify the fields in the query?
> > 
> > > > > Here are 3 variations:
> > 
> > > > > - Query only:
> > 
> > > > > { query: { text: { \_all: "foo bar" }}}
> > 
> > > > > - Filter only:
> > 
> > > > > { query: {  
> > > > > constant\_score: {  
> > > > > filter: { term: { status: "open" }}  
> > > > > }  
> > > > > }}
> > 
> > > > > - Query and Filter:
> > 
> > > > > { query: {  
> > > > > filtered: {  
> > > > > query: { text: { \_all: "foo bar"}}  
> > > > > filter: { term: { status: "open" }}  
> > > > > }  
> > > > > }}
> > 
> > > > > ## So:
> > > > > 
> > > > > 1. You always need wrap your query in a top-level query element
> > > > > 2. A "constant\_score" query says "all docs are equal", so no  
> > > > > scoring  
> > > > > has to happen - just the filter gets applied
> > > > > 3. In the third example, filter reduces the number of docs that  
> > > > > can be matched (and scored) by the query
> > 
> > > > > There is also a top-level filter argument:
> > 
> > > > > {  
> > > > > query: { text: { \_all: "foo bar" }},  
> > > > > filter: { term: { status: "open" }}
> > 
> > > > > }
> > 
> > > > > For normal usage, you should NOT use this version. It's purpose is  
> > > > > different from the "filtered" query mentioned above.
> > 
> > > > > This is intended only to be used when you want to:
> > > > > 
> > > > > - run a query
> > > > > - filter the results
> > > > > - BUT show facets on the UNFILTERED results
> > 
> > > > > So this filter will be less efficient than the "filtered" query.
> > 
> > > > > 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:** [August 4, 2011, 5:56pm UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/9 "2011-08-04T17:56:55Z")

</div>

Yea, if you really want to use a filter from a query, then you can wrap the  
filter with a  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html),  
or, just provide the query to a constant score query.

On Wed, Aug 3, 2011 at 3:21 PM, davrob2 [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:

> thanks Shay,
> 
> So what Filter should I used if I want a normal tokenized match? Or  
> are we then in realm of having to create a query first and then filter  
> it?
> 
> On Aug 3, 12:30 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > You can have a term filter on listIds with a value of 325.
> > 
> > On Wed, Aug 3, 2011 at 2:09 PM, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > 
> > > Is it possible to use a filter to search for an exact value on a field  
> > > as opposed to a simple token match? I used this alot to make joins to  
> > > other entities' ids that are not in the index.
> > 
> > > e.g. Many contacts are in many lists, means that for contacts I migh  
> > > have data like this
> > 
> > > Contact 1: {"listIds" : 2,3,4,100,125,325}  
> > > Contact 2: { "listIds" : 200,325}
> > 
> > > to get all the contacts in list 325, In Lucene syntax I would makle a  
> > > query like this: (listIds:"325")
> > 
> > > On Aug 3, 10:28 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:
> > > 
> > > > thanks clint, that's a brilliant explanation.
> > 
> > > > On Aug 2, 6:36 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> > 
> > > > > Hi David
> > 
> > > > > > I keep hearing that I should be using filters rather than queries  
> > > > > > because they are much quicker
> > 
> > > > > and because their results can be cached. Depending on the filter  
> > > > > type,  
> > > > > some are cached by default, some are not (see Filter Caching  
> > > > > onhttp://  
> > > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/))
> > 
> > > > > > but I'm not sure how I should be using  
> > > > > > them. Up to now I have been using QueryStringQueryBuilder to  
> > > > > > generate Lucene syntax string queries.
> > 
> > > > > You can't use filters on a query string search. You have to use a  
> > > > > request body search:  
> > > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/request-body)..  
> > > > > ..
> > 
> > > > > > i) If I do a sort should I always be using filters because the  
> > > > > > sort  
> > > > > > removes the relevance ranking and so makes the only advantage of  
> > > > > > queries (i.e. relevance score) useless.
> > 
> > > > > If you are not sorting on \_score, then yes, rather use filters.
> > 
> > > > > > ii) Do filters have to be on a basic query to start with?
> > 
> > > > > > e.g. basic query ( search term=\_all:smith) + filters  
> > > > > > (listId="52",  
> > > > > > firstName=smith, lastName=smith, companyName=smith)
> > 
> > > > > > If ii) is true, what should be my basic Query - should it be a  
> > > > > > search  
> > > > > > on all fields, as above, that is then filtered by fields or  
> > > > > > should I  
> > > > > > also specify the fields in the query?
> > 
> > > > > Here are 3 variations:
> > 
> > > > > - Query only:
> > 
> > > > > { query: { text: { \_all: "foo bar" }}}
> > 
> > > > > - Filter only:
> > 
> > > > > { query: {  
> > > > > constant\_score: {  
> > > > > filter: { term: { status: "open" }}  
> > > > > }  
> > > > > }}
> > 
> > > > > - Query and Filter:
> > 
> > > > > { query: {  
> > > > > filtered: {  
> > > > > query: { text: { \_all: "foo bar"}}  
> > > > > filter: { term: { status: "open" }}  
> > > > > }  
> > > > > }}
> > 
> > > > > ## So:
> > > > > 
> > > > > 1. You always need wrap your query in a top-level query element
> > > > > 2. A "constant\_score" query says "all docs are equal", so no  
> > > > > scoring  
> > > > > has to happen - just the filter gets applied
> > > > > 3. In the third example, filter reduces the number of docs that  
> > > > > can be matched (and scored) by the query
> > 
> > > > > There is also a top-level filter argument:
> > 
> > > > > {  
> > > > > query: { text: { \_all: "foo bar" }},  
> > > > > filter: { term: { status: "open" }}
> > 
> > > > > }
> > 
> > > > > For normal usage, you should NOT use this version. It's purpose is  
> > > > > different from the "filtered" query mentioned above.
> > 
> > > > > This is intended only to be used when you want to:
> > > > > 
> > > > > - run a query
> > > > > - filter the results
> > > > > - BUT show facets on the UNFILTERED results
> > 
> > > > > So this filter will be less efficient than the "filtered" query.
> > 
> > > > > 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:58am UTC](https://discuss.elastic.co/t/filters-vs-queries/5032/10 "2017-07-06T03:58:16Z")

</div>


