【问题标题】:specific hour range query in elasticsearch 7.xelasticsearch 7.x 中的特定小时范围查询
【发布时间】:2023-03-31 02:40:01
【问题描述】:

我想了解一下 elasticsearch 7.x 中的查询。

我可以将我的查询转换为一周的特定时间范围吗?

=> 例如)7 天内每天早上 8 点到下午 3 点

现在我的查询(仅 7 天):

"query": {
    "bool": {
      "must": [
        {
          "range": {
            "statusAt": {
              "format": "strict_date_optional_time",
              "gte": "2019-11-04T02:54:28.625Z",
              "lte": "2019-11-11T02:54:28.625Z"
            }
          }
        }
      ]
    }
  }

【问题讨论】:

  • 这个答案可能会有所帮助:stackoverflow.com/a/33537029/4604579
  • @Val 我已经看过了。但我不能那样使用它。错误消息是"[script] query does not support [params]"
  • 那是旧版本的 ES。我修改了答案以使其适用于最新版本,请重试
  • @Val 我把它改成了你的答案。但错误消息是"Variable [min] is not defined."
  • 好吧,缺少参数,再试一次。

标签: elasticsearch


【解决方案1】:

我找到了解决方法。

每个版本都有点不同。

我的案例是 elasticsearch 7.x 版本。我在弹性的公共文档中找到了解决方案。

"query": {
    "bool": {
      "must": [
        {
          "range": {
            "statusAt": {
              "format": "strict_date_optional_time",
              "gte": "2019-11-04T02:54:28.625Z",
              "lte": "2019-11-11T02:54:28.625Z"
            }
          }
        }
      ],
      "filter": [
        {
          "script": {
            "script": {
              "source": "doc['date'].value.getHour() >= params.min && doc['date'].value.getHour() <= params.max",
              "params": {
              "min": 9,
              "max": 15
              }
            }
          }
        }
      ]
    }
  }

参考:

https://stackoverflow.com/a/33537029/4604579 https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-expression.html

【讨论】:

    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多