简体   繁体   中英

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

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 (ie you're missing the from variable in your params object):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM