# Caching AND filter

**URL:** https://discuss.elastic.co/t/caching-and-filter/5240
**Category:** Elasticsearch
**Created:** [August 24, 2011, 4:52pm UTC](https://discuss.elastic.co/t/caching-and-filter/5240 "2011-08-24T16:52:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jasper\_van\_Wanrooy\_C](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasper_van_wanrooy_c/32/2494_2.png) [@Jasper\_van\_Wanrooy\_C](https://discuss.elastic.co/u/Jasper_van_Wanrooy_C)
#### Post date: [August 24, 2011, 4:52pm UTC](https://discuss.elastic.co/t/caching-and-filter/5240/1 "2011-08-24T16:52:04Z")

</div>

Hi,

I'm trying to cache an AND filter, because it is used to define a scope for almost all queries. However, I can't get it to work.  
Context:

- ES 0.17.6
- single node.
- 3Mio+ documents in the index of the same type.

I made a gist of the query and the accompanying response: [https://gist.github.com/cb4db4d846b27b94501e](https://gist.github.com/cb4db4d846b27b94501e).  
The documentation about the \_cache for the and filter does not state explicitly how to use the cache.

Can someone help me with using the \_cache in the right way?

Thanks, Jasper

---

<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 24, 2011, 5:32pm UTC](https://discuss.elastic.co/t/caching-and-filter/5240/2 "2011-08-24T17:32:26Z")

</div>

In this case, the "and" element should have an inner "filters" object in it  
to enable caching, something like:

"and" : { "filters" : [...], "\_cache" : true }

Thats because you can't place another "setting" / field into a case with:  
"and" : [...]

On Wed, Aug 24, 2011 at 7:52 PM, Jasper van Wanrooy - Chatventure \<  
[jvanwanrooy@chatventure.com](mailto:jvanwanrooy@chatventure.com)\> wrote:

> Hi,
> 
> I'm trying to cache an AND filter, because it is used to define a scope for  
> almost all queries. However, I can't get it to work.  
> Context:
> 
> - ES 0.17.6
> - single node.
> - 3Mio+ documents in the index of the same type.
> 
> I made a gist of the query and the accompanying response:  
> [Caching AND filter in Elasticsearch · GitHub](https://gist.github.com/cb4db4d846b27b94501e).  
> The documentation about the \_cache for the and filter does not state  
> explicitly how to use the cache.
> 
> Can someone help me with using the \_cache in the right way?
> 
> Thanks, Jasper  
> \*  
> \*

---

<div class="post-metadata">

### Author: ![Jasper\_van\_Wanrooy\_C](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasper_van_wanrooy_c/32/2494_2.png) [@Jasper\_van\_Wanrooy\_C](https://discuss.elastic.co/u/Jasper_van_Wanrooy_C)
#### Post date: [August 24, 2011, 8:52pm UTC](https://discuss.elastic.co/t/caching-and-filter/5240/3 "2011-08-24T20:52:50Z")

</div>

Thanks for your fast response Shay. Does this work the same way for all filters which need to be cacheable? Then I can implement it properly into Elastica.

Thanks, Jasper

On 24 aug. 2011, at 19:32, Shay Banon wrote:

> In this case, the "and" element should have an inner "filters" object in it to enable caching, something like:
> 
> "and" : { "filters" : [...], "\_cache" : true }
> 
> Thats because you can't place another "setting" / field into a case with: "and" : [...]
> 
> On Wed, Aug 24, 2011 at 7:52 PM, Jasper van Wanrooy - Chatventure [jvanwanrooy@chatventure.com](mailto:jvanwanrooy@chatventure.com) wrote:  
> Hi,
> 
> I'm trying to cache an AND filter, because it is used to define a scope for almost all queries. However, I can't get it to work.  
> Context:
> 
> - ES 0.17.6
> - single node.
> - 3Mio+ documents in the index of the same type.
> 
> I made a gist of the query and the accompanying response: [Caching AND filter in Elasticsearch · GitHub](https://gist.github.com/cb4db4d846b27b94501e).  
> The documentation about the \_cache for the and filter does not state explicitly how to use the cache.
> 
> Can someone help me with using the \_cache in the right way?
> 
> Thanks, Jasper

---

<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 24, 2011, 9:19pm UTC](https://discuss.elastic.co/t/caching-and-filter/5240/4 "2011-08-24T21:19:13Z")

</div>

Its the same \_cache flags on all filters (though different filters have  
different default value for it). But, for filters that have a short form,  
like the and filter, then a specific longer form is also available that  
allows to explicitly set the \_cache flag. You can use the longer form always  
in a client lib you build on top of elasticsearch, which should simplify  
things.

The docs should include samples (where missing), for special cases \_cache  
flag setting, such as the case with the and filter. Lemme try and update  
them where its relevant.

On Wed, Aug 24, 2011 at 11:52 PM, Jasper van Wanrooy - Chatventure \<  
[jvanwanrooy@chatventure.com](mailto:jvanwanrooy@chatventure.com)\> wrote:

> Thanks for your fast response Shay. Does this work the same way for all  
> filters which need to be cacheable? Then I can implement it properly into  
> Elastica.
> 
> Thanks, Jasper
> 
> On 24 aug. 2011, at 19:32, Shay Banon wrote:
> 
> In this case, the "and" element should have an inner "filters" object in it  
> to enable caching, something like:
> 
> "and" : { "filters" : [...], "\_cache" : true }
> 
> Thats because you can't place another "setting" / field into a case with:  
> "and" : [...]
> 
> On Wed, Aug 24, 2011 at 7:52 PM, Jasper van Wanrooy - Chatventure \<  
> [jvanwanrooy@chatventure.com](mailto:jvanwanrooy@chatventure.com)\> wrote:
> 
> > Hi,
> > 
> > I'm trying to cache an AND filter, because it is used to define a scope  
> > for almost all queries. However, I can't get it to work.  
> > Context:
> > 
> > - ES 0.17.6
> > - single node.
> > - 3Mio+ documents in the index of the same type.
> > 
> > I made a gist of the query and the accompanying response:  
> > [Caching AND filter in Elasticsearch · GitHub](https://gist.github.com/cb4db4d846b27b94501e).  
> > The documentation about the \_cache for the and filter does not state  
> > explicitly how to use the cache.
> > 
> > Can someone help me with using the \_cache in the right way?
> > 
> > Thanks, Jasper  
> > \*  
> > \*

---

<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:56am UTC](https://discuss.elastic.co/t/caching-and-filter/5240/5 "2017-07-06T03:56:06Z")

</div>


