# Check if a field string start with a determinate string

**URL:** <https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612>\
**Category:** Kibana\
**Created:** [November 21, 2017, 8:05pm UTC](https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612 "2017-11-21T20:05:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mad-max](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@mad-max](https://discuss.elastic.co/u/mad-max)\
**Post date:** [November 21, 2017, 8:05pm UTC](https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612/1 "2017-11-21T20:05:04Z")

</div>

hello to everyone,  
i want to know how to create a screpted fields. the field is a string and I have to check if it starts with edi. if so the value of the field e EDI else "-".

how can I do. I need some help because I'm completely blocked.  
thank you so much

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [November 21, 2017, 8:20pm UTC](https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612/2 "2017-11-21T20:20:44Z")

</div>

Try this:

```auto
def path = doc['myField'].value;
if (path != null) {
    int loc = path.indexOf('edi');
    if (loc == 0) {
      return 'EDI';
    }
}
return null;

```

The `myField` field must be a keyword type (or use `myField.keyword` if you have the multi-field).

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/1/71b75ca2c4d8b2c7cd6101fce436cbac72b70c02.png)

Personally, if the field is going to either be a certain value or not, I would probably return Booleans from my script, and call the field `isEdi`

---

<div class="post-metadata">

**Author:** ![mad-max](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@mad-max](https://discuss.elastic.co/u/mad-max)\
**Post date:** [November 21, 2017, 8:35pm UTC](https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612/3 "2017-11-21T20:35:51Z")

</div>

HI it is perfect. Can you explain

```
 int loc = path.indexOf('edi');
    if (loc == 0) {
      return 'EDI';

```

so i can lear, i don't know the function that you used to solve my problem.

thank you so much  
M

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [November 21, 2017, 8:42pm UTC](https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612/4 "2017-11-21T20:42:31Z")

</div>

Sure.

`indexOf` is a method of the string variable type that takes a substring (`edi`) and returns back a number for the index of where that substring begins.

So if the string actually starts with `edi`, then the index of that substring will be 0, because the index value is 0-based. If the string doesn't contain that substring at all, then `indexOf` will return a `-1`.

When we find that the index of the substring is 0, then we return a value that becomes the value for the scripted field (`EDI`). If anything else happens, we return a `null`.

---

<div class="post-metadata">

**Author:** ![mad-max](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@mad-max](https://discuss.elastic.co/u/mad-max)\
**Post date:** [November 21, 2017, 9:02pm UTC](https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612/5 "2017-11-21T21:02:15Z")

</div>

Fantastic, thank you so much. un other question, where i find documentation of a method as IndexOf.

thank you so much 😀

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [November 21, 2017, 9:48pm UTC](https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612/6 "2017-11-21T21:48:19Z")

</div>

Painless is based on Groovy and is pretty much a subset of it. It restricts usage of the insecure methods and libraries, but usually you can refer to Groovy documentation to figure out how to do something:

[http://docs.groovy-lang.org/latest/html/documentation/index.html#\_manipulating\_lists](http://docs.groovy-lang.org/latest/html/documentation/index.html#_manipulating_lists)

---

<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, 2017, 9:48pm UTC](https://discuss.elastic.co/t/check-if-a-field-string-start-with-a-determinate-string/108612/7 "2017-12-19T21:48:40Z")

</div>

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