[NOT POSSIBLE] Yet another how to pre-register a "pretty printed" search template with conditional clauses?

hello All,

i have to maintain pretty big search templates. Now, I'd like to extend them by adding conditional clauses, as described here, it should be possible with pre-registered templates. But I ran into the same issue.
Maybe someone knows, how to escape the following code in a "pretty-printed" template in Kibana without having to escape the whole query:

POST _scripts/searchPage
...
  "filter": [
        {{#domain}}
            {
              "term": {
                "domain": "{{domain}}"
              }
            },
        {{/domain}}
...

Thanks a lot!

it would be good to update the documentation:

As written above, this template is not valid JSON because it includes the section markers like {{#line_no}} . For this reason, the template should either be stored in a file (see Pre-registered templateedit) or, when used via the REST API, should be written as a string

as the files scripts were removed in 6.0

So, it seems, the only way to use the section markers in templates, is to escape the whole thing, which increases the maintenance efforts.

Do you mean to replace

be stored in a file

by

be stored in elasticsearch

yes, or maybe, "in a file" should be replace by "in the cluster state"

Thanks for reporting. I opened:

1 Like

FYI, as a workaround, for this particular case, I'll be using the prefix query w/o section markers:

...
  "filter": [
        {
          "prefix": {
            "domain": {
              "value": "{{domain}}"
            }
          }
        }
...

which will work as expected, even when the domain parameter is not set.

@dadoonet I made a comment in your pull request because I think there is still confusion.

It's now impossible to push invalid json templates into ES because the only way to do that was via files and this options is now gone.
So even the new wording is wrong and confusing.

There is now only 1 option when the template becomes invalid JSON because of mustache syntax and it is to put all of it in a string, escape it so it's valid and then push it via the API.

You can't push invalid JSON in the body of the POST to _scripts/* so when files went away, it left only 1 option.

1 Like

I see. That's true. Thanks for the comments.

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