0

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"
  }
}
User1203
  • 77
  • 2
  • 11

1 Answers1

0

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"
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • Thanks, this is working fine, when we have multiple range queries also. But it should work with OR condition. So, I added the same to Should clause. But that is not working as expected. we are getting one "comma" additionally and am not sure how to give it in array format. To be more clear, must should have one query string anf terms query. Should clause should have this date range query. Can you please help on this? – User1203 Feb 25 '19 at 14:59
  • Sure, feel free to ask a new question and we'll solve it there – Val Feb 25 '19 at 15:01
  • Thanks, link to new question: https://stackoverflow.com/questions/54869985/date-range-query-using-searchtemplate-in-elasticsearch-with-should-clause – User1203 Feb 25 '19 at 15:52