【问题标题】:Elasticsearch Date Histogram buckets starts from wrong dateElasticsearch 日期直方图存储桶从错误的日期开始
【发布时间】:2017-10-27 21:17:29
【问题描述】:

我有 ElasticSearch 分析查询。它必须按 7d 个桶聚合总和。 我也使用了扩展范围。 但是结果桶从错误的日期开始。 弹性 5.2.2 版本。 我做错了什么?

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "doc.dateExecute": {
              "from": "2017-07-11",
              "to": "2017-07-31",
              "include_lower": true,
              "include_upper": true,
              "boost": 1.0
            }
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1.0,
      "_name": "onDates"
    }
  },
  "aggregations": {
    "time": {
      "date_histogram": {
        "field": "doc.dateExecute",
        "interval": "7d",
        "offset": 0,
        "order": {
          "_key": "asc"
        },
        "keyed": false,
        "min_doc_count": 0,
        "extended_bounds": {
          "min": "2017-07-11",
          "max": "2017-07-31"
        }
      },
      "aggregations": {
        "incomingAmount": {
          "sum": {
            "field": "doc.incomingAmount"
          }
        },
        "outgoingAmount": {
          "sum": {
            "field": "doc.outgoingAmount"
          }
        }
      }
    }
  }
}

但存储桶从“2017-07-06T00:00:00.000Z”开始,每隔 7 天到达“2017-07-27T00:00:00.000Z”。 我错过了什么?谢谢!

更新 1

我会尝试多种查询变体。不同的时区、范围、禁用扩展边界、间隔多个 7 天。它仅适用于 1-3d 间隔。

更新 2 我试过 7.x。似乎在最新版本上运行良好。

【问题讨论】:

  • 如果您删除 extended_bounds 会发生什么?
  • @val, "from": "2016-09-28", "to":"2017-10-28", extended_bounds 已删除。无论如何,第一个桶从“key_as_string”开始:“2016-09-29T00:00:00.000Z”,不考虑第一个命中 2016-10-01
  • 似乎最近的星期四作为第一个存储桶日期:)
  • 如果使用 dateExecute 而不是 doc.dateExecute 会发生什么?
  • @deathyr,dateExecute 是嵌套文档的嵌套字段。反正我会试试的。 7d case 没有任何变化,但其他测试失败。

标签: database search elasticsearch aggregate


【解决方案1】:

好的,在弹性论坛上进行一些对话并注册一个错误:

1) 在 ES 中使用 DateHistogram 和 ExtendedBounds 很棘手(至少 5.2) DateHistogram 从 UnixEpoch 1970-01-01 开始存储桶,因此如果您使用天间隔(如 7d 或 3d 等)但需要从左边界开始存储桶 - 您需要设置左边界日期的偏移量(以毫秒为单位)。 2) 但是我们在 5.2.2 版本上仍然存在一些错误。对于 7d 间隔,我们在星期四有明确的第一个桶(01-01-1970 是星期四)。所有其他具有适当偏移的存储桶。

缺乏文档和很多奇怪的行为:(

【讨论】:

  • 另外,据此,我尝试将 min extended_bound 设置为范围过滤器中的较低日期,并且偏移量也设置为以毫秒为单位的相同值。但它仍然给出了相同的结果。使用的间隔是 90 天。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
相关资源
最近更新 更多