Painless : Use variable as regex


I'm starting to use painless script in my ES queries and I'm currently struggled to convert a parameter or a variable to a Pattern.

GET ressources/_search

  "query": {
    "bool": {
      "must": {
        "script": {
          "script": {
            "inline": "
              boolean find = false; 
              for( doc in params['_source']['documents']) {
                Pattern patt = /params.value/;
                if ( doc.label =~ /patt/ )  { 
                  find = true;
              }  return find;",
            "lang": "painless",
            "params": {
              "value": "title"

So with this script my variable patt has value "/params.value/" instead of "/title/".
I tried to use def patt = ~params.value; like suggested in Groovy doc but it's not worked too.

Anyone as a suggestion?

Thanks !

1 Like

This is not supported:

Regular expression constants are directly supported. To ensure fast performance, this is the only mechanism for creating patterns. Regular expressions are always constants and compiled efficiently a single time.

Oh I missed that.
Thx for your answer !

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