【问题标题】:Date Range Query using Search Template in Elasticsearch在 Elasticsearch 中使用搜索模板进行日期范围查询
【发布时间】:2019-01-06 13:29:30
【问题描述】:

我们在使用 Elasticsearch 中的搜索模板构建日期范围查询时遇到问题。它工作正常,只有一个条件子句,但是当提供多个条件时,我们会收到以下错误。

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

错误

{
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,
}

查询:

{ "id": "日期模板", “参数”:{ “请求参数”: ”*” } }

同样适用于这个模板:

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

查询

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

【问题讨论】:

    标签: elasticsearch date-range elasticsearch-template


    【解决方案1】:

    首先,我建议你用三引号重写你的模板,因为它更容易阅读和维护,像这样:

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

    然后,调用该查询的正确方法是这样的(即您的 params 对象中缺少 from 变量):

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

    【讨论】:

    • 谢谢,这很好,当我们也有多个范围查询时。但它应该适用于 OR 条件。所以,我在 Should 子句中添加了相同的内容。但这并没有按预期工作。我们额外得到一个“逗号”,但不知道如何以数组格式给出它。更清楚地说,必须有一个查询字符串和一个术语查询。 should 子句应该有这个日期范围查询。你能帮忙吗?
    • 好的,请随时提出新问题,我们会在那里解决
    • 谢谢,新问题的链接:stackoverflow.com/questions/54869985/…
    猜你喜欢
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多