我们在使用Elasticsearch中的搜索模板来构建日期范围查询时遇到问题。使用一个条件子句,它可以正常工作,但是当提供多个条件时,我们将遇到以下错误。
myConnection.Open()
Dim str As String = "SELECT fullname' FROM STUDENTS"
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(dr)
ListBox1.DataSource = dt
myConnection.Close()
错误:
{
"script": {
"lang": "mustache",
"source": "{
\"query\":{
\"bool\":{
\"must\":[
{{#since}}
{\"range\":
{\"@timestamp\":
{
{{#from}}\"from\":\"{{from}}\"{{/from}}
}
}
},{{/since}}
{\"query_string\":
{
\"query\":\"(title:({{query_string}}))\"
}
}
]
}
}
}"
}
}
查询:
{ “ id”:“ dateTemplate”, “参数”:{ “请求参数”: ”*” } }
对于此模板,Same工作正常:
{
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,
}
查询
{
"script": {
"lang": "mustache",
"source": "{\"query\":{\"bool\":{\"must\":[{{#since}}{\"range\": {\"@timestamp\": {\"from\": \"{{since}}\"}}},{{/since}}{\"query_string\":{\"query\":\"(title:({{query_string}}))\"}}]}}}"
}
}
答案 0 :(得分:0)
首先,我建议您使用三引号将模板重写,因为这样更易于阅读和维护:
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"
}
}
}