# Regular expression as a parameter in painless

**URL:** <https://discuss.elastic.co/t/regular-expression-as-a-parameter-in-painless/128838>\
**Category:** Elasticsearch\
**Created:** [April 20, 2018, 9:10am UTC](https://discuss.elastic.co/t/regular-expression-as-a-parameter-in-painless/128838 "2018-04-20T09:10:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [April 20, 2018, 9:10am UTC](https://discuss.elastic.co/t/regular-expression-as-a-parameter-in-painless/128838/1 "2018-04-20T09:10:05Z")

</div>

```
   {
   "query": {
      "bool": {
         "must": [
            {
               "query_string": {
                  "query": "a*",
                  "quote_analyzer": "standard",
                  "allow_leading_wildcard": "true",
                  "analyze_wildcard": "true",
                  "default_operator": "AND",
                  "phrase_slop": 1
               }
            },
            {
               "script": {
                  "script": {
                     "source": "Pattern p = /\\s?a\\S*?/i; p.matcher(params._source.summary ?: '').find();",
                     "lang": "painless"
                  }
               }
            }
         ]
      }
   }
}

```

I want the above query to parameterize because sometimes I am getting an error

> [script] Too many dynamic script compilations within one minute, max: [15/min]; please use on-disk, indexed, or scripts with parameters instead; this limit can be changed by the [script.max\_compilations\_per\_minute]

If we go for index script or file-based script will it help to remove this error?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [April 20, 2018, 6:51pm UTC](https://discuss.elastic.co/t/regular-expression-as-a-parameter-in-painless/128838/2 "2018-04-20T18:51:04Z")

</div>

Regexes are compile time constants because allowing them to be defined at runtime would mean regex compiling _per document_ (ie possibly millions of regex compilations).

While it is not advised and should be used with care, especially with the very short prefix you are using, why not use the [prefix query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html)?

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [April 21, 2018, 5:44am UTC](https://discuss.elastic.co/t/regular-expression-as-a-parameter-in-painless/128838/3 "2018-04-21T05:44:41Z")

</div>

`NOTE: all the queries are saved in the percolator and we are doing a search in percolator`

I use this script to support wildcard searches of all combination

> aus \* gov\*  
> polic\*  
> \*alia \*ment

Like these all combination

If I use [Prefix query | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html) does it help me to solve these requirements. I was using **ngrams and edge-ngrams** , but uses lot of spaces.

If I increase `script.max_compilations_per_minute` to `1000` or more, of-course it will impact the performance but at what level?

---

<div class="post-metadata">

**Author:** ![idmanish](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@idmanish](https://discuss.elastic.co/u/idmanish)\
**Post date:** [April 21, 2018, 6:43am UTC](https://discuss.elastic.co/t/regular-expression-as-a-parameter-in-painless/128838/4 "2018-04-21T06:43:54Z")

</div>

I am also facing "max\_compilations\_per\_minute" error.  
This is mainly happening due to painless script we are using in in search queries registered as percolator.  
re-indexing of data is not an option for us.  
How can we avoid this circuit break exception happening mainly due to "max\_compilations\_per\_minute".

Alternative or worst solution:-  
Is there any max limit for setting "max\_compilations\_per\_minute" recommended.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 19, 2018, 6:44am UTC](https://discuss.elastic.co/t/regular-expression-as-a-parameter-in-painless/128838/5 "2018-05-19T06:44:09Z")

</div>

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