# Caching and boolean queries for autocomplete

**URL:** <https://discuss.elastic.co/t/caching-and-boolean-queries-for-autocomplete/5456>\
**Category:** Elasticsearch\
**Created:** [September 26, 2011, 6:43pm UTC](https://discuss.elastic.co/t/caching-and-boolean-queries-for-autocomplete/5456 "2011-09-26T18:43:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Devins\_3](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@Josh\_Devins\_3](https://discuss.elastic.co/u/Josh_Devins_3)\
**Post date:** [September 26, 2011, 6:43pm UTC](https://discuss.elastic.co/t/caching-and-boolean-queries-for-autocomplete/5456/1 "2011-09-26T18:43:29Z")

</div>

Hi folks,

I have the following boolean query that is being fired for about every  
keystroke for auto-complete:

{  
"query": {  
"bool": {  
"must": [  
{ "term": { "groupId": 1781 } },  
{ "prefix": { "description": "pre" } }  
]  
}  
}  
}

I have an alternative to this that uses a filter for the prefix part  
and just a term query:

{  
"query": {  
"term": { "groupId": 1781 }  
},  
"filter": {  
"prefix": { "description": "pre" }  
}  
}

I understand that the second one, since it uses a filter, can cache  
the result out of the filter. However what I'm looking for is to  
ideally cache the query on the term (groupId), so I can reuse that  
query result for subsequent keystrokes and subsequent prefix filters.  
Is this possible with ES? If not, is it reasonable to consider doing  
this or should either query 1 or 2 above be sufficient for a look-  
ahead/auto-complete type scenario?

I was also considering a query cache using an out-of-process cache  
(aka Redis) so the query cache can be looked up from any node. Since  
the query term in this case is a simple ID, the result set is easily  
cacheable on this natural key. I've never tried to build a plugin for  
ES, but is it possible to hook in to post-query and pre-filter to  
inject a cache like this? Of course, the cache would need to be  
invalidated when this ID is updated in the index, but I can trigger  
this from outside of ES easily (unless there is also a way to hook  
into the create/update flow of ES).

Thoughts?

Many thanks,

Josh

---

<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:53am UTC](https://discuss.elastic.co/t/caching-and-boolean-queries-for-autocomplete/5456/2 "2017-07-06T03:53:32Z")

</div>

Closed on July 6, 2017, 3:53am UTC


