Using Triple Quotes in Elastic Templates

I am using templating to do query and rescoring work. The rescoring happens based on couple of values that are index in the document. Due to the logic that needs to happen based on the value in the document I decided to use painless instead of ternary operator. Especially its hard to achieve the logic in ternary and also readability get really low.

I fallowed the documentation in elastic search to get an understanding of painless Elastic Painless Walkthrough

But when I render the template It complains at the end of the closing triple quotes. I was unable to really find a clear answer. This looks like clearly due to invalid JSON but according the elastic documentation this should be working. My elastic version is 6.1-6.2.

Stackoverflow has a similar question:
Stackoverflow Question

Kibana forum
Kibana Forum Triple Quotes

Here is the error I am getting when I am rendering:

parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 232, column 27

Here is the template:

{
  "query": {
    "rescore_query": {
      "function_score": {
        "boost_mode":"sum",
        "score_mode": "sum",
        "functions":[{
            "script_score" : {
              "script" : {
                  "lang":"painless",
                  {{#rescore.my_params.params}}
                    "params": {
                      "x": {{rescore.my_params.params.x}},
                      "y": {{rescore.my_params.params.y}},
                      "z": {{rescore.my_params.params.z}}
                    },
                  {{/rescore.my_params.params}}
                  {{#rescore.my_params.my_function}}
                    "source":"""
                      double doc_x = 0.0;
                      double doc_y = 0.0;
                      if (doc['doc_x'].empty) {
                          doc_x = 0.0;
                      } else {
                          doc_x = doc['doc_x'].value;
                      }
                      if (doc['doc_y'].empty) {
                          doc_y = params.y;
                      } else {
                          doc_y = doc['doc_y'].value;
                      }
                      double final_value = 2 * (doc_x + doc_y);
                      return final_value;
                    """ {{! here is where the error is getting thrown}}
                  {{/rescore.my_params.my_function}}
              }
            }
        }]
      }
    }

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