# Constant\_Score vs Bool Query

**URL:** <https://discuss.elastic.co/t/constant-score-vs-bool-query/48146>\
**Category:** Elasticsearch\
**Created:** [April 22, 2016, 8:46am UTC](https://discuss.elastic.co/t/constant-score-vs-bool-query/48146 "2016-04-22T08:46:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aboo](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@aboo](https://discuss.elastic.co/u/aboo)\
**Post date:** [April 22, 2016, 8:46am UTC](https://discuss.elastic.co/t/constant-score-vs-bool-query/48146/1 "2016-04-22T08:46:00Z")

</div>

Hi,  
i'm comparing the usage of Constant\_score against a bool query and i'm finding that the latter returns more results.  
I'm not interested in the scoring of results and hence that why i'm going via the constant score route

Constant\_Score Query

GET 2016042\*/mytype/\_search  
{  
"size": 0,  
"explain": false,  
"query": {  
"constant\_score": {  
"filter": {  
"term": {  
"eCode": "event1"  
}  
},  
"filter": {  
"range": {  
"@timestamp": {  
"gte": "now-2d",  
"lte": "now"  
}  
}  
}  
}  
}  
}

Constant Score Result  
{  
"took": 192,  
"timed\_out": false,  
"\_shards": {  
"total": 4,  
"successful": 4,  
"failed": 0  
},  
"hits": {  
"total": 230282,  
"max\_score": 0,  
"hits": []  
}  
}

* * *

Bool Query

GET 2016042\*/mytype/\_search  
{  
"size": 0,  
"explain": false,  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"eCode": {  
"value": "event1"  
}  
}  
}  
],  
"filter": {  
"range": {  
"@timestamp": {  
"gte": "now-2d",  
"lte": "now"  
}  
}  
}  
}  
}  
}

Bool Query result

{  
"took": 165,  
"timed\_out": false,  
"\_shards": {  
"total": 4,  
"successful": 4,  
"failed": 0  
},  
"hits": {  
"total": 223736,  
"max\_score": 0,  
"hits": []  
}  
}

1. Why do the 2 queries return different hits?  
Bool Query : 223736 hits  
Constant Score Query : 230282 hits ( 7K hits more than Bool Query)

2. I was under the impression that the constant score query would perform better. But in this particular scenario, the bool query was faster

In any case, the query will be integrated in a Watcher and that would execute every 5 mins. could there be any issue by virtue of using Constant Score as i understand the query results are cached?

Can someone please help clarify these?

Regards  
A.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 22, 2016, 9:16am UTC](https://discuss.elastic.co/t/constant-score-vs-bool-query/48146/2 "2016-04-22T09:16:10Z")

</div>

Hi,

this looks like a problem with the leniency of the Query DSL to me. [Constant Score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html) accepts only one `filter` element, but you specify two and assume that they will be somehow combined. Instead, the parser replaces the first element with the second, so I think in this case the "term" query part gets overwritten.  
This might also be the reason for the performance differences. You can still use the `constant_score` if you are not interested in scoring, but you need to put a `bool` filter inside to combine the two filtering conditions. Alternatively you can just use the two conditions inside a `bool` queries `filter` clause, then you won't need the constant score around it.

---

<div class="post-metadata">

**Author:** ![aboo](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@aboo](https://discuss.elastic.co/u/aboo)\
**Post date:** [April 29, 2016, 12:20pm UTC](https://discuss.elastic.co/t/constant-score-vs-bool-query/48146/3 "2016-04-29T12:20:08Z")

</div>

Hi Christoph ,  
you were right. the last filter (range) was only taken into consideration and that would explain why the constant\_score query returned more records.  
I went with you recommendation where i still use a constant\_score query but having a bool query underneath it and the outcome yielded the same number of records as the bool query

Thanks a lot for your help

Regards  
Aboo

---

<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 5, 2017, 10:55pm UTC](https://discuss.elastic.co/t/constant-score-vs-bool-query/48146/4 "2017-07-05T22:55:15Z")

</div>


