elasticsearchdate-rangeelasticsearch-template

Date Range Query using Search Template in Elasticsearch


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"
  }
}

Solution

  • First of all, I suggest you rewrite your template using triple quotes, as it's much easier to read and maintain, like this:

    POST _scripts/dateTemplate
    {
      "script": {
        "lang": "mustache",
        "source": """
          {
            "query": {
              "bool": {
                "must": [
                  {{#since}}
                  {
                    "range": {
                      "@timestamp": {
                        {{#from}}"from": "{{from}}"{{/from}}
                      }
                    }
                  },
                  {{/since}}
                  {
                    "query_string": {
                      "query": "(title:({{query_string}}))"
                    }
                  }
                ]
              }
            }
          }
        """
      }
    }
    

    Then, the correct way to invoke that query is like this (i.e. you're missing the from variable in your params object):

    {
      "id": "dateTemplate",
      "params": {
        "query_string": "*",
        "since": {
          "from": "2018-07-23"
        }
      }
    }