What is the difference between filtered query and regular query with filter?


#1

What is the different between these two queries?

{
"query" : {
"match" : {
"field_1" : true
}
},
"filter" : {
"and" :
[
{
"exists" : {"field" : "field_2"}
},
{
"term" : {"field_3" : "alien"}
}
]
}
}

{
"query" : {
"filtered" : {
"query" : {
"match" : {
"field_1" : true
}
},
"filter" : {
"and" :
[
{
"exists" : {"field" : "field_2"}
},
{
"term" : {"field_3" : "alien"}
}
]
}
}
}
}

is there a reason why one would choose the one against the other? I looked through the doc and can't find any thing explaining the differences.


FilteredQuery slower than query and filter?
(Shay Banon) #2

The difference is the execution mode, and agreed its confusing. This is why we changed the top level filter (as in, the same level as query) to post_filter.

Its all documented in the previous link, but in general, if you want everything to be filtered, you are better off using filtered query. If you want to only apply the filter on the hits, but not on aggregations, you should use post_filter. In your specific example, you should use filtered query.


(system) #3