Search template does not work

Hello All,
I am trying to use search template with REST endpoint : GET /_search/template,and it does work.
for instance:
{
"inline": {
"query": {
"match": {
"title": "{{query_string}}"
}
}
},
"params": {
"query_string": "search for these words"
}
}

But when it comes to condition clause:
{
"query": {
"bool": {
"must": {
"match": {
"line": "{{text}}"
}
},
"filter": {
{{#line_no}}
"range": {
"line_no": {
{{#start}}
"gte": "{{start}}"
{{#end}},{{/end}}
{{/start}}
{{#end}}
"lte": "{{end}}"
{{/end}}
}
}
{{/line_no}}
}
}
}
}
the above template is not valid JSON formate ,so I change it to string ,as the doc showes (https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-template.html#_more_template_examples),and the complete request is as follow:
GET /_search/template
{
"inline": "{"query":{"bool":{"must":{"match":{"line":"{{text}}"}},"filter":{{{#line_no}}"range":{"line_no":{{{#start}}"gte":"{{start}}"{{#end}},{{/end}}{{/start}}{{#end}}"lte":"{{end}}"{{/end}}}}{{/line_no}}}}}}",
"params": {}
}
but the result tells error:Failed to compile inline script,Improperly closed variable in query-template:1

however when I store the template as tmpl.mustache under config/scripts/ on elasticsearch server,and reference the template file by it's name,then it works correctly.
Anyone has encountered the confusing problem? Thanks for your sharing!

1 Like