Substring and contains in painless

i have a internal uri which has one of values like /appname/api/v4/bdq/rules/ i want to filter them a nd extract only field v4 from that url using substring function

1 Like


please dont add a second post only 5 hours after posting the first, just wait for an answer and bump the same post after some time. Note that this forum does not come with an SLA, yet we try to answer as many questions as possible :slight_smile:

So in this case, you can take a look at painless regular expressions and use split to extract that part from the URL. See

hope this helps!


I dont see split function working for me.

a bit more information would be useful, like the error you are getting and also the input you are using it on.

i have an endpoint uri which looks like /appname/api/v4/bdq/rules/

1)extract v* from all uri and display their counts

I asked for more information, your second reply does not contain any error messages or how the split function is not working - can you elaborate? I know what you want to do, but I would like to see some code that shows how you are using the split function.

looking for something like this

def path = doc['internalUri.keyword'].contains('v*');
/which will retrieve all paths which contains word v3,v2,v1/
if (path != null) {
return path.Substring(1,23);
abstract only v3 from path
return "";
but not working


"root_cause": [
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"return path.Substring(1,23);\n }\n",
" ^---- HERE"
"script": "def path = doc['internalUri.keyword'].contains('v3');\nif (path != null) {\n return path.Substring(1,23);\n }\nreturn "";",
"lang": "painless"

again, the context is important. Please provide a full reprodicble example. I have no idea in which context this script is used. If it is a script query, it should only return a boolean. Therefore the full stack trace is important.

Sorry Alex it is a painless script which was found in documentation


what i was trying to accomplish was get all strings which was word v3 in parsed field which has value like
.... /appname/api/v3/bdq/rules/

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