# Query cache: Alternatives to TERM query

**URL:** <https://discuss.elastic.co/t/query-cache-alternatives-to-term-query/180188>\
**Category:** Elasticsearch\
**Created:** [May 8, 2019, 2:07pm UTC](https://discuss.elastic.co/t/query-cache-alternatives-to-term-query/180188 "2019-05-08T14:07:07Z")\
**Posts on this page:** 6\
**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 8, 2019, 2:07pm UTC](https://discuss.elastic.co/t/query-cache-alternatives-to-term-query/180188/1 "2019-05-08T14:07:07Z")

</div>

Hi,  
I want to take advantage of Query Cache. I have many term queries inside my filters.

I have read somewhere that term queries are not cached.

What are alternatives for a String data type, that i can use, if i want my queries to get cached ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 9, 2019, 11:32am UTC](https://discuss.elastic.co/t/query-cache-alternatives-to-term-query/180188/2 "2019-05-09T11:32:05Z")

</div>

The important part is to use a term query within a filter context, i.e. the `filter` part of a boolean query. Judging from what you said, you already did this.

On top of that the node query cache only kicks in at a certain segment size, I think it's 10k documents.

If you want to know if that cache is being used, you can check via

```auto
GET _nodes/stats?human&filter_path=**.query_cache

```

---

<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 9, 2019, 11:39am UTC](https://discuss.elastic.co/t/query-cache-alternatives-to-term-query/180188/3 "2019-05-09T11:39:35Z")

</div>

Thanks for the reply.

On the 10k document front, its not a worry, we have over billion docs and i am very sure the segment size is exceeding 10k.

Yes, i have already used Term query inside the filter clause. Does it make that term query an eligible candidate for caching ?

Just for background, the issue we are dealing with is oversized shards causing slow search performance. Our shard size has grown upto 100Gb.  
We are looking at more data division by routing by partition and hoping to use query cache.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 9, 2019, 12:24pm UTC](https://discuss.elastic.co/t/query-cache-alternatives-to-term-query/180188/4 "2019-05-09T12:24:06Z")

</div>

Yes, it does. However, there is more to it. You can check the `IndicesQueryCache` for more information. it will cache up to 10k queries in up to 10% of the total heap by default. Those queries will only be cached for a segment (in an LRU cache), if the total number if documents is greater than 10k and the segment has at least 3% of the total documents of a shard.

Next step is possibly to check for evictions in the above cache.

---

<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, 4:43am UTC](https://discuss.elastic.co/t/query-cache-alternatives-to-term-query/180188/5 "2019-05-10T04:43:09Z")

</div>

Yes, ok.  
Let me first check with caching and performance. I will also have to study evictions after that.

Thanks Alex for your inputs.

---

<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 7, 2019, 4:57am UTC](https://discuss.elastic.co/t/query-cache-alternatives-to-term-query/180188/6 "2019-06-07T04:57:05Z")

</div>

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