How do you preregister a search template with conditional clauses?

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