NEST 在 Elasticsearch 中索引文档时添加时区

NEST is adding TimeZone while indexing docs in Elasticsearch

我的 c# class 中有一个 DateTime 字段,如下所示

 public DateTime PassedCreatedDate { get; set; }

在将它从 NEST 索引到 elasticssearch 时,它会将它与本地时区一起保存。如何避免这种情况?

 "PassedCreatedDate": "2015-08-14T15:50:04.0479046+05:30" //Actual value saved in ES
 "PassedCreatedDate": "2015-08-14T15:50:04.047" //Expected value

elasticsearch 中 PassedCreatedDate 的映射是

  "PassedCreatedDate": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },

我知道有一个字段作为字符串并在 ElasticProperty 中提供格式,但是是否有任何设置可以避免在仅使用日期时间字段时添加时区?

要在没有时区偏移的情况下保存日期时间,需要更改两件事。

首先,NEST 使用 JSON.Net 进行 json 序列化,因此我们需要更改 ElasticClient 上的序列化程序设置以将 DateTimes 序列化为所需的格式,并将这些 DateTimes 解释为Local 反序列化时种类

var settings = new ConnectionSettings(new Uri("http://localhost:9200"));

settings.SetJsonSerializerSettingsModifier(jsonSettings => 
{
    jsonSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ss",
    jsonSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local
});

var connection = new InMemoryConnection(settings);
var client = new ElasticClient(connection: connection);

其次,我们需要通过映射告诉 Elasticsearch,相关字段的日期时间格式

"PassedCreatedDate": {
    "type": "date",
    "format": "yyyy-MM-ddTHH:mm:ss"
},