Filter array values using painless script


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.

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


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