无法在Elasticsearch查询中正确过滤时间

发布于 2025-01-29 12:33:54 字数 1106 浏览 3 评论 0原文

保存在ES中的记录具有称为Updated_at的字段,该字段将时间存储在时代中。 在我的搜索查询中,我会根据“ yyyy-mm-dd”格式过滤日期,如下所示:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "bool": {
                "filter": [
                  {
                    "range": {
                      "segment_status.updated_at": {
                        "from": "2022-05-16",
                        "to": "2022-05-16",
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                      }
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1
              }
            },
            "path": "segment_status",
            "ignore_unmapped": false,
            "score_mode": "avg",
            "boost": 1
          }
        }
        ]
    }
  }
        
}

我的时区是GMT+05:30。我有一张纪录的时代时间1652733000,该纪录是5月17日左右凌晨2:00。使用上面的查询,我正在打出此记录,我假设这是因为GMT的时间是5月16日。

请验证此问题,如果有步行路线可以在生成查询的代码中进行修复,这将是有帮助的。

The records which are saved in ES have a field called updated_at which stores time in epoch.
In my search query I'm filtering date based on 'yyyy-mm-dd' format as follows:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "bool": {
                "filter": [
                  {
                    "range": {
                      "segment_status.updated_at": {
                        "from": "2022-05-16",
                        "to": "2022-05-16",
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                      }
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1
              }
            },
            "path": "segment_status",
            "ignore_unmapped": false,
            "score_mode": "avg",
            "boost": 1
          }
        }
        ]
    }
  }
        
}

My time zone is GMT+05:30. I have a record saved with epoch time 1652733000 which comes out to be 17th May at around 2:00 am. Using the above ES query I'm hitting this record and I'm assuming that is because the time at GMT is 16th May.

Please validate this and it would be helpful if there is a walkaround to fix this in the code generating the query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错々过的事 2025-02-05 12:33:54

您是正确的,自Epoch以来的Miliseconds评估了2022年5月16日星期一8:30:00 PM UTC时间。如果您想使用户在自己的时区中按日期进行搜索,则必须停止根据自己的时区进行思考。如果您可以访问日期作为localdate对象和用户的时区,则自从Epoch以来,可以使用Miliseconds对Elasticsearch进行查询:

LocalDate date = ...
Instant startOfDay = date.atStartOfDay(userZoneId).toInstant();
Instant endOfDay = startOfDay.plusMillis(3600 * 24 * 1000 - 1);

You are correct, those miliseconds since epoch evaluate Monday, May 16, 2022 8:30:00 PM UTC time. If you want to enable users to search by date in their own timezones, you have to stop thinking in terms of your own timezone. If you have access to the date as LocalDate object and the timezone of the user, you can make queries against elasticsearch using miliseconds since epoch:

LocalDate date = ...
Instant startOfDay = date.atStartOfDay(userZoneId).toInstant();
Instant endOfDay = startOfDay.plusMillis(3600 * 24 * 1000 - 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文