c# - 在 NEST DateRange 谓词中使用 DateTimeOffset

标签 c# .net elasticsearch nest

我有一个带有 DateRanges 谓词的简单 NEST 查询:

DateTimeOffset dt;
...
.DateRange(r => r
    .Field("Time")
    .LessThanOrEquals(dt)

根据 this question ,简单地将 DateTimeOffset 转换为 DateTime 可能会导致数据丢失LessThanOrEquals 和其他谓词方法接受 Nest.DateMath 类型的对象,该类型具有以下运算符:

public static implicit operator DateMath(DateTime dateTime);
public static implicit operator DateMath(string dateMath);

此外DateRangeQueryDescriptor还有一个额外的方法:

public DateRangeQueryDescriptor<T> TimeZone(string timeZone);

但我不确定如何使用它。

问题:

我如何使用 DateTimeOffset 对象作为 NEST 中的 DateRange 谓词的值,并确保发送到 Elastic 的日期是正确的?

最佳答案

显然,我错了。

根据 thisthis ,所有日期都存储或必须存储为 UTC,因此范围参数也应作为 UTC 传递。

Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.

关于c# - 在 NEST DateRange 谓词中使用 DateTimeOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48008777/

相关文章:

.net - 使用 .NET 3.5 中的 SOAP 1.1 Web 服务

elasticsearch - 没有 ACID 数据库是否可以进行事务(就数据一致性而言)?

elasticsearch - 何时直接查询cassandra与使用索引器

c# - ASP.NET DropDownList 中的 XSS 可能吗?

c# - 在哪里存储自定义配置文件

c# - 从 C++/CLI 调用 C# dll 时出现 FileNotFoundException

c# - 使用自定义配置文件启动可执行文件

.net - 使用 IList<T> 的 NHibernate 和 XML 序列化

.net - 如何解决 Xamarin.Firebase.iOS.Core - 8.10.0.1 nuget 安装错误

elasticsearch - 如果缺少字段,ElasticSearch功能得分查询将不默认为1