# Adding script\_fields to transform index using painless scripts

**URL:** <https://discuss.elastic.co/t/adding-script-fields-to-transform-index-using-painless-scripts/240601>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 9, 2020, 8:04pm UTC](https://discuss.elastic.co/t/adding-script-fields-to-transform-index-using-painless-scripts/240601 "2020-07-09T20:04:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![InfiniteDreamer](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@InfiniteDreamer](https://discuss.elastic.co/u/InfiniteDreamer)\
**Post date:** [July 9, 2020, 8:04pm UTC](https://discuss.elastic.co/t/adding-script-fields-to-transform-index-using-painless-scripts/240601/1 "2020-07-09T20:04:01Z")

</div>

we have fields like hour of day, week of month,quarter of year.. derived from @timestamp field using scripted field in kibana.

We want to create a transform index to summarize our operational data, we want to store those fields hour of day, week of month,.. into transformed index instead of creating scripted fields in kibana.

we ran the following query in elasticsearch 7.5.1 and 7.8.0. both versions are giving back the same error. Kindly help us to solve the problem.

DSL:

```auto
POST /_transform/_preview?pretty
{
  "source": {
    "index": "kibana_sample_data_ecommerce"
  },
  "pivot": {
    "group_by": {
      "customer_first_name.keyword": {
        "terms": {
          "field": "customer_first_name.keyword"
        }
      },
      "day_of_week": {
        "terms": {
          "field": "day_of_week"
        }
      }
    },
    "aggregations": {
      "order_date": {
        "max": {
          "field": "order_date"
        }
      },
      "script_fields": {
        "hour_of_day": {
          "script": {
               "lang": "painless",
                "source": """
                ZonedDateTime date = doc['order_date'].value;
                if(!doc['order_date'].empty)
                {
                return date.getHour();
                }
              """
             
          }
        }
      }
    }
  },
  "dest": {
    "index": "pivot_logs"
  }
}

```

Mapping for todel002

```auto
    {
      "pivot_logs" : {
        "mappings" : {
          "properties" : {
            "customer_first_name" : {
              "properties" : {
                "keyword" : {
                  "type" : "keyword"
                }
              }
            },
            "day_of_week" : {
              "type" : "keyword"
            },
            "order_date" : {
              "properties" : {
                "max" : {
                  "type" : "date"
                }
              }
            },
            "script_fields" : {
              "properties" : {
                "hour_of_day" : {
                  "type" : "integer"
                }
              }
            }
          }
        }
      }
    }

```

after adding script\_field to aggregation throwing the following error.

```auto
{
      "error" : {
        "root_cause" : [
          {
            "type" : "named_object_not_found_exception",
            "reason" : "[1:77] unknown field [hour_of_day]"
          }
        ],
        "type" : "x_content_parse_exception",
        "reason" : "[1:466] [data_frame_transform_config] failed to parse field [pivot]",
        "caused_by" : {
          "type" : "x_content_parse_exception",
          "reason" : "[1:466] [data_frame_transform_pivot] failed to parse field [aggregations]",
          "caused_by" : {
            "type" : "named_object_not_found_exception",
            "reason" : "[1:77] unknown field [hour_of_day]"
          }
        }
      },
      "status" : 400
    }

```

---

<div class="post-metadata">

**Author:** ![InfiniteDreamer](https://avatars.discourse-cdn.com/v4/letter/i/779978/32.png) [@InfiniteDreamer](https://discuss.elastic.co/u/InfiniteDreamer)\
**Post date:** [July 14, 2020, 12:11pm UTC](https://discuss.elastic.co/t/adding-script-fields-to-transform-index-using-painless-scripts/240601/2 "2020-07-14T12:11:07Z")

</div>

Hi,  
Kindly help

---

<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 14, 2020, 1:02pm UTC](https://discuss.elastic.co/t/adding-script-fields-to-transform-index-using-painless-scripts/240601/3 "2020-07-14T13:02:50Z")

</div>

Hi,

we had a bug in our documentation, please check the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/transform-painless-examples.html#painless-time-features) again for an example.

Instead of `script_fields` use `script` together with an aggregation.

It seems you run into the same issue as the user in this [thread](https://discuss.elastic.co/t/use-painless-script-in-transform-aggregation/240600). Please check his last post for the solution.

---

<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 11, 2020, 1:15pm UTC](https://discuss.elastic.co/t/adding-script-fields-to-transform-index-using-painless-scripts/240601/4 "2020-08-11T13:15:10Z")

</div>

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