# Filters vs. Queries (ES Version 1.4+)

**URL:** <https://discuss.elastic.co/t/filters-vs-queries-es-version-1-4/21614>\
**Category:** Elasticsearch\
**Created:** [January 13, 2015, 4:16pm UTC](https://discuss.elastic.co/t/filters-vs-queries-es-version-1-4/21614 "2015-01-13T16:16:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aken](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@aken](https://discuss.elastic.co/u/aken)\
**Post date:** [January 13, 2015, 4:16pm UTC](https://discuss.elastic.co/t/filters-vs-queries-es-version-1-4/21614/1 "2015-01-13T16:16:11Z")

</div>

I have a question regarding filters and queries.

I understand that filters are used in conjunction with routing and aliases,  
but it also seems that they can often be used interchangeably with queries.

Is there a technical difference, e.g. with regard to caching mechanisms, to  
how they are implemented?

Regards, Andrew

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/15ad0953-04fd-4813-a1c8-131b16bd7977%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/15ad0953-04fd-4813-a1c8-131b16bd7977%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [January 13, 2015, 5:25pm UTC](https://discuss.elastic.co/t/filters-vs-queries-es-version-1-4/21614/2 "2015-01-13T17:25:10Z")

</div>

Hi Andrew,

Queries and filter are similar in terms of their ability to match  
documents. However, filters do not perform any scoring and can be cached.  
As a consequence, you should use filters instead of queries when you do not  
care about the produced scores.

On Tue, Jan 13, 2015 at 5:16 PM, AndrewK [kenworthyas@gmail.com](mailto:kenworthyas@gmail.com) wrote:

> I have a question regarding filters and queries.
> 
> I understand that filters are used in conjunction with routing and  
> aliases, but it also seems that they can often be used interchangeably with  
> queries.
> 
> Is there a technical difference, e.g. with regard to caching mechanisms,  
> to how they are implemented?
> 
> Regards, Andrew
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/15ad0953-04fd-4813-a1c8-131b16bd7977%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/15ad0953-04fd-4813-a1c8-131b16bd7977%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/15ad0953-04fd-4813-a1c8-131b16bd7977%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/15ad0953-04fd-4813-a1c8-131b16bd7977%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6uUHnySkOmxzVP\_R35y0hd\_jcMxyQj2NN5QJxE\_zcaAg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6uUHnySkOmxzVP_R35y0hd_jcMxyQj2NN5QJxE_zcaAg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![aken](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@aken](https://discuss.elastic.co/u/aken)\
**Post date:** [January 13, 2015, 9:49pm UTC](https://discuss.elastic.co/t/filters-vs-queries-es-version-1-4/21614/3 "2015-01-13T21:49:05Z")

</div>

Hallo Adrien,

Many thanks for the clarification.

But won't the query cache (1.4.0-Beta)

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index-modules-shard-query-cache.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index-modules-shard-query-cache.html)

partially "close the gap" in performance? The cache won't be as lightweight  
as the filter cache (as the key is the entire JSON query rather than a  
bitset and operates at shard-level), but is a cache nonetheless.

Or am I comparing two completely different things?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/2a197fce-6492-4f71-8688-b8710058dbd7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2a197fce-6492-4f71-8688-b8710058dbd7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ed\_Kim](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@Ed\_Kim](https://discuss.elastic.co/u/Ed_Kim)\
**Post date:** [January 13, 2015, 10:55pm UTC](https://discuss.elastic.co/t/filters-vs-queries-es-version-1-4/21614/4 "2015-01-13T22:55:22Z")

</div>

Filters work based on caching bitsets to provide a fast lookup of documents  
that match the criteria. It looks like the query cache actually stores the  
hits returned from within the node. For now, it looks like query cache will  
not help with fetching documents (says count/aggregation/suggestions only),  
but when/if it does work to store search results, it's not optimized for  
frequent updates on the index (since that will invalidate the cache  
result). Filters, on the other hand, are updated aggressively by ES, so new  
documents get incrementally added to the bitset.

So to answer the question, they both have similar goals, but they  
accomplish them differently, so you will have to decide which  
implementation fits your needs better.

On Tuesday, January 13, 2015 at 1:49:06 PM UTC-8, AndrewK wrote:

> Hallo Adrien,
> 
> Many thanks for the clarification.
> 
> But won't the query cache (1.4.0-Beta)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index-modules-shard-query-cache.html)
> 
> partially "close the gap" in performance? The cache won't be as  
> lightweight as the filter cache (as the key is the entire JSON query rather  
> than a bitset and operates at shard-level), but is a cache nonetheless.
> 
> Or am I comparing two completely different things?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8cacc807-7efb-437c-a22f-5d75f849b41c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8cacc807-7efb-437c-a22f-5d75f849b41c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:39am UTC](https://discuss.elastic.co/t/filters-vs-queries-es-version-1-4/21614/5 "2017-07-06T00:39:13Z")

</div>


