When trying to store a handlebars template in the .scripts index the template is validated using mustache which leads to a syntax error.
curl -X POST -d '{{
"lang": "handlebars",
"inline": "{\n \"size\": 0,\n {{#if filters}}\n \"query\": {\n \"filtered\": {\n \"filter\": {\n \"bool\": {\n \"must\": [\n {{#each filters}}\n {{#if @index}},
{{/if}}\n {{#if value}}\n { \"term\": { \"{{field}}\": \"{{value}}\" } }\n {{/if}}\n {{#if values}}\n { \"terms\": { \"{{field}}\": [ {{#each values}}{{#if
@index}},{{/if}}\"{{this}}\"{{/each}} ] } }\n {{/if}}\n {{/each}}\n ]\n }\n }\n }\n },\n {{/if}}\n {{#each aggregationTerms}}\n {{#if @index}},{{/if}}\n \"aggs
\": {\n \"{{this}}\": {\n \"terms\": {\n \"field\": \"{{this}}\"\n }\n {{/each}}\n {{#each aggregationTerms}}\n }\n }\n {{/each}}\n}\n",
"params": {
"filters": [ { "field": "foo", "values": ["bar"] } ],
"aggregationTerms": [ "lorum", "epsum" ]
}
}
}' http://elastic.local:9200/test/test/_search/template
curl -X POST -d '{{
"lang": "handlebars",
"template": "{\n \"size\": 0,\n {{#if filters}}\n \"query\": {\n \"filtered\": {\n \"filter\": {\n \"bool\": {\n \"must\": [\n {{#each filters}}\n {{#if @index}},
{{/if}}\n {{#if value}}\n { \"term\": { \"{{field}}\": \"{{value}}\" } }\n {{/if}}\n {{#if values}}\n { \"terms\": { \"{{field}}\": [ {{#each values}}{{#if
@index}},{{/if}}\"{{this}}\"{{/each}} ] } }\n {{/if}}\n {{/each}}\n ]\n }\n }\n }\n },\n {{/if}}\n {{#each aggregationTerms}}\n {{#if @index}},{{/if}}\n \"aggs
\": {\n \"{{this}}\": {\n \"terms\": {\n \"field\": \"{{this}}\"\n }\n {{/each}}\n {{#each aggregationTerms}}\n }\n }\n {{/each}}\n}\n"
}' http://elastic.local:9200/_search/template/test_template
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Unable to parse [{\n \"size\": 0,\n {{#if filters}}\n \"query\": {\n \"filtered\": {\n \"filter\": {\n \"bool\": {\n \"must\": [\n {{#each filters}}\n {{#if @index}}, {{/if}}\n {{#if value}}\n { \"term\": { \"{{field}}\": \"{{value}}\" } }\n {{/if}}\n {{#if values}}\n { \"terms\": { \"{{field}}\": [ {{#each values}}{{#if @index}},{{/if}}\"{{this}}\"{{/each}} ] } }\n {{/if}}\n {{/each}}\n ]\n }\n }\n }\n },\n {{/if}}\n {{#each aggregationTerms}}\n {{#if @index}},{{/if}}\n \"aggs\": {\n \"{{this}}\": {\n \"terms\": {\n \"field\": \"{{this}}\"\n }\n {{/each}}\n {{#each aggregationTerms}}\n }\n }\n {{/each}}\n}\n] lang [mustache]"}],"type":"illegal_argument_exception","reason":"Unable to parse [{\n \"size\": 0,\n {{#if filters}}\n \"query\": {\n \"filtered\": {\n \"filter\": {\n \"bool\": {\n \"must\": [\n {{#each filters}}\n {{#if @index}}, {{/if}}\n {{#if value}}\n { \"term\": { \"{{field}}\": \"{{value}}\" } }\n {{/if}}\n {{#if values}}\n { \"terms\": { \"{{field}}\": [ {{#each values}}{{#if @index}},{{/if}}\"{{this}}\"{{/each}} ] } }\n {{/if}}\n {{/each}}\n ]\n }\n }\n }\n },\n {{/if}}\n {{#each aggregationTerms}}\n {{#if @index}},{{/if}}\n \"aggs\": {\n \"{{this}}\": {\n \"terms\": {\n \"field\": \"{{this}}\"\n }\n {{/each}}\n {{#each aggregationTerms}}\n }\n }\n {{/each}}\n}\n] lang [mustache]","caused_by":{"type":"mustache_exception","reason":"Mismatched start/end tags: if @index != if in query-template:10"}},"status":400}
lang [mustache]","caused_by":{"type":"mustache_exception","reason":"Mismatched start/end tags: if @index != if in query-template:10"}
{
"size": 0,
{{#if filters}}
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{{#each filters}}
{{#if @index}}, {{/if}}
{{#if value}}
{ "term": { "{{field}}": "{{value}}" } }
{{/if}}
{{#if values}}
{ "terms": { "{{field}}": [ {{#each values}}{{#if @index}},{{/if}}"{{this}}"{{/each}} ] } }
{{/if}}
{{/each}}
]
}
}
}
},
{{/if}}
{{#each aggregationTerms}}
{{#if @index}},{{/if}}
"aggs": {
"{{this}}": {
"terms": {
"field": "{{this}}"
}
{{/each}}
{{#each aggregationTerms}}
}
}
{{/each}}
}