The current generation of queries that supply a Filter will result in a root level filter. For example, if you had:
query.Query(q => q.Number > 10).Where(q => q.State == "WA");
then this generates something along the lines of
{
"query" : {
"range" : {
"Number" : {
"gt" : 10
}
}
},
"filter" : {
"term" : {
"State" : "WA"
}
}
}
Note: it's not really useful to score a range query as it either matches or not, but that's not the point.
However, using the root-level filter is actually using something called Post Filter, which is a post processing filter. This is useful when you want to pass certain hits to facets/aggregations, but then you need to prune them from being returned within the overall response (a rare need). What you actually want is a filtered query that itself contains a filter and/or a query.
The above should become:
{
"query" : {
"filtered" : {
"query" : {
"Number" : {
"range" : {
"gt" : 10
}
}
},
"filter" : {
"term" : {
"State" : "WA"
}
}
}
}
}
This will perform the filter before any query (or hits being returned), which is when you want it to happen.
The current generation of queries that supply a
Filterwill result in a root levelfilter. For example, if you had:then this generates something along the lines of
{ "query" : { "range" : { "Number" : { "gt" : 10 } } }, "filter" : { "term" : { "State" : "WA" } } }Note: it's not really useful to score a
rangequery as it either matches or not, but that's not the point.However, using the root-level
filteris actually using something called Post Filter, which is a post processing filter. This is useful when you want to pass certain hits to facets/aggregations, but then you need to prune them from being returned within the overall response (a rare need). What you actually want is afilteredquery that itself contains afilterand/or aquery.The above should become:
{ "query" : { "filtered" : { "query" : { "Number" : { "range" : { "gt" : 10 } } }, "filter" : { "term" : { "State" : "WA" } } } } }This will perform the
filterbefore any query (or hits being returned), which is when you want it to happen.