# How to apply filter in computed field used script in elaticsearch

**URL:** <https://discuss.elastic.co/t/how-to-apply-filter-in-computed-field-used-script-in-elaticsearch/113115>\
**Category:** Elasticsearch\
**Created:** [December 25, 2017, 2:08am UTC](https://discuss.elastic.co/t/how-to-apply-filter-in-computed-field-used-script-in-elaticsearch/113115 "2017-12-25T02:08:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yanianthe](https://avatars.discourse-cdn.com/v4/letter/y/8dc957/32.png) [@yanianthe](https://discuss.elastic.co/u/yanianthe)\
**Post date:** [December 25, 2017, 2:08am UTC](https://discuss.elastic.co/t/how-to-apply-filter-in-computed-field-used-script-in-elaticsearch/113115/1 "2017-12-25T02:08:07Z")

</div>

My use case is to filter in my computed field used script field,is there any way I can reuse the script field definition in order to apply "terms query" in the "bool" ?

```
{ "query": { "bool": 
            { "must": [ 
                          { "script": { "script": {
                                        "inline":"def monthyear =doc['GoUpDate'].date.year + '-' + doc['GoUpDate'].date.monthOfYear; int flag= 0; for(int i =0;i<params.enums.length;i++){ if(params.enums[i]==monthyear) { flag=1; break; } };return flag ",
                                        "lang":"painless",
                                        "params":{"enums":["2015-1","2015-2"]} } } } ] } },
"script_fields": { "test1": { "script": 
                            { "lang": "painless", "inline": "doc['GoUpDate'].date.year + '-' + doc['GoUpDate'].date.monthOfYear " } } } }

```

thanks!

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [December 27, 2017, 3:48pm UTC](https://discuss.elastic.co/t/how-to-apply-filter-in-computed-field-used-script-in-elaticsearch/113115/2 "2017-12-27T15:48:10Z")

</div>

Hi @yanianthe,

I will try a different approach to filter these dates, please let me know if it work for you.  
I think it's more straightforward: use range query with format + date math.

The idea is to filter GoUpDate field by month without extract the month using script.

```
POST my_index/doc/1
{
  "GoUpDate": "2015-01-18T00:00:00"
}

POST my_index/doc/2
{
  "GoUpDate": "2015-02-18T00:00:00"
}

POST my_index/doc/3
{
  "GoUpDate": "2015-03-18T00:00:00"
}

GET my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "GoUpDate": {
              "gte": "2015-01||/M",
              "lte": "2015-01||/M",
              "format": "yyyy-MM"
            }
          }
        },
        {
          "range": {
            "GoUpDate": {
              "gte": "2015-02||/M",
              "lte": "2015-02||/M",
              "format": "yyyy-MM"
            }
          }
        }
      ]
    }
  },
  "script_fields": {
    "test1": {
      "script": {
        "lang": "painless",
        "source": "doc['GoUpDate'].value.year + '-' + doc['GoUpDate'].value.monthOfYear "
      }
    }
  }
}

```

Results in docs 1 and 2 only:

```
{
  "took": 4,
  "timed_out": false,
  "_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
  },
  "hits": {
"total": 2,
"max_score": 1,
"hits": [
  {
    "_index": "my_index",
    "_type": "doc",
    "_id": "2",
    "_score": 1,
    "fields": {
      "test1": [
        "2015-2"
      ]
    }
  },
  {
    "_index": "my_index",
    "_type": "doc",
    "_id": "1",
    "_score": 1,
    "fields": {
      "test1": [
        "2015-1"
      ]
    }
  }
]
  }
}

```

Hope it helps.

Cheers,  
LG

---

<div class="post-metadata">

**Author:** ![yanianthe](https://avatars.discourse-cdn.com/v4/letter/y/8dc957/32.png) [@yanianthe](https://discuss.elastic.co/u/yanianthe)\
**Post date:** [December 28, 2017, 2:57am UTC](https://discuss.elastic.co/t/how-to-apply-filter-in-computed-field-used-script-in-elaticsearch/113115/3 "2017-12-28T02:57:30Z")

</div>

thanks a lot!

---

<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 25, 2018, 2:58am UTC](https://discuss.elastic.co/t/how-to-apply-filter-in-computed-field-used-script-in-elaticsearch/113115/4 "2018-01-25T02:58:14Z")

</div>

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