# Use painless script in transform aggregation

**URL:** <https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600>\
**Category:** Elasticsearch\
**Created:** [July 9, 2020, 7:54pm UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600 "2020-07-09T19:54:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DILIP\_SHARMA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dilip_sharma/32/42672_2.png) [@DILIP\_SHARMA](https://discuss.elastic.co/u/DILIP_SHARMA)\
**Post date:** [July 9, 2020, 7:54pm UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600/1 "2020-07-09T19:54:15Z")

</div>

Hi i am running 7.6.2 Elastic search and kibana ,  
and i was trying simple painless script in transform aggregation .

```
POST _transform/_preview
{
  "source": {
    "index": "testindex*"
  },
 
  "pivot": {
      "group_by": {
      "customer.keyword": {
        "terms": {
          "field": "customer.keyword"
        }
      },
      "locationcode.keyword": {
        "terms": {
          "field": "locationcode.keyword"
        }
      }
    },
    "aggregations": {

 "script_fields": {
      "hour_of_day": { 
        "script": {
          "lang": "painless",
          "source": """
            ZonedDateTime date = doc['journeydate'].value; 
            return date.getHour(); 
          """
        }
      },
      "month_of_year": { 
        "script": {
          "lang": "painless",
          "source": """
            ZonedDateTime date = doc['journeydate'].value; 
            return date.getMonthValue(); 
          """
        }
      }
    }

    }
    
    
  }}

```

but its throw the error

```
{
  "error" : {
    "root_cause" : [
      {
        "type" : "named_object_not_found_exception",
        "reason" : "[1:35] unable to parse BaseAggregationBuilder with name [month_of_year]: parser not found"
      }
    ],
    "type" : "x_content_parse_exception",
    "reason" : "[1:525] [data_frame_transform_config] failed to parse field [pivot]",
    "caused_by" : {
      "type" : "x_content_parse_exception",
      "reason" : "[1:525] [data_frame_transform_pivot] failed to parse field [aggregations]",
      "caused_by" : {
        "type" : "named_object_not_found_exception",
        "reason" : "[1:35] unable to parse BaseAggregationBuilder with name [month_of_year]: parser not found"
      }
    }
  },
  "status" : 400
}

```

any suggestion on this ?  
thanks

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [July 11, 2020, 8:32am UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600/2 "2020-07-11T08:32:08Z")

</div>

Hi,

there is no `script_fields` aggregation, maybe you mean `scripted_metric` or `bucket_script`. Examples can be found in the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/transform-painless-examples.html).

Hendrik

---

<div class="post-metadata">

**Author:** ![DILIP\_SHARMA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dilip_sharma/32/42672_2.png) [@DILIP\_SHARMA](https://discuss.elastic.co/u/DILIP_SHARMA)\
**Post date:** [July 11, 2020, 9:02am UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600/3 "2020-07-11T09:02:26Z")

</div>

Hi ,  
i am talking about `script_fields` same available in [doc](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/transform-painless-examples.html#painless-time-features)

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [July 13, 2020, 6:17am UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600/4 "2020-07-13T06:17:14Z")

</div>

Thanks, this looks like an error in the documentation, I will check with the docs team.

What's your usecase? What feature do you want to extract? `script_fields` can not be used together with aggregations, because they are only applied to the result set, which is a limited (top n) set of results.

However scripts can be used in aggregations:

```auto
  "aggregations": {
    "avg_hour": {
      "avg": {
        "script": {
          "source": "ZonedDateTime date = doc['order_date'].value; return date.getHour();"
        }
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![DILIP\_SHARMA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dilip_sharma/32/42672_2.png) [@DILIP\_SHARMA](https://discuss.elastic.co/u/DILIP_SHARMA)\
**Post date:** [July 13, 2020, 6:38am UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600/5 "2020-07-13T06:38:13Z")

</div>

Thanks ,  
my use case is trying to create transform index on my sales index .  
which can have field like -  
1 last visit ( which i am getting with max ( visitdate) )  
2 week of last visit ( here i was trying to script , as i need to use last visit field to achieved this .)  
i will try as per ur suggestion and will get back to you .

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [July 13, 2020, 10:03am UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600/6 "2020-07-13T10:03:07Z")

</div>

We fixed the documentation, I think with a `max` aggregation and a `script`, you should be able to implement 2. Once you have it, please share your solution.

---

<div class="post-metadata">

**Author:** ![DILIP\_SHARMA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dilip_sharma/32/42672_2.png) [@DILIP\_SHARMA](https://discuss.elastic.co/u/DILIP_SHARMA)\
**Post date:** [July 13, 2020, 12:28pm UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600/7 "2020-07-13T12:28:26Z")

</div>

Hi @Hendrik_Muhs ,  
Thanks for your help ,yes i able to do that .  
there some more condition will be , which team will put it .  
but this is my basic input and output

```
POST _transform/_preview
{
  "source": {
    "index": "bdf_salesinvoicec1_3_2019"
  },
 
  "pivot": {
      "group_by": {
      "customer.keyword": {
        "terms": {
          "field": "customer.keyword"
        }
      },
      "locationcode.keyword": {
        "terms": {
          "field": "locationcode.keyword"
        }
      }
    },
   "aggregations": {
       "maxDoc_Date": { "max": { "field": "documentdate" }},
    "MonthofDate": {
      "max": {
        "script": {
          "source": "ZonedDateTime date = doc['documentdate'].value; return date.getMonthValue();"
                    
        }
        }
        },
        "dayofmonth": {
        "max": {
          "script": {
            "source": "ZonedDateTime date = doc['documentdate'].value; return date.getDayOfMonth();"
                    
        }
		}
		},
       "weekday": {
        "max": {
          "script": {
            "source": "return doc['documentdate'].value.getDayOfWeek() ;"
                    
        }
        
      }
    }
        }}}

```

output --

```
{
  "preview" : [
    {
      "MonthofDate" : 9.0,
      "weekday" : 5.0,
      "locationcode" : {
        "keyword" : "location1"
      },
      "customer" : {
        "keyword" : "customer 1 "
      },
      "maxDoc_Date" : "2019-09-20T01:35:50.283Z",
      "dayofmonth" : 20.0
    },
    {
      "MonthofDate" : 8.0,
      "weekday" : 4.0,
      "locationcode" : {
        "keyword" : "location2"
      },
      "customer" : {
        "keyword" : "customer 2"
      },
      "maxDoc_Date" : "2019-08-19T07:43:25.000Z",
      "dayofmonth" : 19.0
    }...

```

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:** [August 10, 2020, 12:28pm UTC](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600/8 "2020-08-10T12:28:29Z")

</div>

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