# Plain filter and constant\_score

**URL:** <https://discuss.elastic.co/t/plain-filter-and-constant-score/15058>\
**Category:** Elasticsearch\
**Created:** [December 30, 2013, 10:53pm UTC](https://discuss.elastic.co/t/plain-filter-and-constant-score/15058 "2013-12-30T22:53:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Han\_JU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han_ju/32/962_2.png) [@Han\_JU](https://discuss.elastic.co/u/Han_JU)\
**Post date:** [December 30, 2013, 10:53pm UTC](https://discuss.elastic.co/t/plain-filter-and-constant-score/15058/1 "2013-12-30T22:53:10Z")

</div>

Hi,

We are currently benchmarking our ES setup so I've got some new questions:

1. We found out that, for the same query (filter actually), when put like  
this:

{  
"filter": {...},  
"fields": [...]  
}

is consistently slower than this form:

{  
"query": {  
"constant\_score": {  
"filter": {...},  
...  
}  
}  
}.

All filter and fields part are identical, but the performance is different,  
especially when caches are warm, filters wrapped in a constant\_score are  
nearly 10x faster than when they are put directly.  
So what happens behind this? How ElasticSearch interpret filters that are  
put directly (not wrapped in any outer structure)?

Thanks in advance.

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ad91095f-7e7b-4def-8570-73dbccaa7cb2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ad91095f-7e7b-4def-8570-73dbccaa7cb2%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [December 30, 2013, 11:16pm UTC](https://discuss.elastic.co/t/plain-filter-and-constant-score/15058/2 "2013-12-30T23:16:43Z")

</div>

The "outer" filter is basically a post filter, ie. filtering happens after  
all the documents have been collected via the query. This should not  
really be used unless you are trying to do something like multi-select  
faceting where you don't want facet counts to be affected by the filter.  
You should be using a filtered query [1] or as you discovered a constant  
score query if you only want to execute a filter.

BTW, in elasticsearch 1.0, this outer filter has been renamed to  
"post\_filter" to avoid some of the confusion.

Thanks,  
Matt Weber

On Mon, Dec 30, 2013 at 2:53 PM, Han JU [ju.han.felix@gmail.com](mailto:ju.han.felix@gmail.com) wrote:

> Hi,
> 
> We are currently benchmarking our ES setup so I've got some new questions:
> 
> 1. We found out that, for the same query (filter actually), when put like  
> this:
> 
> {  
> "filter": {...},  
> "fields": [...]  
> }
> 
> is consistently slower than this form:
> 
> {  
> "query": {  
> "constant\_score": {  
> "filter": {...},  
> ...  
> }  
> }  
> }.
> 
> All filter and fields part are identical, but the performance is  
> different, especially when caches are warm, filters wrapped in a  
> constant\_score are nearly 10x faster than when they are put directly.  
> So what happens behind this? How Elasticsearch interpret filters that are  
> put directly (not wrapped in any outer structure)?
> 
> Thanks in advance.
> 
> --  
> 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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/ad91095f-7e7b-4def-8570-73dbccaa7cb2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ad91095f-7e7b-4def-8570-73dbccaa7cb2%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAJ3KEoBhOub8U5zc\_DzsecE4LrsxF40PUBaDCvnwX%3DWrO4f50w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJ3KEoBhOub8U5zc_DzsecE4LrsxF40PUBaDCvnwX%3DWrO4f50w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Han\_JU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han_ju/32/962_2.png) [@Han\_JU](https://discuss.elastic.co/u/Han_JU)\
**Post date:** [December 31, 2013, 2:44pm UTC](https://discuss.elastic.co/t/plain-filter-and-constant-score/15058/3 "2013-12-31T14:44:32Z")

</div>

Thanks Matt.  
It seems that we've done terrible things ... effectively `post_filter` is a  
better name.

在 2013年12月31日星期二UTC+1上午12时16分43秒，Matt Weber写道：

> The "outer" filter is basically a post filter, ie. filtering happens after  
> all the documents have been collected via the query. This should not  
> really be used unless you are trying to do something like multi-select  
> faceting where you don't want facet counts to be affected by the filter.  
> You should be using a filtered query [1] or as you discovered a constant  
> score query if you only want to execute a filter.
> 
> BTW, in elasticsearch 1.0, this outer filter has been renamed to  
> "post\_filter" to avoid some of the confusion.
> 
> Thanks,  
> Matt Weber
> 
> On Mon, Dec 30, 2013 at 2:53 PM, Han JU \<[ju.han...@gmail.com](mailto:ju.han...@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > Hi,
> > 
> > We are currently benchmarking our ES setup so I've got some new questions:
> > 
> > 1. We found out that, for the same query (filter actually), when put like  
> > this:
> > 
> > {  
> > "filter": {...},  
> > "fields": [...]  
> > }
> > 
> > is consistently slower than this form:
> > 
> > {  
> > "query": {  
> > "constant\_score": {  
> > "filter": {...},  
> > ...  
> > }  
> > }  
> > }.
> > 
> > All filter and fields part are identical, but the performance is  
> > different, especially when caches are warm, filters wrapped in a  
> > constant\_score are nearly 10x faster than when they are put directly.  
> > So what happens behind this? How Elasticsearch interpret filters that are  
> > put directly (not wrapped in any outer structure)?
> > 
> > Thanks in advance.
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/ad91095f-7e7b-4def-8570-73dbccaa7cb2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ad91095f-7e7b-4def-8570-73dbccaa7cb2%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/050b2a38-4393-4fa5-a036-41fd29a797c9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/050b2a38-4393-4fa5-a036-41fd29a797c9%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:58am UTC](https://discuss.elastic.co/t/plain-filter-and-constant-score/15058/4 "2017-07-06T01:58:55Z")

</div>


