【问题标题】:How does attribute mapping for the NEST Elasticsearch client work for date timesNEST Elasticsearch 客户端的属性映射如何适用于日期时间
【发布时间】:2018-02-18 19:13:07
【问题描述】:

我有一个 C# 对象,用于将文档索引到 Elasticsearch 中的现有类型。

该对象有一个 DateTime 字段,我想使用基于 NEST 客户端属性的映射来配置映射。我希望将格式配置为 DateFormat.basic_date_time_no_millis。我希望这将从日期时间对象中剔除毫秒。用作要索引的文档的类如下。

public class DBQueueDepth
{
    public string QueueName { get; set; }
    public int ProcessedCount { get; set; }
    public int NotProcessedCount { get; set; }
    public string Environment { get; set; }

    [Date(Format = DateFormat.basic_date_time_no_millis)]
    public DateTime SampleTimeStamp { get; set; }
}

在 Elasticsearch 中映射到的类型是;

    "dbQueueDepth": {
    "properties": {
      "environment": {
        "type": "text"
      },
      "notProcessedCount": {
        "type": "integer"
      },
      "processedCount": {
        "type": "integer"
      },
      "queueName": {
        "type": "text"
      },
      "sampleTimeStamp": {
        "type": "date",
        "format": "date_time_no_millis"
      }
    }

我的客户代码是

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

        var settings =
            new ConnectionSettings(node).MaximumRetries(10)
                .MaxRetryTimeout(new TimeSpan(0, 0, 0, 10))
                .DefaultIndex("austin_operational_data")
                .InferMappingFor<DBQueueDepth>(i => i.TypeName("dbQueueDepth"));


        ElasticClient client = new ElasticClient(settings);
        var response = client.IndexMany(rows);

当我尝试为文档编制索引时,我收到以下错误。

{index 返回 400 _index:operational_data _type:dbQueueDepth _id:AV5rqc3g6arLsAmJzpLK _version:0 错误:类型:mapper_parsing_exception 原因:“无法解析 [sampleTimeStamp]” CausedBy: 类型:非法参数异常原因:“无效格式:“2017-09-10T12:00:41.9926558Z”格式错误为“.9926558Z””}

我期望 NEST 客户端根据日期属性中的格式去除毫秒是否正确?

InferMappingFor 方法是否指示客户端使用 C# 对象上的属性指定的格式?

【问题讨论】:

    标签: c# elasticsearch elasticsearch-net


    【解决方案1】:

    你的假设并不完全正确。 format on a date field 控制请求中的日期字符串如何在 Elasticsearch 的服务器端解析,即它告诉 Elasticsearch 期望字符串的格式。

    现在,您可以将formatJsonConverter 结合使用,将DateTime 序列化为不发出毫秒的格式。但是,我可能会通过从应用程序代码中的DateTime 实例中删除毫秒数(可能在属性设置器中)并保留format 和序列化来解决这个问题。

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 2020-12-22
      • 1970-01-01
      • 2014-10-14
      • 2017-12-22
      • 1970-01-01
      相关资源
      最近更新 更多