Filtered query vs query w/ filter


(asanderson) #1

Is there any difference in performance (or otherwise) between using a
filtered query vs. a query with a filter at the same level?

e.g.
{
"query": {
"filtered" : {
"query" : {
"term" : { "foo" : "bar" }
},
"filter" : {
"term" : { "foo2" : "bar2" }
}
}
}
}

vs.

{
"query": {
"term" : { "foo" : "bar" }
},
"filter" : {
"term" : { "foo2" : "bar2" }
}
}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Jason Wee) #2

The hunch said the first example is probably faster but try execute and
record the timespent? it should give an indication.

Jason

On Sat, Aug 31, 2013 at 5:22 AM, asanderson a.steven.anderson@gmail.comwrote:

Is there any difference in performance (or otherwise) between using a
filtered query vs. a query with a filter at the same level?

e.g.
{
"query": {
"filtered" : {
"query" : {
"term" : { "foo" : "bar" }
},
"filter" : {
"term" : { "foo2" : "bar2" }
}
}
}
}

vs.

{
"query": {
"term" : { "foo" : "bar" }
},
"filter" : {
"term" : { "foo2" : "bar2" }
}
}

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Zachary Tong) #3

A filtered query is faster than a top-level filter. Filtered queries first
apply the filter, then perform query phase on remaining documents.
Top-level filter first executes the query (e.g. on all documents), then
applies the top-level filter to docs that matched the query.

The only time to use a top-level filter is when you need to filter search
results but not facet results. Since top-level filters are applied after
queries, and facets get their data from the query results, top-level
filters don't affect the facet results at all. This can be useful in
certain situations, usually when you need the facet results in the UI to
not change.

So yeah, tl;dr: use filtered_query unless you are doing facets and need
specific behavior =)

-Zach

On Friday, August 30, 2013 5:22:34 PM UTC-4, asanderson wrote:

Is there any difference in performance (or otherwise) between using a
filtered query vs. a query with a filter at the same level?

e.g.
{
"query": {
"filtered" : {
"query" : {
"term" : { "foo" : "bar" }
},
"filter" : {
"term" : { "foo2" : "bar2" }
}
}
}
}

vs.

{
"query": {
"term" : { "foo" : "bar" }
},
"filter" : {
"term" : { "foo2" : "bar2" }
}
}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(asanderson) #4

A filtered query is faster than a top-level filter. Filtered queries
first apply the filter, then perform query phase on remaining documents.
Top-level filter first executes the query (e.g. on all documents), then
applies the top-level filter to docs that matched the query.

The only time to use a top-level filter is when you need to filter search
results but not facet results. Since top-level filters are applied after
queries, and facets get their data from the query results, top-level
filters don't affect the facet results at all. This can be useful in
certain situations, usually when you need the facet results in the UI to
not change.

So yeah, tl;dr: use filtered_query unless you are doing facets and need
specific behavior =)

Zach,

Thanks for the explanation. It would be great if the ES guide had this
explanation. Also, it would be great if top level filters had some
parameter that allowed one to specify before or after query phase.

Thanks!

Steve

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #5