# Painless equivalent of simple groovy script (substring issues?)

**URL:** <https://discuss.elastic.co/t/painless-equivalent-of-simple-groovy-script-substring-issues/66769>\
**Category:** Elasticsearch\
**Created:** [November 21, 2016, 7:25pm UTC](https://discuss.elastic.co/t/painless-equivalent-of-simple-groovy-script-substring-issues/66769 "2016-11-21T19:25:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reflection](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reflection/32/13329_2.png) [@reflection](https://discuss.elastic.co/u/reflection)\
**Post date:** [November 21, 2016, 7:25pm UTC](https://discuss.elastic.co/t/painless-equivalent-of-simple-groovy-script-substring-issues/66769/1 "2016-11-21T19:25:28Z")

</div>

The following works in groovy but not in painless; any ideas what I'm doing wrong (using elasticsearch-dsl-py but that shouldn't matter)?:

```
.bucket('protocol', 'terms', script={
          'inline': 'def protocol = doc["name"].value[0..4]; \
                     if (protocol == "https") { \
                       return "https"; \
                     } else if (protocol[0..3] == "http") { \
                       return "http"; \
                     } else { \
                       return "other"; \
                     }',
          'lang': 'groovy'
        }, size=100)

.bucket('protocol', 'terms', script={
          'inline': 'def protocol = doc["name"].value.substring(0, 4); \
                     if (protocol == "https") { \
                       return "https"; \
                     } else if (protocol.substring(0, 3) == "http") { \
                       return "http"; \
                     } else { \
                       return "other"; \
                     }',
          'lang': 'painless'
        }, size=100)
```

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [November 21, 2016, 8:50pm UTC](https://discuss.elastic.co/t/painless-equivalent-of-simple-groovy-script-substring-issues/66769/2 "2016-11-21T20:50:52Z")

</div>

> [@reflection](#):
>
> doc["name"].value[0..4]

`'https://blah'.substring(0, 5)` returns `https` for me. Looks like substring is exclusive while Groovy's `[0..4]` is inclusive.

---

<div class="post-metadata">

**Author:** ![reflection](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reflection/32/13329_2.png) [@reflection](https://discuss.elastic.co/u/reflection)\
**Post date:** [November 21, 2016, 9:48pm UTC](https://discuss.elastic.co/t/painless-equivalent-of-simple-groovy-script-substring-issues/66769/3 "2016-11-21T21:48:26Z")

</div>

Thanks, Nik! Feel dumb for just assuming that groovy's slick notation and substring were the same.

---

<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:** [December 19, 2016, 9:48pm UTC](https://discuss.elastic.co/t/painless-equivalent-of-simple-groovy-script-substring-issues/66769/4 "2016-12-19T21:48:28Z")

</div>

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