# What kind of filters are really cached?

**URL:** <https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559>\
**Category:** Elasticsearch\
**Created:** [May 10, 2019, 12:47pm UTC](https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559 "2019-05-10T12:47:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)\
**Post date:** [May 10, 2019, 12:47pm UTC](https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559/1 "2019-05-10T12:47:29Z")

</div>

Hi,  
I am building queries so that i can take advantage of query caching.  
I need to know, how to write a filter so that it can be cached.  
Can someone please help me if the below will be cached ?

```
{
	"query": {
		"bool": {
			"must": [
				// Some queries
			],
			"filter": [
				{
					"term": {
						"type": {
							"value": "Cars",
							"boost": 1.0
						}
					}
				}
			]
		}
	}
}

```

When i run the above my cached index with same documents and cache disabled index with same documents gives exactly same throughput. The queryCache size goes upto 50Mb.

I am not able to understand why my caching and its utilization is not working.  
Please help !

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 10, 2019, 1:57pm UTC](https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559/2 "2019-05-10T13:57:14Z")

</div>

Why do you think cache is not working? I mean can you give details about your test?

---

<div class="post-metadata">

**Author:** ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)\
**Post date:** [May 13, 2019, 7:13am UTC](https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559/3 "2019-05-13T07:13:24Z")

</div>

I have two separate ES clusters:  
Cluster A - "queries.cache.enable=false" - 35million docs  
Cluster B - "queries.cache.enable=true" - 35million docs

The only difference in A and B is cache, rest everything is exactly same.

_Test_ -  
I have test queries like above of ~3lakh records. I run it against A and then B.  
The throughput i get is exactly same.

_Cached details_:  
I can see data cached on A is 0kb and on B is 100mb. So some queries are getting cached on B for sure.

**Query** :  
Why is ES not utilizing the cache and giving me better performance on B ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 13, 2019, 9:41am UTC](https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559/4 "2019-05-13T09:41:21Z")

</div>

Most of the cache used is the file system cache. @jpountz can tell more on this topic hopefully.

---

<div class="post-metadata">

**Author:** ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)\
**Post date:** [May 13, 2019, 9:46am UTC](https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559/5 "2019-05-13T09:46:33Z")

</div>

Thanks. I thought it was in-memory cache as it would use some part of assigned heap. Not sure though.

@jpountz, any help would be appreciated. We are stuck on a important issue on production.

---

<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:** [May 20, 2019, 8:29am UTC](https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559/6 "2019-05-20T08:29:48Z")

</div>

There are several caches, a shard request cache, a filter cache, plus Elasticsearch heavily relies on the OS cache.

Term filters are never cached, for a good reason: the inverted index is already a cache of hits for every possible term. So if all your queries look like the one you shared, it's expected that the query cache doesn't help.

---

<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:** [June 17, 2019, 8:30am UTC](https://discuss.elastic.co/t/what-kind-of-filters-are-really-cached/180559/7 "2019-06-17T08:30:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
