# Substring in painless

**URL:** <https://discuss.elastic.co/t/substring-in-painless/88660>\
**Category:** Elasticsearch\
**Created:** [June 8, 2017, 6:34am UTC](https://discuss.elastic.co/t/substring-in-painless/88660 "2017-06-08T06:34:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raghvendra](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@raghvendra](https://discuss.elastic.co/u/raghvendra)\
**Post date:** [June 8, 2017, 6:34am UTC](https://discuss.elastic.co/t/substring-in-painless/88660/1 "2017-06-08T06:34:26Z")

</div>

hi ..I am new to ELK stack . I want to create a new filed from a existing field created by logstash at the time of parising , by painless scripting.

this is my "request" field =\> "/gameserver\_pkr?websocket=1&&action=update\_score"  
I want to have a substring from index 0 to index of "?".  
I have appended =\> script.painless.regex.enabled: true into elasticsearch.yml file.  
Can anyone help ?  
scripted field code is-\>  
def String p = doc['request'].value;  
Def String App = p.subString(0, p.indexOf ('?'));  
return App;

---

<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:** [June 9, 2017, 3:24pm UTC](https://discuss.elastic.co/t/substring-in-painless/88660/3 "2017-06-09T15:24:46Z")

</div>

If you are trying to use a regex, I think it would look something like:

```auto
Matcher match = /([^\?]+).*/.matcher(doc['request'].value);
if (match.matches()) {
  return match.group(1);
} else {
  // something is wrong? the path is malformed, decide what to return or throw an error
}

```

---

<div class="post-metadata">

**Author:** ![raghvendra](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@raghvendra](https://discuss.elastic.co/u/raghvendra)\
**Post date:** [June 10, 2017, 1:11pm UTC](https://discuss.elastic.co/t/substring-in-painless/88660/4 "2017-06-10T13:11:05Z")

</div>

i have got it fixed .. i had to use request.keyword instead of request and rest was same.  
Thanks

---

<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 8, 2017, 1:11pm UTC](https://discuss.elastic.co/t/substring-in-painless/88660/5 "2017-07-08T13:11:14Z")

</div>

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