# Filter array values using painless script

**URL:** <https://discuss.elastic.co/t/filter-array-values-using-painless-script/249468>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 22, 2020, 7:53am UTC](https://discuss.elastic.co/t/filter-array-values-using-painless-script/249468 "2020-09-22T07:53:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![askids](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/askids/32/22252_2.png) [@askids](https://discuss.elastic.co/u/askids)\
**Post date:** [September 22, 2020, 7:53am UTC](https://discuss.elastic.co/t/filter-array-values-using-painless-script/249468/1 "2020-09-22T07:53:49Z")

</div>

hi,

I am trying to get some stats for field that has array values. Just for overall stats, I am using the stats api (sample given below). But now, I want to apply a filter on values within the array and get stats only when they meet certain condition. Lets say, I want consider values in the array that are starting with Y. How can I achieve that via script? Please let me know. My index has roughly around 2M records.

```auto
    POST /myindex/_search?size=0
    {
      "aggs": {
        "my_stats": {
           "stats": {
             "script": {
               "lang" : "painless",
               "source" : "doc['arrayfieldname'].size()"
             } 
          }
        }
      }
    }

```

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:** [October 20, 2020, 7:53am UTC](https://discuss.elastic.co/t/filter-array-values-using-painless-script/249468/2 "2020-10-20T07:53:52Z")

</div>

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