# How do you preregister a search template with conditional clauses?

**URL:** <https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768>\
**Category:** Elasticsearch\
**Created:** [June 13, 2018, 4:45pm UTC](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768 "2018-06-13T16:45:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashokc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashokc/32/11892_2.png) [@ashokc](https://discuss.elastic.co/u/ashokc)\
**Post date:** [June 13, 2018, 4:45pm UTC](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768/1 "2018-06-13T16:45:44Z")

</div>

We use mustache search templates that have conditional clauses (those # symbols). Worked fine in 5.x when we could simply dump them to config/scripts folder... and ES would reload them pretty quick

Does not look like this file option is supported any more... 6.2.4 does not seem to process this folder... Is that correct or am I missing something?

And if we try to pre-register these templates, these '#' symbols cause issues... even when we escape the whole template source to be a String...

The documentation says that:

> 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:

This question was asked earlier by Jack\_Fazackerley but was closed for no answers...

> [@Conditional Clauses not working with Pre-registered template](https://discuss.elastic.co/t/conditional-clauses-not-working-with-pre-registered-template/117094):
>
> Hi, From the [Docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#_conditional_clauses) it says that using conditional clauses will cause JSON to be invalid so the JSON would have to be a string but it also says that Pre-registered templates can be used instead with no note of it needing to be converted to a string. Am I misinterpreting this? If not, how is it possible to use a pre-registered template with conditional clauses? Each time I try and create a template I get the following in response: { "error": { "root\_cause": [ { "type": "parsi…

There is no path forward for these templates then in 6.x?

Thank you

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 13, 2018, 6:57pm UTC](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768/2 "2018-06-13T18:57:48Z")

</div>

Could you send us your query template?  
I faced this issue in the past, and i noticed the conditional clauses must be properly formatted.

---

<div class="post-metadata">

**Author:** ![ashokc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashokc/32/11892_2.png) [@ashokc](https://discuss.elastic.co/u/ashokc)\
**Post date:** [June 13, 2018, 10:17pm UTC](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768/3 "2018-06-13T22:17:21Z")

</div>

I think you are on to somethig there klof. Mucking with the format, I was able to put a simple template into cluster state. My real templates are hundreds of lines long so no luck there yet.

Here is the simple template that I was able to massage.

```
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match" : {
          "custom_all" : "{{queryString}}"
        }
      }
      {{#from}}
        ,"from": "{{from}}"
      {{/from}}
      {{^from}}
        ,"from": "0"
      {{/from}}
      {{#nresults}}
        ,"size": "{{nresults}}"
      {{/nresults}}
      {{^nresults}}
        ,"size": "100"
      {{/nresults}}
    }
  }
}

```

I can put it in cluster state if and only if

- Escape the whole "source" object so we get a String value for "source" element

- And turn the whole thing into a single line (remove all new lines)

Surprised by the 2nd requirement.

With those in place, I can run something like

`curl -X POST -H 'Content-Type: application/json; charset=UTF-8' http://localhost:9200/_scripts/testing_conditionals_escaped_oneline -d @./scripts/testing_conditionals_escaped_oneline.mustache`

---

<div class="post-metadata">

**Author:** ![ashokc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashokc/32/11892_2.png) [@ashokc](https://discuss.elastic.co/u/ashokc)\
**Post date:** [June 14, 2018, 1:15am UTC](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768/4 "2018-06-14T01:15:59Z")

</div>

As for that surprise 2nd requirement, it turns out I had some hidden tabs in the old template files that were messing up. Removing those took care of it. No need to remove new lines.

Thx for the pointer klof!

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 14, 2018, 6:53am UTC](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768/5 "2018-06-14T06:53:17Z")

</div>

Yes exactly, remove all the new lines helps.  
But, it's not the proper way to do it, especially when you have to maintain a huge template.  
you should be able to insert the template "pretty\_printed".  
Do you mind to send your big template in a gist file?

---

<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:** [July 12, 2018, 6:53am UTC](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768/6 "2018-07-12T06:53:20Z")

</div>

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