Caching and boolean queries for autocomplete


(Josh Devins-3) #1

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


(system) #2