# Unable to use painless script in group\_by term in transform

**URL:** <https://discuss.elastic.co/t/unable-to-use-painless-script-in-group-by-term-in-transform/239294>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 30, 2020, 12:22pm UTC](https://discuss.elastic.co/t/unable-to-use-painless-script-in-group-by-term-in-transform/239294 "2020-06-30T12:22:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayazahmed](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@ayazahmed](https://discuss.elastic.co/u/ayazahmed)\
**Post date:** [June 30, 2020, 12:22pm UTC](https://discuss.elastic.co/t/unable-to-use-painless-script-in-group-by-term-in-transform/239294/1 "2020-06-30T12:22:39Z")

</div>

following the tutorial from [https://www.elastic.co/guide/en/elasticsearch/reference/current/transform-painless-examples.html#painless-group-by](https://www.elastic.co/guide/en/elasticsearch/reference/current/transform-painless-examples.html#painless-group-by)

```auto
POST _transform/_preview
{
  "source": {
    "index": [ 
      "kibana_sample_data_logs"
    ]
  },
  "pivot": {
    "group_by": {
      "agent": {
        "terms": {
          "script": { 
            "source": """String agent = doc['agent.keyword'].value;
            if (agent.contains("MSIE")) {
              return "internet explorer";
            } else if (agent.contains("AppleWebKit")) {
              return "safari";
            } else if (agent.contains('Firefox')) {
              return "firefox";
            } else { return agent }""",
            "lang": "painless"
          }
        }
      }
    },
    "aggregations": { 
      "200": {
        "filter": {
          "term": {
            "response": "200"
          }
        }
      },
      "404": {
        "filter": {
          "term": {
            "response": "404"
          }
        }
      },
      "503": {
        "filter": {
          "term": {
            "response": "503"
          }
        }
      }
    }
  },
  "dest": { 
    "index": "pivot_logs"
  }
}

```

response:

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "x_content_parse_exception",
        "reason" : "[1:20] [data_frame_terms_group] unknown field [script]"
      }
    ],
    "type" : "x_content_parse_exception",
    "reason" : "[1:447] [data_frame_transform_config] failed to parse field [pivot]",
    "caused_by" : {
      "type" : "x_content_parse_exception",
      "reason" : "[1:447] [data_frame_transform_pivot] failed to parse field [group_by]",
      "caused_by" : {
        "type" : "x_content_parse_exception",
        "reason" : "[1:20] [data_frame_terms_group] unknown field [script]"
      }
    }
  },
  "status" : 400
}

```

elastic version:

```auto
{
  "name" : "loges",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "1jvrf2DvTbCByIZMjMFPqA",
  "version" : {
    "number" : "7.6.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
    "build_date" : "2020-02-29T00:15:25.529771Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

```

---

<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:** [June 30, 2020, 2:15pm UTC](https://discuss.elastic.co/t/unable-to-use-painless-script-in-group-by-term-in-transform/239294/2 "2020-06-30T14:15:53Z")

</div>

The documentation link targets the latest version which is `7.8`, your version is `7.6.1`.

Script support in `group_by` has been added in [7.7](https://github.com/elastic/elasticsearch/pull/53167).

---

<div class="post-metadata">

**Author:** ![ayazahmed](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@ayazahmed](https://discuss.elastic.co/u/ayazahmed)\
**Post date:** [June 30, 2020, 2:26pm UTC](https://discuss.elastic.co/t/unable-to-use-painless-script-in-group-by-term-in-transform/239294/3 "2020-06-30T14:26:36Z")

</div>

Thanks @Hendrik_Muhs, Do we have any alternative with same concept, in 7.6.1 ?

---

<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:** [June 30, 2020, 2:38pm UTC](https://discuss.elastic.co/t/unable-to-use-painless-script-in-group-by-term-in-transform/239294/4 "2020-06-30T14:38:07Z")

</div>

If your usecase is similar to the one from the documentation, you could fix the data during ingest or clean it up using reindex.

---

<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:** [July 28, 2020, 2:38pm UTC](https://discuss.elastic.co/t/unable-to-use-painless-script-in-group-by-term-in-transform/239294/5 "2020-07-28T14:38:21Z")

</div>

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