Can java.util.regex.Pattern be used in a painless script?

Is there a way to use java.util.regex.Pattern in a painless script? I get an error:

curl -i -XPOST http://localhost:9200/_scripts/painless/_execute -H 'Content-type: application/json' --data-binary '{"script": {"source": "return java.util.regex.Pattern.matches(\"a*b\",\"aaab\");", "params": { "test":1 } } }'

HTTP/1.1 500 Internal Server Error
content-type: application/json; charset=UTF-8
content-length: 668

{"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["... n java.util.regex.Pattern.matches(\"a*b\",\"aaab\");","                             ^---- HERE"],"script":"return java.util.regex.Pattern.matches(\"a*b\",\"aaab\");","lang":"painless"}],"type":"script_exception","reason":"compile error","script_stack":["... n java.util.regex.Pattern.matches(\"a*b\",\"aaab\");","                             ^---- HERE"],"script":"return java.util.regex.Pattern.matches(\"a*b\",\"aaab\");","lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"method [java.util.regex.Pattern, matches/2] not found"}},"status":500}

Other java.util classes seem to work:

curl -i -XPOST http://localhost:9200/_scripts/painless/_execute -H 'Content-type: application/json' --data-binary '{"script": {"source": "new java.util.BitSet(10).length();", "params": { "test":1 } } }'

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 14

{"result":"0"}

I know there is built-in regex support but that requires the pattern be a literal, hard-coded string. A native script I'm trying to port to painless needs to use a regex pattern string passed as a parameter.

please see https://www.elastic.co/guide/en/elasticsearch/painless/7.5/painless-walkthrough.html#modules-scripting-painless-regex - especially about enabling regexes and how regexes are used. Pattern.compile is not included intentionally.

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