# Bool query x Script filter

**URL:** <https://discuss.elastic.co/t/bool-query-x-script-filter/97198>\
**Category:** Elasticsearch\
**Created:** [August 16, 2017, 7:44am UTC](https://discuss.elastic.co/t/bool-query-x-script-filter/97198 "2017-08-16T07:44:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paulo\_Silva](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Paulo\_Silva](https://discuss.elastic.co/u/Paulo_Silva)\
**Post date:** [August 16, 2017, 7:44am UTC](https://discuss.elastic.co/t/bool-query-x-script-filter/97198/1 "2017-08-16T07:44:14Z")

</div>

Hi all,

I've come across with this situation here in my project. By running the following query:

```
GET /unittesttg1_tg1_fq1/_search
{
  "size": 0,
  "aggs": {
    "groupby": {
      "date_histogram": {
        "field": "LAST_MODIFIED_DATE",
        "interval": "month",
        "format": "yyyyMM",
        "min_doc_count": 1,
        "time_zone": "+08:00"
      },
      "aggs": {
        "measure": {
          "sum": {
            "field": "ENUMID"
          }
        }
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "range": {
                  "LAST_MODIFIED_DATE": {
                    "gte": "2017-01-01T00:00:00",
                    "lte": "2017-01-31T23:59:59",
                    "time_zone": "+08:00"
                  }
                }
              },
              {
                "range": {
                  "LAST_MODIFIED_DATE": {
                    "gte": "2017-02-01T00:00:00",
                    "lte": "2017-02-28T23:59:59",
                    "time_zone": "+08:00"
                  }
                }
              }
            ],
            "filter": [
              {
                "script": {
                  "script": {
                    "inline": " (ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('+08:00')).getHour() == 8) || (ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('+08:00')).getHour() == 9) "
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

I'm getting the months aggregated in and out the interval (I'm supposed to filter just the months "2017-01" and "2017-02"). For instance, the aggregated value of "2017-03" and "2017-04" are being shown.

However, if I remove the scripting filter bit ( **below** ) the monthly interval works all right

```
 "filter": [
                  {
                    "script": {
                      "script": {
                        "inline": " (ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('+08:00')).getHour() == 8) || (ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('+08:00')).getHour() == 9) "
                      }
                    }
                  }
                ]

```

But of course, I lose the script filter I need.  
Any thoughts?

Thanks  
'

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 16, 2017, 1:19pm UTC](https://discuss.elastic.co/t/bool-query-x-script-filter/97198/2 "2017-08-16T13:19:20Z")

</div>

can you quickly explain what your goal here is? I dont think that the script filter is a great idea, because it requires processing of every single hit, but I want to understand the use-case first.

Thanks!

---

<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:** [September 13, 2017, 1:19pm UTC](https://discuss.elastic.co/t/bool-query-x-script-filter/97198/3 "2017-09-13T13:19:31Z")

</div>

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