Queries vs Filters - Order of execution
elasticsearch
Solution
Luckily, ES provides two types of filters for you to work with:
{
"query" : {
"field" : { "title" : "Catch-22" }
},
"filter" : {
"term" : { "year" : 1961 }
}
}
{
"query": {
"filtered" : {
"query" : {
"field" : { "title" : "Catch-22" }
},
"filter" : {
"term" : { "year" : 1961 }
}
}
}
}
In the first case, filters are applied to all documents found by the query. In the second case, the documents are filtered before the query runs. This yields better performance.
Quoted from: http://www.packtpub.com/elasticsearch-server-for-fast-scalable-flexible-search-solution/book
About cache, I'm not sure about cache mechanism of filters. My guessing would be: First case, since the filter is against a set of results returned by query, the cache is kind of specific for this return set. Second case, the filter is applied first, the cache is stored for the indices you checked against, thus, this cache is more reusable because it does not rely on the content of the query, but at larger memory cost and query time for first time(before the cache is generated).
Problem
I've read this question and a colleague of mine made me doubt: In a filtered query, when is the filter applied ? Before or after executing the query ? When is the result cached ? If the filter is applied beforehand, wouldn't it be a a good thing to duplicate the query part in the filters ? If the filter is applied afterward, then i'm having trouble understanding what is cached.