Constant_Score vs Bool Query

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.

Hi,

this looks like a problem with the leniency of the Query DSL to me. Constant Score 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.

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