# Problem with function\_score and bool query including range

**URL:** <https://discuss.elastic.co/t/problem-with-function-score-and-bool-query-including-range/100701>\
**Category:** Elasticsearch\
**Created:** [September 15, 2017, 4:00pm UTC](https://discuss.elastic.co/t/problem-with-function-score-and-bool-query-including-range/100701 "2017-09-15T16:00:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [September 15, 2017, 4:00pm UTC](https://discuss.elastic.co/t/problem-with-function-score-and-bool-query-including-range/100701/1 "2017-09-15T16:00:51Z")

</div>

I am trying to randomly pick documents from an index. This query successfully returns one document.

```
GET logstash-2017.09.10/_search
{
  "size": 1,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": { "query": "filetype: iis AND env.keyword: PROD" }
            }
          ]
        }
      },
      "random_score": {}
    }
  }
}
```

However, if I add a range to narrow down the time, it starts returning zero documents.

```
GET logstash-2017.09.10/_search
{
  "size": 1,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": { "query": "filetype: iis AND env.keyword: PROD" }
            },
            {
              "range" : {
                "timestamp" : {
                  "gt" : "2016-09-11 00:00:00",
                  "lt" : "2020-09-11 00:00:00"
                }
              }
            }
          ]
       }
      },
      "random_score": {}
    }
  }
}
```

I am expecting the bool/must to mean that documents must match both the query\_string and the range. Is that not how it works? The range covers the entire time span of the index, so I expect it to be a no-op, but it actually stops the query working.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [September 15, 2017, 4:11pm UTC](https://discuss.elastic.co/t/problem-with-function-score-and-bool-query-including-range/100701/2 "2017-09-15T16:11:57Z")

</div>

Arrghh. It should have been

```
"range" : {
                "@timestamp" : {
                  "gt" : "2016-09-11T00:00:00",
                  "lt" : "2020-09-11T00:00:00"
                }
```

What I originally had was copied from the documentation, which appears to me to be incorrect.

---

<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:** [October 13, 2017, 4:12pm UTC](https://discuss.elastic.co/t/problem-with-function-score-and-bool-query-including-range/100701/3 "2017-10-13T16:12:01Z")

</div>

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