# Constant\_score, filtered and filter in a query

**URL:** <https://discuss.elastic.co/t/constant-score-filtered-and-filter-in-a-query/11594>\
**Category:** Elasticsearch\
**Created:** [April 15, 2013, 8:21pm UTC](https://discuss.elastic.co/t/constant-score-filtered-and-filter-in-a-query/11594 "2013-04-15T20:21:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Andhavarapu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_andhavarapu/32/782_2.png) [@Abhishek\_Andhavarapu](https://discuss.elastic.co/u/Abhishek_Andhavarapu)\
**Post date:** [April 15, 2013, 8:21pm UTC](https://discuss.elastic.co/t/constant-score-filtered-and-filter-in-a-query/11594/1 "2013-04-15T20:21:01Z")

</div>

Hi All,

My understanding is that filtered executes on the results of the filter.  
I'm trying to include the filtered along with the filter wrapped by  
constant\_score. I'm trying to understand how the query works.

My document structure  
ID,List,Date : Date

{  
"filtered" : {  
"query" : {  
"term" : { "Date" : "xxx" }  
},  
"query": {  
"constant\_score": {  
"filter": {  
"terms": { "DataIds": [61, 71], "execution": "and", "\_cache":  
true }  
}  
}  
}  
}  
}

The above query is to get the count of documents, whose DataIDs contains  
61,71 and document date is xxx and I have 50 million documents across 5  
shards. I'm trying to figure out the best possible way to achieve this. I'm  
using \_msearch with search\_type as count in the header.

1. Is the filter section broken down to 2 separate filters 61, 71 that are  
executed separately and results are intersected ? Or is the filter  
[61,71] executed at once meaning the iterate the data set once ?

2. Help structuring the above query. I get syntax errors

3. Is filtered section parallel like filter executed on all the shards or  
the results aggregated to once place and results are further filter there ?

Any help is appreciated,

Thanks.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 16, 2013, 6:15pm UTC](https://discuss.elastic.co/t/constant-score-filtered-and-filter-in-a-query/11594/2 "2013-04-16T18:15:18Z")

</div>

I am not sure why you need constant\_score here, but I think that the query  
that you had in mind is this:

{  
"filtered": {  
"query": {  
"term": {  
"Date": "xxx"  
}  
},  
"filter": {  
"terms": {  
"DataIds": [61, 71], "execution": "and", "\_cache": true  
}  
}  
}  
}

The "terms" filter will create an individual term filters for each term and  
then combine them using "and" filter so it will iterate over data once. The  
filter will be executed on all shards.

On Monday, April 15, 2013 4:21:01 PM UTC-4, Abhishek Andhavarapu wrote:

> Hi All,
> 
> My understanding is that filtered executes on the results of the filter.  
> I'm trying to include the filtered along with the filter wrapped by  
> constant\_score. I'm trying to understand how the query works.
> 
> My document structure  
> ID,List,Date : Date
> 
> {  
> "filtered" : {  
> "query" : {  
> "term" : { "Date" : "xxx" }  
> },  
> "query": {  
> "constant\_score": {  
> "filter": {  
> "terms": { "DataIds": [61, 71], "execution": "and", "\_cache":  
> true }  
> }  
> }  
> }  
> }  
> }
> 
> The above query is to get the count of documents, whose DataIDs contains  
> 61,71 and document date is xxx and I have 50 million documents across 5  
> shards. I'm trying to figure out the best possible way to achieve this. I'm  
> using \_msearch with search\_type as count in the header.
> 
> 1. Is the filter section broken down to 2 separate filters 61, 71 that are  
> executed separately and results are intersected ? Or is the filter  
> [61,71] executed at once meaning the iterate the data set once ?
> 
> 2. Help structuring the above query. I get syntax errors
> 
> 3. Is filtered section parallel like filter executed on all the shards or  
> the results aggregated to once place and results are further filter there ?
> 
> Any help is appreciated,
> 
> Thanks.

--  
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).  
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, 2:40am UTC](https://discuss.elastic.co/t/constant-score-filtered-and-filter-in-a-query/11594/3 "2017-07-06T02:40:53Z")

</div>


