复杂布尔查询中的弹性搜索错误

时间:2018-11-08 11:36:09

标签: elasticsearch elasticsearch-dsl

我正在尝试进行Elasticsearch查询,我需要在其中搜索Elasticsearch表中的时间范围。我有记录有星际和结束时间。从UI中,我给出了开始时间和结束时间,这是我需要搜索文件的时间窗口。假设记录中开始时间和结束时间的时间窗口小于用户输入的时间窗口,我创建了以下查询:

    {
    "_source":["filename","starttime","endtime"],
    "sort":[{
        "starttime":{"order":"asc"}
    }],
    "query":{
        "bool":{
            "should":{
                "bool":{
                    "must":[
                        "range":{
                            "starttime":{
                                "lte":1489602610000
                            }
                        },
                        "range":{
                            "endtime":{
                                "gte":1489602610000,
                            }
                        }
                    ]
                }
            },
            "should":{
                "bool":{
                    "must":[
                        "range":{
                            "starttime":{
                                "gte":1489602610000
                            }
                        },
                        "range":{
                            "endtime":{
                                "lte":1489689000000
                            }
                        }
                    ]
                }
            },
            "should":{
                "bool":{
                    "must":[
                        "range":{
                            "starttime":{
                                "lte":1489689000000
                            }
                        },
                        "range":{
                            "endtime":{
                                "gte":1489689000000
                            }
                        }
                    ]
                }
            }
            }
        }
}

我遇到错误

  

“意外字符(':'(代码58)):期望逗号分隔   数组条目\ n,位于[来源:   org.elasticsearch.transport.netty4.ByteBufStreamInput@29263f09;线:   11,栏:33]“

1 个答案:

答案 0 :(得分:2)

您的查询存在几个问题:

  • 一个悬挂的逗号
  • 不止一个bool/should子句
  • range查询未正确包装在花括号中

您可以在下面找到正确的查询:

{
  "_source": [
    "filename",
    "starttime",
    "endtime"
  ],
  "sort": [
    {
      "starttime": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "range": {
                  "starttime": {
                    "lte": 1489602610000
                  }
                }
              },
              {
                "range": {
                  "endtime": {
                    "gte": 1489602610000
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "range": {
                  "starttime": {
                    "gte": 1489602610000
                  }
                }
              },
              {
                "range": {
                  "endtime": {
                    "lte": 1489689000000
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "range": {
                  "starttime": {
                    "lte": 1489689000000
                  }
                }
              },
              {
                "range": {
                  "endtime": {
                    "gte": 1489689000000
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}