How to use Mustache script in percolator

we are using Mustache script in the query to use params, since script contains Pattern and painless doesn't support parameterizing Pattern.

query:

PUT test_index/notification/ID
{
    "source": {
        "query": {
            "bool": {
                "must": [
                    {
                        "bool": {
                            "must": {
                                "bool": {
                                    "should": [
                                        {
                                            "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 = {{param_0}}; p.matcher(params._source.summary ?: '').find();",
                                                                "lang": "painless"
                                                            }
                                                        }
                                                    }
                                                ]
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }
                ]
            }
        },
        "sort": [
            {
                "headline.case_insensitive_sort_raw": "asc"
            }
        ]
    },
    "params": {
        "param_0": "/\\s?a\\S*?/i"
    }
}

and percolator search is:

GET test_index/_search
{
  "query": {
    "percolate": {
      "field": "query",
      "document_type": "doctype",
      "index": "document_index_testing",
      "type": "mediaitems",
      "id": "262587721"
    }
    
  }
}

and it is not matching. If the percolator query using painless, it works fine.

is there any way to make Mustache script works in Percolator

1 Like

Your percolate example isn't actually using the template you defined. To do this, you would need to include percolate inside your template query, and have your search use the template.

However, regarding your use of painless, this is going to be very inefficient and most likely hit default compilation limits. While it will be similarly inefficient, have you tried the regex query?

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