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

**URL:** <https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144>\
**Category:** Elasticsearch\
**Created:** [July 5, 2019, 5:19pm UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144 "2019-07-05T17:19:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jetnet](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jetnet](https://discuss.elastic.co/u/jetnet)\
**Post date:** [July 5, 2019, 5:19pm UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/1 "2019-07-05T17:19:45Z")

</div>

hello All,

i have to maintain pretty big search templates. Now, I'd like to extend them by adding conditional clauses, as described [here](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-template.html), it should be possible with pre-registered templates. But I ran into the same [issue](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768/3).  
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

```

```auto
...
  "filter": [
        {{#domain}}
            {
              "term": {
                "domain": "{{domain}}"
              }
            },
        {{/domain}}
...

```

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![jetnet](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jetnet](https://discuss.elastic.co/u/jetnet)\
**Post date:** [July 6, 2019, 7:21am UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/2 "2019-07-06T07:21:33Z")

</div>

it would be good to update the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#pre-registered-templates):

> 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 template](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#pre-registered-templates)[edit](https://github.com/elastic/elasticsearch/edit/7.2/docs/reference/search/search-template.asciidoc)) or, when used via the REST API, should be written as a string

as the files scripts were [removed in 6.0](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/breaking_60_scripting_changes.html#_file_scripts_removed)

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 6, 2019, 7:39am UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/3 "2019-07-06T07:39:29Z")

</div>

Do you mean to replace

> be stored in a file

by

> be stored in elasticsearch

---

<div class="post-metadata">

**Author:** ![jetnet](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jetnet](https://discuss.elastic.co/u/jetnet)\
**Post date:** [July 6, 2019, 8:42am UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/4 "2019-07-06T08:42:33Z")

</div>

yes, or maybe, "_in a file_" should be replace by "_in the cluster state_"

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 6, 2019, 9:48am UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/5 "2019-07-06T09:48:12Z")

</div>

Thanks for reporting. I opened:

> <https://github.com/elastic/elasticsearch/pull/44038>

---

<div class="post-metadata">

**Author:** ![jetnet](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jetnet](https://discuss.elastic.co/u/jetnet)\
**Post date:** [July 6, 2019, 10:50am UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/6 "2019-07-06T10:50:38Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![martinr\_ubi](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@martinr\_ubi](https://discuss.elastic.co/u/martinr_ubi)\
**Post date:** [July 8, 2019, 9:48pm UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/7 "2019-07-08T21:48:52Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 8, 2019, 10:16pm UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/8 "2019-07-08T22:16:37Z")

</div>

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

---

<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:** [August 5, 2019, 10:16pm UTC](https://discuss.elastic.co/t/not-possible-yet-another-how-to-pre-register-a-pretty-printed-search-template-with-conditional-clauses/189144/9 "2019-08-05T22:16:41Z")

</div>

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