Hi,

I am a little new to using painless scripting.  I was able to use it in a kibana visualization to convert the units of a field.  Also in a query_and_update scenario.  

Now I have a situation/aggregation that does not fit kibana, so trying to use the 3rd party _transform_ plugin.   I thought it should be possible to use scripted_fields to count the number of occurrences of a specific term in the aggregated results.  But, I havent figured how the right syntax it seems.

Once past this problem - If statusText can be "a" "b" or "c" etc., I would like to calculate how many "a", "b" and "c" were found.

    {
      "aggs": {
          "terms": {
            "field": "myfield.raw",
            "size": 10000
          },
          "aggs": {
            "latest_record": {
              "top_hits": {
                "sort": [
                  {
                    "@timestamp": {
                      "order": "desc"
                    }
                  }
                ],
                "_source": {
                  "includes": [
                    "statusText"
                  ]
                },
                "size": 1
              }
            }
          }
        },
        size = 0,
       "scripted_fields": {
       "script": {
    "lang": "painless",
    "inline": "int cnt=0; return cnt;"
          }
       }
    }
`"[parsing_exception] Unknown key for a START_OBJECT in [scripted_fields]., with { line=1 & col=227 }"`

Thanks

---