I'm building a search template with mustache for elasticsearch with a bool query but I've got issues with my conditional block
I've code this part of the template :
\"must\":[
{{#query_string}}
{\"multi_match\":{
\"query\":\"{{query_string}}\",
\"fields\": [
\"cdm_cours_2_title.text^5\",
\"cdm_cours_4_resume.text\",
\"cdm_cours_5_description.text\"
]
}},{{/query_string}}
{{#title}}
{\"match\":{\"cdm_cours_2_title.text\":\"{{title}}\"}},
{{/title}}
{{#resume}}
{\"match\":{\"cdm_cours_4_resume.text\":\"{{resume}}\"}},
{{/resume}}
{{#description}}
{\"match\":{\"cdm_cours_5_description.text\":\"{{description}}\"}}
{{/description}}
]
}},
But the issue is that I want to be able to set values for one, none or multiple parameters of this query and the comma are messing things up. I don't know how I can do it correctly. In this configuration it doesn't work if you don't set the last field and I don't know how to handle that.
A common way of solving this is to make your template support an array of queries and specify which one is the
last
one.Here the template will loop through an array of
queries
and the comma will be added if and only if the query is NOT tagged as the last one in the array:Then you need to invoke your query like this:
You can specify any number of conditions and in any order, but the last one MUST have
last: true
specified.