We are facing issue while framing date range query using search template in Elasticsearch. It is working fine, with one conditional clause, but when multiple conditions are provided, we are getting following error.
{
"script": {
"lang": "mustache",
"source": "{
\"query\":{
\"bool\":{
\"must\":[
{{#since}}
{\"range\":
{\"@timestamp\":
{
{{#from}}\"from\":\"{{from}}\"{{/from}}
}
}
},{{/since}}
{\"query_string\":
{
\"query\":\"(title:({{query_string}}))\"
}
}
]
}
}
}"
}
}
Error:
{
error: {
root_cause: [
{
type: "general_script_exception",
reason: "Failed to compile stored script [dateTemplate] using lang [mustache]",
}
],
type: "general_script_exception",
reason: "Failed to compile stored script [dateTemplate] using lang [mustache]",
caused_by: {
type: "mustache_exception",
reason: "Improperly closed variable in query-template:1",
},
},
status: 500,
}
Query:
{ "id": "dateTemplate", "params": { "query_string": "*" } }
Same is working fine for this Template:
{
"script": {
"lang": "mustache",
"source": "{\"query\":{\"bool\":{\"must\":[{{#since}}{\"range\": {\"@timestamp\": {\"from\": \"{{since}}\"}}},{{/since}}{\"query_string\":{\"query\":\"(title:({{query_string}}))\"}}]}}}"
}
}
Query
{
"id": "date",
"params": {
"query_string": "*",
"since": "2018-07-23"
}
}