# Painless: string to word list

**URL:** <https://discuss.elastic.co/t/painless-string-to-word-list/91532>\
**Category:** Kibana\
**Created:** [July 2, 2017, 1:39am UTC](https://discuss.elastic.co/t/painless-string-to-word-list/91532 "2017-07-02T01:39:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smlbiobot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smlbiobot/32/29622_2.png) [@smlbiobot](https://discuss.elastic.co/u/smlbiobot)\
**Post date:** [July 2, 2017, 1:39am UTC](https://discuss.elastic.co/t/painless-string-to-word-list/91532/1 "2017-07-02T01:39:37Z")

</div>

I am trying to break a string field into a list of words in order to do word occurrence count, but am having some trouble figuring how to write it properly. This is difficult especially since there is no way for me to save my script in-progress and if I leave an incorrect painless script in Kibana, nothing will work.

Anyway, this is what I have right now:

```
def text = doc['content.keyword'].value;

ArrayList words = new ArrayList();
BreakIterator breakIterator = BreakIterator.getWordInstance();
breakIterator.setText(text);
int lastIndex = breakIterator.first();
while (BreakIterator.DONE != lastIndex) {
    int firstIndex = lastIndex;
    lastIndex = breakIterator.next();
    if (lastIndex != BreakIterator.DONE && Character.isLetterOrDigit(text.charAt(firstIndex))) {
        words.add(text.substring(firstIndex, lastIndex));
    }
}

return words;

```

I recognize that this probably failed because words is an ArrayList and the field type can only be string / int / boolean. How might I do this?

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [July 3, 2017, 5:49pm UTC](https://discuss.elastic.co/t/painless-string-to-word-list/91532/2 "2017-07-03T17:49:08Z")

</div>

Without using regexes, this seems like a valid approach. Is it possible to provide the errors you are seeing when you attempt to do it this way?

Edit: The words ArrayList can handle any type as all items added to it are considered of type 'def' in Painless, so I don't believe that is the issue here.

---

<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 31, 2017, 5:49pm UTC](https://discuss.elastic.co/t/painless-string-to-word-list/91532/3 "2017-07-31T17:49:09Z")

</div>

Closed on July 31, 2017, 5:49pm UTC

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