# 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:** 1\
**Showing post:** 3

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

---

_[View the full topic](https://discuss.elastic.co/t/how-do-you-preregister-a-search-template-with-conditional-clauses/135768)._
