Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it matter if the filter is inside or outside a filtered query?

While these two are giving me the same result, what is the difference between having the filter inside or outside? Is there a difference in terms of performance or the actions behind the scenes?

"query": {
    "filtered": {
        "query":  { "match": { "email": "business opportunity" }},
        "filter": { "term":  { "folder": "inbox" }}
    }
}
"query": {
    "filtered": {
    "query":  { "match": { "email": "business opportunity" }}
    }
},
"filter": {
    "term":  { "folder": "inbox" }
}
like image 489
Sum NL Avatar asked Mar 09 '15 08:03

Sum NL


1 Answers

Filter inside a Query

Elasticsearch will execute the query and filter in what ever way is more efficient to reduce the results set and get you the answer as quickly as possible. This is referred to as a filtered_query

Filter after a Query

The query is run first and then results are filtered before returning them to the client. This is referred to as a post_filter.

Although a post_filter is less efficient, it can be useful when combining with an aggregation - the aggregated values won't take into account the post_filter, i.e. the aggregation will only be on the query results.

like image 54
Olly Cruickshank Avatar answered Oct 15 '22 12:10

Olly Cruickshank