# Caching filters

**URL:** https://discuss.elastic.co/t/caching-filters/5802
**Category:** Elasticsearch
**Created:** [November 7, 2011, 10:05pm UTC](https://discuss.elastic.co/t/caching-filters/5802 "2011-11-07T22:05:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Tim\_Pease](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_pease/32/3084_2.png) [@Tim\_Pease](https://discuss.elastic.co/u/Tim_Pease)
#### Post date: [November 7, 2011, 10:05pm UTC](https://discuss.elastic.co/t/caching-filters/5802/1 "2011-11-07T22:05:39Z")

</div>

I am attempting to create a cached filter that will be used to filter  
out search results according to some "whitelist" and "blacklist" URLs.  
The filter I created can be viewed at the following gist:

> <https://gist.github.com/TwP/1346329>

My question is do I need to explicitly cache each filter? Or can I  
just set { "\_cache": true } on the outermost _and_ filter?

The first file in the gist shows the explicit caching, and the second  
file in the gist shows the implicit caching (only caching the _and_  
filter). I only care about the collection of all the filters taken  
together. I will not be using these same filters individually in other  
queries.

Blessings,  
TwP

---

<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: [November 9, 2011, 7:01am UTC](https://discuss.elastic.co/t/caching-filters/5802/2 "2011-11-09T07:01:08Z")

</div>

You can only cache the outmost "and", assuming the same filter will be used  
in other requests. Internal filters can be cached, and then, if they are  
used on their own in other type of filters, the cached form will be used  
(term filters, for example, are automatically cached).

One thing that I would try and see, if possibly use a single bool filter  
instead of the combination of and / or / not over the relevant term /  
prefix filters. With many elements, it might make more sense.

On Tue, Nov 8, 2011 at 12:05 AM, Tim Pease [tim.pease@gmail.com](mailto:tim.pease@gmail.com) wrote:

> I am attempting to create a cached filter that will be used to filter  
> out search results according to some "whitelist" and "blacklist" URLs.  
> The filter I created can be viewed at the following gist:
> 
> [Examples of ES Filter Cache · GitHub](https://gist.github.com/1346329)
> 
> My question is do I need to explicitly cache each filter? Or can I  
> just set { "\_cache": true } on the outermost _and_ filter?
> 
> The first file in the gist shows the explicit caching, and the second  
> file in the gist shows the implicit caching (only caching the _and_  
> filter). I only care about the collection of all the filters taken  
> together. I will not be using these same filters individually in other  
> queries.
> 
> Blessings,  
> TwP

---

<div class="post-metadata">

### Author: ![Tim\_Pease](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_pease/32/3084_2.png) [@Tim\_Pease](https://discuss.elastic.co/u/Tim_Pease)
#### Post date: [November 9, 2011, 6:01pm UTC](https://discuss.elastic.co/t/caching-filters/5802/3 "2011-11-09T18:01:21Z")

</div>

Thanks for the response and clarifying the caching feature. And yes, I did  
run across the "bool" filter. It does the job perfectly!

TwP

---

<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:49am UTC](https://discuss.elastic.co/t/caching-filters/5802/4 "2017-07-06T03:49:15Z")

</div>


