# Constant\_score vs. filtered; and a follow up about custom\_filters\_score

**URL:** <https://discuss.elastic.co/t/constant-score-vs-filtered-and-a-follow-up-about-custom-filters-score/15182>\
**Category:** Elasticsearch\
**Created:** [January 9, 2014, 8:04pm UTC](https://discuss.elastic.co/t/constant-score-vs-filtered-and-a-follow-up-about-custom-filters-score/15182 "2014-01-09T20:04:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weitzenfeld1](https://avatars.discourse-cdn.com/v4/letter/w/8e7dd6/32.png) [@weitzenfeld1](https://discuss.elastic.co/u/weitzenfeld1)\
**Post date:** [January 9, 2014, 8:04pm UTC](https://discuss.elastic.co/t/constant-score-vs-filtered-and-a-follow-up-about-custom-filters-score/15182/1 "2014-01-09T20:04:57Z")

</div>

_Question 1_  
Is there a preferred way to write a query that consists solely of filters -  
that is, return documents iff they pass a set of filters?

From this[http://elasticsearch-users.115913.n3.nabble.com/Filters-vs-Queries-td3219558.html](http://elasticsearch-users.115913.n3.nabble.com/Filters-vs-Queries-td3219558.html)thread, I've gathered that there are two options:

- Constant Score:

- Filtered:

Am I right that these will produce identical results? Is there a reason to  
prefer one over the other?

_Question 2_

I'd like to write a query that first uses filters to limit to a subset of  
documents (as in question 1), then uses function scoring with filters to  
score each of the the documents in the subset.

A year ago, I used custom\_filters\_score, like so:

{  
"query":{  
"custom\_filters\_score":{  
"query":{  
"filtered":{  
"query":{  
"match\_all":{

```
              }
           },
           "filter":{
              "and":[ 

```

- 

```
              <limiting filters>*
            ]
         }
      }
   },
   "filters":[ 

```

- 

```
         <scoring filters>*
   ],
   "params":{

   },
   "score_mode":"total"
}

```

}  
}

Now, with function score query, I'm thinking:

{  
"query":{  
"function\_score":{  
"functions":[  
\* \*  
],  
"query":{  
"filtered":{  
"query":{  
"match\_all":{

```
              }
           },
           "filter":{
              "and":[
                 * <limiting filters>*
              ]
           }
        }
     },
     "score_mode":"first"
  }

```

}  
}

Is this the best way to achieve what I want?

--  
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/b8c0ac6d-b496-4b6e-b5f5-4b510b4ac634%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b8c0ac6d-b496-4b6e-b5f5-4b510b4ac634%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:** ![weitzenfeld1](https://avatars.discourse-cdn.com/v4/letter/w/8e7dd6/32.png) [@weitzenfeld1](https://discuss.elastic.co/u/weitzenfeld1)\
**Post date:** [January 9, 2014, 8:42pm UTC](https://discuss.elastic.co/t/constant-score-vs-filtered-and-a-follow-up-about-custom-filters-score/15182/2 "2014-01-09T20:42:18Z")

</div>

I'll answer _Question 1_ myself:

I read the entire thread I linked to and kimchy says:

"A filtered query with a match\_all query is automatically converted  
internally to a constant\_score one, so guess which one is better? :)"

On Thursday, January 9, 2014 3:04:57 PM UTC-5, [weitz...@howaboutwe.com](mailto:weitz...@howaboutwe.com)  
wrote:

> _Question 1_  
> Is there a preferred way to write a query that consists solely of filters
> 
> - that is, return documents iff they pass a set of filters?
> 
> From this[http://elasticsearch-users.115913.n3.nabble.com/Filters-vs-Queries-td3219558.html](http://elasticsearch-users.115913.n3.nabble.com/Filters-vs-Queries-td3219558.html)thread, I've gathered that there are two options:
> 
> - Constant Score:
> 
> - Filtered:
> 
> Am I right that these will produce identical results? Is there a reason  
> to prefer one over the other?
> 
> _Question 2_
> 
> I'd like to write a query that first uses filters to limit to a subset of  
> documents (as in question 1), then uses function scoring with filters to  
> score each of the the documents in the subset.
> 
> A year ago, I used custom\_filters\_score, like so:
> 
> {  
> "query":{  
> "custom\_filters\_score":{  
> "query":{  
> "filtered":{  
> "query":{  
> "match\_all":{
> 
> ```
> }
> },
> "filter":{
> "and":[ 
> 
> ```
> 
> - 
> 
> ```
> <limiting filters>*
> ]
> }
> }
> },
> "filters":[ 
> 
> ```
> 
> - 
> 
> ```
> <scoring filters>*
> ],
> "params":{
> 
> },
> "score_mode":"total"
> }
> 
> ```
> 
> }  
> }
> 
> Now, with function score query, I'm thinking:
> 
> {  
> "query":{  
> "function\_score":{  
> "functions":[  
> \* \*  
> ],  
> "query":{  
> "filtered":{  
> "query":{  
> "match\_all":{
> 
> ```
> }
> },
> "filter":{
> "and":[
> * <limiting filters>*
> ]
> }
> }
> },
> "score_mode":"first"
> }
> 
> ```
> 
> }  
> }
> 
> Is this the best way to achieve what I want?

--  
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/81e3eb91-b488-4890-9852-4f6b1a9ee966%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/81e3eb91-b488-4890-9852-4f6b1a9ee966%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:57am UTC](https://discuss.elastic.co/t/constant-score-vs-filtered-and-a-follow-up-about-custom-filters-score/15182/3 "2017-07-06T01:57:34Z")

</div>


