# Working with strings in Painless

**URL:** <https://discuss.elastic.co/t/working-with-strings-in-painless/107896>\
**Category:** Elasticsearch\
**Created:** [November 16, 2017, 9:46am UTC](https://discuss.elastic.co/t/working-with-strings-in-painless/107896 "2017-11-16T09:46:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![merito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/merito/32/24309_2.png) [@merito](https://discuss.elastic.co/u/merito)\
**Post date:** [November 16, 2017, 9:46am UTC](https://discuss.elastic.co/t/working-with-strings-in-painless/107896/1 "2017-11-16T09:46:13Z")

</div>

Hi,

I'm trying to manipulate strings doing an aggregation, but I fail. I've got a field mapped as a keyword, let's call it `my_field`. There are some data like `12.0343.13`, `3.253.88` in this field in docs. I try to do the aggregation to find the position of the first dot. My code is

```auto
{
  "query": {
    "bool": {
      "filter": [],
      "should":[],
      "must_not": []
    }
  },
    "aggs": {
        "parameter": {
            "terms": {
                "script" : {
                    "inline": "doc['my_field'].value.indexOf('.')",
                    "lang": "painless"
                }
            }
        }
    },
    "size": 0
}

```

but instead of a nice aggregation with values like 1, 2, 3 I've got an error

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [

        ],
        "script": "doc['my_field'].value.indexOf('.')",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "test_index",
        "node": "cXuJC5fxR3GsoK6qHMu2bQ",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [

          ],
          "script": "doc['my_field'].value.indexOf('.')",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          }
        }
      }
    ]
  },
  "status": 500
}

```

My ES version

```auto
"version" : {
    "number" : "5.5.2",
    "build_hash" : "b2f0c09",
    "build_date" : "2017-08-14T12:33:14.154Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  }

```

I cannot use any of the String functions from [Painless API Reference - String](https://www.elastic.co/guide/en/elasticsearch/painless/5.5/painless-api-reference.html#painless-api-reference-String). I think I miss the idea of using this functions, but I don't know what I'm doing wrong.

Best regards

---

<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:** [November 16, 2017, 5:30pm UTC](https://discuss.elastic.co/t/working-with-strings-in-painless/107896/2 "2017-11-16T17:30:33Z")

</div>

Hi Dawid,

If I had to take a guess, it's that some values of 'my\_field' are null. If you modify your script to be the following:

"if (doc['my\_field'].value != null) return doc['my\_field'].value.indexOf('.'); else return -1;"

does it work?

---

<div class="post-metadata">

**Author:** ![merito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/merito/32/24309_2.png) [@merito](https://discuss.elastic.co/u/merito)\
**Post date:** [November 16, 2017, 5:57pm UTC](https://discuss.elastic.co/t/working-with-strings-in-painless/107896/3 "2017-11-16T17:57:07Z")

</div>

Hi Jack,

yes, it works, thank you. But still it is very unintuitive, because this

```auto
doc['my_value'].value

```

works like a charm without handling the null values.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 16, 2017, 7:28pm UTC](https://discuss.elastic.co/t/working-with-strings-in-painless/107896/4 "2017-11-16T19:28:34Z")

</div>

`doc['my_value']` returns an accessor for the field, but it doesn't actually try to access the value until you call `.value`.

---

<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:** [November 16, 2017, 7:46pm UTC](https://discuss.elastic.co/t/working-with-strings-in-painless/107896/5 "2017-11-16T19:46:03Z")

</div>

Hi Dawid,

Out of curiosity, what would you personally expect the behaviour to be here? I only ask because I would like to improve the documentation around this if possible.

Thanks!

---

<div class="post-metadata">

**Author:** ![merito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/merito/32/24309_2.png) [@merito](https://discuss.elastic.co/u/merito)\
**Post date:** [November 16, 2017, 9:17pm UTC](https://discuss.elastic.co/t/working-with-strings-in-painless/107896/6 "2017-11-16T21:17:43Z")

</div>

@rjernst running the code from my previous post as a script works well and there I access the value calling `.value` for every doc, where sometimes are no data

@Jack_Conradson I don't know. I think the documentation is OK, but the behavior is strange. Using `doc['my_field'].value` is ok, but when I add the method (`indexOf(String)`) to this code it doesn't work. Why there is no exception for only accessing the `.value` if there is no value?

Anyway thank you guys for the discussion.

Edit: @Jack_Conradson I know, you can add an information to the description of Script Aggregation that nulls are handled directly by aggregation and it is ok to return null, but in any other case you have to carry about them.

---

<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 14, 2017, 9:17pm UTC](https://discuss.elastic.co/t/working-with-strings-in-painless/107896/7 "2017-12-14T21:17:57Z")

</div>

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