Painless : Use variable as regex


#1

Hi.

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 !


(Adrien Grand) #2

This is not supported: https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-syntax.html#patterns

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.


#3

Oh I missed that.
Thx for your answer !


(system) #4

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