# Constant\_score wrapper yields different results than Boolean query

**URL:** <https://discuss.elastic.co/t/constant-score-wrapper-yields-different-results-than-boolean-query/212831>\
**Category:** Elasticsearch\
**Created:** [December 23, 2019, 10:21am UTC](https://discuss.elastic.co/t/constant-score-wrapper-yields-different-results-than-boolean-query/212831 "2019-12-23T10:21:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EpsilonDelta](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@EpsilonDelta](https://discuss.elastic.co/u/EpsilonDelta)\
**Post date:** [December 23, 2019, 10:21am UTC](https://discuss.elastic.co/t/constant-score-wrapper-yields-different-results-than-boolean-query/212831/1 "2019-12-23T10:21:41Z")

</div>

The following two queries return a different number of results and I cannot figure out why. The only difference between these two queries is that one is wrapped in `"constant_score": { "filter":{ ...} }.`

Note that the aim of this query is to just get data (the "some\_field" data) within a 1h time interval of a certain host which then will be aggregated into buckets of 1s. I do not care about scores in this case.

**First query with the wrapper:**

```
{
    "query": {
        "constant_score": {
            "filter": {
                "bool": {
                    "should": [
                        {
                            "match": {
                                "hostname": "some_host"
                            }
                        }
                    ],
                    "must": [
                        {
                            "range": {
                                "@timestamp": {
                                    "gte": "2019-09-21T13:12:00",
                                    "lt": "2019-09-21T14:12:00"
                                }
                            }
                        }
                    ],
                    "filter": [
                        {
                            "range": {
                                "some_field": {
                                    "gte": 200,
                                    "lt": 500
                                }
                            }
                        }
                    ]
                }
            }
        }
    },
    "size": 0,
    "aggs": {
        "date_histogram": {
            "date_histogram": {
                "field": "@timestamp",
                "interval": "second"
            }
        }
    }
}

```

**Second query without the wrapper which yields more results:**

```
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "hostname": "some_host"
                    }
                }
            ],
            "must": [
                {
                    "range": {
                        "@timestamp": {
                            "gte": "2019-09-21T13:12:00",
                            "lt": "2019-09-21T14:12:00"
                        }
                    }
                }
            ],
            "filter": [
                {
                    "range": {
                        "some_field": {
                            "gte": 200,
                            "lt": 500
                        }
                    }
                }
            ]
        }
    },
    "size": 0,
    "aggs": {
        "date_histogram": {
            "date_histogram": {
                "field": "@timestamp",
                "interval": "second"
            }
        }
    }
}

```

Furthermore, since these queries were already implemented, I tried to clean the query up and got

```
{
"query": {
    "bool": {
        "filter": [
            {
                "match": {
                    "hostname": "some_host"
                }
            },
            {
                "range": {
                    "@timestamp": {
                        "gte": "2019-09-21T13:12:00",
                        "lt": "2019-09-21T14:12:00"
                    }
                }
            },
            {
                "range": {
                    "some_field": {
                        "gte": 200,
                        "lt": 500
                    }
                }
            }
        ]
    }
},
"size": 0,
"aggs": {
    "date_histogram": {
        "date_histogram": {
            "field": "@timestamp",
            "interval": "second"
        }
    }
}

```

}

The third query yields the same number of hits as the query with the `constant_score` clause.

---

<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:** [January 20, 2020, 10:21am UTC](https://discuss.elastic.co/t/constant-score-wrapper-yields-different-results-than-boolean-query/212831/2 "2020-01-20T10:21:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
