【问题标题】:Query to pull with timestamps that were updated more than 30m ago not working使用超过 30m 前更新的时间戳的查询无法正常工作
【发布时间】:2017-08-19 10:58:08
【问题描述】:

我绞尽脑汁想解决这个问题。一切似乎都检查过了,但它并没有像我期望的那样工作:(

我有一个索引test-index,其中包含以下文档:

{
        "_index": "test-index",
        "_type": "testType",
        "_id": "AV33b_VYUyX1XZAq7NTI",
        "_score": 1,
        "_source": {
          "timestamp": "2017-08-17T17:56:55"
        }
      },
      {
        "_index": "test-index",
        "_type": "testType",
        "_id": "AV33cBN4UyX1XZAq7NTJ",
        "_score": 1,
        "_source": {
          "timestamp": "2017-08-18T17:11:12"
        }
      },
      {
        "_index": "test-index",
        "_type": "testType",
        "_id": "AV33cetJUyX1XZAq7NTK",
        "_score": 1,
        "_source": {
          "timestamp": "2017-08-19T17:11:12"
        }
      }

这里可以看出我有

  1. 理论上是昨天更新的文档。
  2. 今天 (08/18) 20 分钟前(现在 = 17:30 在撰写本文时)最后一次更新的文档
  3. 一份明天“最后更新”的文档,只是为了说明我为什么对这不起作用感到困惑。

我有以下疑问:

GET test-index/testType/_search?pretty
{
  "query": {
    "range": {
      "timestamp": {
        "lte": "now-30m"
      }
    }
  }
}

它会提取今天(20 分钟前)更新的记录,以及昨天更新的记录。我原以为它只会在昨天打破记录。

"hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test-index",
        "_type": "testType",
        "_id": "AV33b_VYUyX1XZAq7NTI",
        "_score": 1,
        "_source": {
          "timestamp": "2017-08-17T17:56:55"
        }
      },
      {
        "_index": "test-index",
        "_type": "testType",
        "_id": "AV33cBN4UyX1XZAq7NTJ",
        "_score": 1,
        "_source": {
          "timestamp": "2017-08-18T17:11:12"
        }
      }
    ]

将查询更改为gte now-30m,它会按预期工作并提取带有明天时间戳的记录。如果我也将范围查询更改为lte now-1d,则范围查询也能正常工作,只显示预期的 08/17 记录,但我想使用一分钟截止。当我尝试做几个小时时,也会观察到同样的不当行为。

我尝试将我的格式设置为 yyyy-MM-dd HH:mm:ss 并接受 ES 的默认日期映射,但没有成功。

有人知道这里可能出了什么问题吗?

编辑:它似乎也为“今天”提取记录,但也是未来的某个时间,例如:

 {
        "_index": "test-index",
        "_type": "testType",
        "_id": "AV33gSs6UyX1XZAq7NTS",
        "_score": 1,
        "_source": {
          "timestamp": "2017-08-18 19:11:12"
        }
      }

这似乎是一个精度问题,我只是不知道问题是什么,因为一切似乎都是正确的。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我想我最终找出了造成这种情况的根本原因。索引文档时,ES 将提供的值视为 UTC 日期/时间。查询时,ES 使用 now 的 UTC 日期/时间与索引的时间戳进行比较。

    鉴于我比 UTC 晚 5 小时,并且我正在使用本地日期/时区索引文档,我的查询基本上是在说“给我从现在起不到 5 小时 - 30 分钟的日期。

    这是我最终编写的查询,以查看它在字面上比较了哪些值,以及我必须做些什么才能在 bool 查询中产生“预期”结果:

    GET test-index/testType/_search?pretty
    {
      "query": {
        "bool" : {
          "must" : {
            "script" : {
              "script" : {
                "inline": "doc['timestamp'].value < new Date().getTime() - (5 * 60 * 60 * 1000) - (120 * 60 * 1000)",
                "lang": "painless"
               }
            }
          }
        }
      },
      "script_fields": {
        "timestampValue" : {
          "script" : "doc['timestamp'].value"
        },
        "valueTimestampMustBeLessThan" : {
          "script" : "new Date().getTime() - (120 * 60 * 1000)"
        },
        "now" : {
          "script" : "new Date().getTime()"
        },
        "subtract": {
          "script": "(120 * 60 * 1000)"
        },
        "timestamp" : {
          "script" : "doc['timestamp']"
        },
        "lt?" : {
          "script" : "doc['timestamp'].value < new Date().getTime() - (120 * 60 * 1000)"
        },
        "gt?" : {
          "script" : "doc['timestamp'].value > new Date().getTime() - (120 * 60 * 1000)"
        }
      }
    }
    

    一个例子:

    • 我在 2017 年 8 月 18 日下午 6:40 左右插入的文档将其 UTC 时间读取为该时间,并将其“本地”时间读取为下午 1:40。
    • 我在 2017 年 8 月 18 日下午 6:41 左右运行的查询显示 now 的 UTC 时间为晚上 11:41,其“本地”时间为下午 6:41。

    ES 文档中有很多地方提到它使用 UTC 日期,例如:

    但直到现在我才完全理解其中的含义。

    在我所做的事情中,我只需要确保我的应用插入 UTC 时间,尤其是考虑到不同的时区。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2017-05-16
      相关资源
      最近更新 更多