Elasticsearch: Split doesn't work in painless

Created on 23 Aug 2017  路  4Comments  路  Source: elastic/elasticsearch

Given the following input string
'http://hostname.domain'
I need to split it into two parts: 'http:' and 'hostname.domain'

However, when executing the following code

String urlAsString=String.valueOf(ctx.url); 
ctx.domainAndHost=urlAsString.split('//')[1]

I do encounter the following error message

{
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "... ); ctx.domain=urlAsString.split('//')[0]",
          "                             ^---- HERE"
        ],
        "script": "String urlAsString=String.valueOf(ctx.url); ctx.domain=urlAsString.split('//')[0]",
        "lang": "painless",
        "header": {
          "processor_type": "script",
          "property_name": "inline"
        }
      }

Most helpful comment

String#split is not whitelisted because it creates dynamic Pattern objects internally. Use Pattern#split instead like /\//.split(urlAsString[0]).

All 4 comments

String#split is not whitelisted because it creates dynamic Pattern objects internally. Use Pattern#split instead like /\//.split(urlAsString[0]).

@nik9000 ,
Thank you for your response. Appreciate it =)

This issue is closed and a bit old, but I have a question regarding Patterns. For my painless script, the pattern is defined through params. Is it possible for me to create a Pattern object from a string? The url you provided to the API docs lists String pattern() (java 9), however all attempts to use this causes compile errors similar to Caused by: java.lang.IllegalArgumentException: Unknown call [pattern] with [1] arguments.

@synthesize Please ask questions on our forum. We use github for feature requests and confirmed bug reports. Comments on a closed issue are likely to be overlooked or ignored.

Was this page helpful?
0 / 5 - 0 ratings