在带有Should子句的Elasticsearch中使用SearchTemplate进行日期范围查询 [英] Date Range Query using SearchTemplate in Elasticsearch with Should clause

查看:447
本文介绍了在带有Should子句的Elasticsearch中使用SearchTemplate进行日期范围查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一种情况,我们必须使用 OR条件进行范围查询。
一次查询正常,但在多次查询触发时出错。

We have a scenario where we have to do range query with "OR" condition. It is working fine, with one query, but getting error while triggering with multiple query.

POST _scripts/dateTemplate
{
  "script": {
    "lang": "mustache",
    "source": """
        {
        "query": {
            "bool": {
                "must": {
                "query_string": {
                    "query": "title:({{searchkeyword}})"
                }
            },
            "filter": {
                "bool":{
                    "must":[
                        {
                            "match_all": {}
                        }
                        {{#f_url}} , {
                            "terms" : {
                                "f_url": [{{#toJson}}f_url{{/toJson}} ]
                                }
                            }
                        {{/f_url}}
                    ],
                    "should":[
                        {{#since}}
                            {
                                "range": {
                                    "{{since}}": {
                                        {{#from}}"from": "{{from}}"{{#to}},{{/to}}{{/from}}
                                        {{#to}}"to":"{{to}}"{{/to}}
                                    }
                                }
                            }
                        {{/since}}
                    ]
                }
            }
        }
    }
    }
"""
  }
}

调用模板时查询

POST _search/template
{
  "id": "dateTemplate",
  "params": {
    "searchkeyword": "*",
    "since":[ {
      "since":"@timestamp",
      "from": "2018-06-01"
    },
     {
      "since":"@timestamp",
      "from": "2018-06-08"
    }
    ]
  }
}

错误

{
  "error": {
    "root_cause": [
      {
        "type": "json_parse_exception",
        "reason": "Unexpected character ('{' (code 123)): was expecting comma to separate Array entries\n at [Source: \t\t{\n\t\t\"query\": {\n\t\t\t\"bool\": {\n\t\t\t\t\"must\": {\n\t\t\t\t\"query_string\": {\n\t\t\t\t\t\"query\": \"title:(*)\"\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"filter\": {\n\t\t\t\t\"bool\":{\n\t\t\t\t\t\"must\":[\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"match_all\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t],\n\t\t\t\t\t\"should\":[\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\"range\": {\n\t\t\t\t\t\t\t\t\t\"@timestamp\": {\n\t\t\t\t\t\t\t\t\t\t\"from\": \"2018-06-01\"\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\"range\": {\n\t\t\t\t\t\t\t\t\t\"@timestamp\": {\n\t\t\t\t\t\t\t\t\t\t\"from\": \"2018-06-08\"\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t}; line: 25, column: 9]"
      }
    ],
    "type": "json_parse_exception",
    "reason": "Unexpected character ('{' (code 123)): was expecting comma to separate Array entries\n at [Source: \t\t{\n\t\t\"query\": {\n\t\t\t\"bool\": {\n\t\t\t\t\"must\": {\n\t\t\t\t\"query_string\": {\n\t\t\t\t\t\"query\": \"title:(*)\"\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"filter\": {\n\t\t\t\t\"bool\":{\n\t\t\t\t\t\"must\":[\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"match_all\": {}\n\t\t\t\t\t\t}\n\t\t\t\t\t],\n\t\t\t\t\t\"should\":[\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\"range\": {\n\t\t\t\t\t\t\t\t\t\"@timestamp\": {\n\t\t\t\t\t\t\t\t\t\t\"from\": \"2018-06-01\"\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\"range\": {\n\t\t\t\t\t\t\t\t\t\"@timestamp\": {\n\t\t\t\t\t\t\t\t\t\t\"from\": \"2018-06-08\"\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t}; line: 25, column: 9]"
  },
  "status": 500
}

相同,如果我在must子句中添加它,则它在 AND条件下也可以正常工作。
您能帮忙用 AND和 OR条件来构架模板吗?

Same if I add it in must clause, it is working perfectly with "AND" condition. Can you please help in framing the template with both "AND" and "OR" Condition?

推荐答案

您快到了,您只需要让小胡子知道何时到达数组的最后一个元素即可。模板应如下所示(即,我们在每个元素之后添加逗号,最后一个除外):

You're almost there, you simply need to let mustache know when the last element of the array is reached. So your template should look like this (i.e. we add a comma after each element, except the last one):

  ...
                "should":[
                    {{#since}}
                        {
                            "range": {
                                "{{since}}": {
                                    {{#from}}"from": "{{from}}"{{#to}},{{/to}}{{/from}}
                                    {{#to}}"to":"{{to}}"{{/to}}
                                }
                            }
                        }{{^last}},{{/last}}                <-- modify this line
                    {{/since}}
                ]
  ...

然后只需修改您的调用以将 last 标志包括到最后一个元素 since 数组的值:

And then simply modify your call to include the last flag to the last element of the since array:

POST _search/template
{
  "id": "dateTemplate",
  "params": {
    "searchkeyword": "*",
    "since":[ {
      "since":"@timestamp",
      "from": "2018-06-01"
    },
     {
      "since":"@timestamp",
      "from": "2018-06-08",
      "last": true                                         <-- add this line
    }
    ]
  }
}

这篇关于在带有Should子句的Elasticsearch中使用SearchTemplate进行日期范围查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆