簡體   English   中英

NEST 在 Elasticsearch 中索引文檔時添加 TimeZone

[英]NEST is adding TimeZone while indexing docs in Elasticsearch

我的 c# 類中有一個 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 中提供格式,但是是否有任何設置可以在僅使用日期時間字段時避免此時區添加?

要在沒有時區偏移的情況下保存 DateTimes,有兩件事需要改變。

首先,NEST 使用 JSON.Net 進行 json 序列化,因此我們需要更改ElasticClient上的序列化器設置,將 DateTimes 序列化為所需的格式,並在反序列化時將這些 DateTimes 解釋為Local kind

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,相關字段的 DateTime 格式

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM