# Truncating results in watcher action

**URL:** https://discuss.elastic.co/t/truncating-results-in-watcher-action/73908
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [February 3, 2017, 8:41pm UTC](https://discuss.elastic.co/t/truncating-results-in-watcher-action/73908 "2017-02-03T20:41:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![andrey81inmd](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@andrey81inmd](https://discuss.elastic.co/u/andrey81inmd)
#### Post date: [February 3, 2017, 8:41pm UTC](https://discuss.elastic.co/t/truncating-results-in-watcher-action/73908/1 "2017-02-03T20:41:44Z")

</div>

Hello,

I have a query that returns really long strings sometimes. I want to truncate the string to a max length of let's say 200 characters. I'm calling a webhook that has a parameter like this:

"message" : "{{#ctx.payload.hits.hits}}{{\_source.text}} - {{\_source.exception\_message}}  
  
{{/ctx.payload.hits.hits}}"

Is there any easy way to truncate \_source.text and \_source.exception\_message fields when generating this parameter?

Thanks,  
Andrey

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 3, 2017, 9:39pm UTC](https://discuss.elastic.co/t/truncating-results-in-watcher-action/73908/2 "2017-02-03T21:39:50Z")

</div>

Hey Andrey,

mustache itself has no means of truncating, but you can use a script transform in your action to achieve this, see this example

```auto
POST _xpack/watcher/watch/_execute
{
  "watch" : {
    "trigger" : { "schedule" : { "interval" : "10s" } },
    "input" : {
      "simple" : {
        "data" : [
          {"a": "short"}, {"a" : "loooooooooongloooooooooongloooooooooongloooooooooongloooooooooongloooooooooongloooooooooongloooooooooongloooooooooongloooooooooong"}
        ]
      }
    },
    "actions" : {
      "log_error" : {
        "transform" : {
          "script" : {
            "inline" : "ctx.payload.data.collect(e -> e.a.substring(0, (int) Math.min(e.a.length(), 10)))"
          }
        },
        "logging" : {
          "text" : "Found {{ctx.payload.hits.total}} errors in the logs"
        }
      }
    }
  }
}

```

--Alex

---

<div class="post-metadata">

### Author: ![andrey81inmd](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@andrey81inmd](https://discuss.elastic.co/u/andrey81inmd)
#### Post date: [February 3, 2017, 10:14pm UTC](https://discuss.elastic.co/t/truncating-results-in-watcher-action/73908/3 "2017-02-03T22:14:51Z")

</div>

Alex,

That looks great, but doesn't seem to work in my version.. it's returning this error:

```
  "type": "script_exception",
  "reason": "failed to compile script [ScriptException[Failed to compile inline script [ctx.payload.data.collect(e -> e.text.substring(0, (int) Math.min(e.text.length(), 300)))] using lang [groovy]]; nested: ScriptException[failed to compile groovy script]; nested: MultipleCompilationErrorsException[startup failed:\n39b79deb278b37ddf0f53ca43268d9df949f9b2a: 1: unexpected token: -> @ line 1, column 28.\n ctx.payload.data.collect(e -> e.text.substring(0, (int) Math.min(e.text.length(), 300)))\n ^\n\n1 error\n];] with lang [ctx.payload.data.collect(e -> e.text.substring(0, (int) Math.min(e.text.length(), 300)))] of type [groovy]"

```

We haven't upgraded to 5 yet. Is there a way to do this with the previous version?

Thanks,  
Andrey

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 3, 2017, 10:31pm UTC](https://discuss.elastic.co/t/truncating-results-in-watcher-action/73908/4 "2017-02-03T22:31:24Z")

</div>

Hey,

do'h, my bad. I was thinking in terms of Elasticsearch 5 using the new and fresh scripting language. You can do something similar in groovy, along the lines

```auto
x.data.collect { x -> x.a.substring(0, Math.min(x.a.length(), 10)) }

```

not tested this time and top of my more and more rusty groovy skills.

--Alex

---

<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: [March 3, 2017, 10:31pm UTC](https://discuss.elastic.co/t/truncating-results-in-watcher-action/73908/5 "2017-03-03T22:31:56Z")

</div>

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