# TermFilter as part of OR Filter - Caching

**URL:** https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471
**Category:** Elasticsearch
**Created:** [April 26, 2012, 8:09am UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471 "2012-04-26T08:09:20Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![aditya\_tripathi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aditya_tripathi/32/1491_2.png) [@aditya\_tripathi](https://discuss.elastic.co/u/aditya_tripathi)
#### Post date: [April 26, 2012, 8:09am UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/1 "2012-04-26T08:09:20Z")

</div>

Hi,  
I had a quick question.

In our app we use lot of term filters as part of OR filters. Term Filters  
are automatically cached. Are they also cached when they are part of an OR  
Filter.

I observe a sharp rise in filter cache size when users where for whom these  
OR filter will have lot of term filters log in to the system.

-Thanks.

--  
View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/TermFilter-as-part-of-OR-Filter-Caching-tp3940555p3940555.html](http://elasticsearch-users.115913.n3.nabble.com/TermFilter-as-part-of-OR-Filter-Caching-tp3940555p3940555.html)  
Sent from the ElasticSearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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 27, 2012, 10:30am UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/2 "2012-04-27T10:30:54Z")

</div>

Hiya

> In our app we use lot of term filters as part of OR filters. Term Filters  
> are automatically cached. Are they also cached when they are part of an OR  
> Filter.

No, not unless you specify that the OR filter should be cached.

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"or" : {  
"\_cache" : 1,  
"filters" : [  
{  
"term" : {  
"bar" : 2  
}  
},  
{  
"term" : {  
"foo" : 1  
}  
}  
]  
}  
}  
}  
}  
}  
'

You may also try setting the individual term filters to NOT be cached:

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"or" : {  
"\_cache" : 1,  
"filters" : [  
{  
"term" : {  
"\_cache" : 0,  
"foo" : 1  
}  
},  
{  
"term" : {  
"\_cache" : 0,  
"bar" : 2  
}  
}  
]  
}  
}  
}  
}  
}  
'

Let us know how this works out - I'd be interested to see.

clint

---

<div class="post-metadata">

### Author: ![aditya\_tripathi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aditya_tripathi/32/1491_2.png) [@aditya\_tripathi](https://discuss.elastic.co/u/aditya_tripathi)
#### Post date: [April 27, 2012, 10:58am UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/3 "2012-04-27T10:58:12Z")

</div>

You are right - You have to explicitly set \_cache=true for OR Filters to get cached.  
And all the Term Filters in that OR Filter are cached by default unless explicitly set to false.

---

<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 29, 2012, 4:37pm UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/4 "2012-04-29T16:37:00Z")

</div>

Also, it might make sense to use term_s_ filter, also, if you have  
combination of term/terms/range filters, it might make sense to use bool  
filter (which does bitwise operations) compared to or filter. It will  
probably improve perf.

On Fri, Apr 27, 2012 at 1:30 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> Hiya
> 
> > In our app we use lot of term filters as part of OR filters. Term Filters  
> > are automatically cached. Are they also cached when they are part of an  
> > OR  
> > Filter.
> 
> No, not unless you specify that the OR filter should be cached.
> 
> curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
> {  
> "query" : {  
> "constant\_score" : {  
> "filter" : {  
> "or" : {  
> "\_cache" : 1,  
> "filters" : [  
> {  
> "term" : {  
> "bar" : 2  
> }  
> },  
> {  
> "term" : {  
> "foo" : 1  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }  
> '
> 
> You may also try setting the individual term filters to NOT be cached:
> 
> curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
> {  
> "query" : {  
> "constant\_score" : {  
> "filter" : {  
> "or" : {  
> "\_cache" : 1,  
> "filters" : [  
> {  
> "term" : {  
> "\_cache" : 0,  
> "foo" : 1  
> }  
> },  
> {  
> "term" : {  
> "\_cache" : 0,  
> "bar" : 2  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }  
> '
> 
> Let us know how this works out - I'd be interested to see.
> 
> clint

---

<div class="post-metadata">

### Author: ![Tim\_J](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Tim\_J](https://discuss.elastic.co/u/Tim_J)
#### Post date: [May 24, 2012, 4:22pm UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/5 "2012-05-24T16:22:34Z")

</div>

Shay,  
I'm a bit confused about the performance of bool vs. and/or  
filters. In your previous post you mentioned that it might make sense  
to try a bool filter as it would be more performant than or. The docs  
for and/or filters ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-)  
dsl/and-filter.html) seem to say the opposite. Could you clarify?

Thanks,  
-Tim

On Apr 29, 12:37 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Also, it might make sense to use term_s_ filter, also, if you have  
> combination of term/terms/range filters, it might make sense to use bool  
> filter (which does bitwise operations) compared to or filter. It will  
> probably improve perf.
> 
> On Fri, Apr 27, 2012 at 1:30 PM, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com)wrote:
> 
> > Hiya
> 
> > > In our app we use lot of term filters as part of OR filters. Term Filters  
> > > are automatically cached. Are they also cached when they are part of an  
> > > OR  
> > > Filter.
> 
> > No, not unless you specify that the OR filter should be cached.
> 
> > curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
> > {  
> > "query" : {  
> > "constant\_score" : {  
> > "filter" : {  
> > "or" : {  
> > "\_cache" : 1,  
> > "filters" : [  
> > {  
> > "term" : {  
> > "bar" : 2  
> > }  
> > },  
> > {  
> > "term" : {  
> > "foo" : 1  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > }  
> > }  
> > '
> 
> > You may also try setting the individual term filters to NOT be cached:
> 
> > curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
> > {  
> > "query" : {  
> > "constant\_score" : {  
> > "filter" : {  
> > "or" : {  
> > "\_cache" : 1,  
> > "filters" : [  
> > {  
> > "term" : {  
> > "\_cache" : 0,  
> > "foo" : 1  
> > }  
> > },  
> > {  
> > "term" : {  
> > "\_cache" : 0,  
> > "bar" : 2  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > }  
> > }  
> > '
> 
> > Let us know how this works out - I'd be interested to see.
> 
> > 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: [May 24, 2012, 4:41pm UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/6 "2012-05-24T16:41:34Z")

</div>

On Thu, 2012-05-24 at 09:22 -0700, Tim J wrote:

> Shay,  
> I'm a bit confused about the performance of bool vs. and/or  
> filters. In your previous post you mentioned that it might make sense  
> to try a bool filter as it would be more performant than or. The docs  
> for and/or filters ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-)  
> dsl/and-filter.html) seem to say the opposite. Could you clarify?

As I understand it:

- an 'or' filter is not cached. instead, the individual clauses might  
be cached
- a 'bool' filter IS cached.

So, for the two clauses (status = 'active') and (tag = 'foo'):

- if you always use them together, then combine them with a 'bool'  
filter
- if you use each clause often, but independently - eg perhaps you  
always use (status = 'active') but you combine it with many version  
of (tag = $tag) - then rather use an 'or' filter

Similarly, for the 'terms' filter, if you always query  
(tag = foo or tag=bar) together, then use the 'bool' execution.

If you have lots of combinations (eg (tag=foo), (tag=bar), (tag=foo or  
tag=bar), (tag=foo or tag=baz) etc) then use the 'plain' (or) execution.

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: [May 24, 2012, 4:46pm UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/7 "2012-05-24T16:46:30Z")

</div>

> As I understand it:
> 
> - an 'or' filter is not cached. instead, the individual clauses might  
> be cached
> - a 'bool' filter IS cached.

Apparently, the last line above is incorrect. From the docs:

```
    The result of the bool filter is not cached by default (though
    internal filters might be). The _cache can be set to true in
    order to enable caching.

```

So @kimchy: i'm a bit confused as well.

---

<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: [May 29, 2012, 6:15pm UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/8 "2012-05-29T18:15:37Z")

</div>

The way filters work (most of them, like range, terms, and any cached one)  
is that it creates a bitset with "on"/"off" for each document matching. The  
bool filter works by doing bitwise operation on that bitset. and/or/not  
work by being part of the iteration process over matching docs, doing it  
"on the fly" for a document.

usually, for filters that already have a fixed bitset representation, it  
makes sense to use bool filter (those include term, terms, range, and  
cached filters). Ones that don't, and compute the filter per doc (like the  
geo ones), it makes sense to use or/and/not filter.

On Thu, May 24, 2012 at 6:46 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> > As I understand it:
> > 
> > - an 'or' filter is not cached. instead, the individual clauses might  
> > be cached
> > - a 'bool' filter IS cached.
> 
> Apparently, the last line above is incorrect. From the docs:
> 
> ```
> The result of the bool filter is not cached by default (though
> internal filters might be). The _cache can be set to true in
> order to enable caching.
> 
> ```
> 
> So @kimchy: i'm a bit confused as well.

---

<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:26am UTC](https://discuss.elastic.co/t/termfilter-as-part-of-or-filter-caching/7471/9 "2017-07-06T03:26:36Z")

</div>


