Comma issues in mustache conditional block elasticsearch

48 views Asked by At

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.

1

There are 1 answers

4
Val On

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:

  {{#queries}}
    {{#query_string}}
      {\"multi_match\":{
         \"query\":\"{{value}}\",
         \"fields\": [
            \"cdm_cours_2_title.text^5\",
            \"cdm_cours_4_resume.text\", 
            \"cdm_cours_5_description.text\"
         ]
      }}
      {{^last}},{{/last}}
    {{/query_string}}
    {{#title}}
      {\"match\":{\"cdm_cours_2_title.text\":\"{{value}}\"}}
      {{^last}},{{/last}}
    {{/title}}
    {{#resume}}
      {\"match\":{\"cdm_cours_4_resume.text\":\"{{value}}\"}}
      {{^last}},{{/last}}
    {{/resume}}
    {{#description}}
      {\"match\":{\"cdm_cours_5_description.text\":\"{{value}}\"}}
      {{^last}},{{/last}}
    {{/description}}
  {{/queries}}

Then you need to invoke your query like this:

POST index/_search/template
{
  "id": "my-query",
  "params": {
    "queries": [
      {
        "query_string": {
          "value": "my-query-string"
        }
      },
      {
        "title": {
          "value": "my-title"
        }
      },
      {
        "resume": {
          "value": "my-resume"
        }
      },
      {
        "description": {
          "value": "my-description",
          "last": true                    <--- the last condition has last = true
        }
      }
    ]
  }
}

You can specify any number of conditions and in any order, but the last one MUST have last: true specified.