简体   繁体   English

NEST Elasticsearch客户端的属性映射在日期时间如何工作

[英]How does attribute mapping for the NEST Elasticsearch client work for date times

I have a C# object that I am using to index documents to an existing type in Elasticsearch. 我有一个C#对象,用于将文档索引到Elasticsearch中的现有类型。

The object has a DateTime field that I want to configure the mapping using the the NEST clients attribute based mapping. 该对象具有一个DateTime字段,我想使用基于NEST客户端属性的映射来配置映射。 I am looking to configure the format as DateFormat.basic_date_time_no_millis. 我正在将格式配置为DateFormat.basic_date_time_no_millis。 I am hopping that this will strip out the milliseconds from the date time object. 我希望这将从日期时间对象中删除毫秒。 The class being used as the document to index is below. 用作索引文档的类如下。

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; }
}

The type being mapped to in Elasticsearch is; 在Elasticsearch中映射到的类型是;

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

My client code is 我的客户代码是

        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);

When I try and index the document I receive the following error. 当我尝试为文档建立索引时,出现以下错误。

{index returned 400 _index: operational_data _type: dbQueueDepth _id: AV5rqc3g6arLsAmJzpLK _version: 0 error: Type: mapper_parsing_exception Reason: "failed to parse [sampleTimeStamp]" CausedBy: Type: illegal_argument_exception Reason: "Invalid format: "2017-09-10T12:00:41.9926558Z" is malformed at ".9926558Z""} {索引返回400 _index:运算数据_type:dbQueueDepth _id:AV5rqc3g6arLsAmJzpLK _version:0错误:类型:mapper_parsing_exception原因:“未能解析[sampleTimeStamp]]原因:类型:非法_argument_exception1:2017:无效:10 :41.9926558Z“格式错误,格式为” .9926558Z“”}}

Am I correct in expecting the NEST client to strip out the milliseconds based on the formatting in the Date Attribute ? 我期望NEST客户端根据Date属性中的格式删除毫秒是正确的吗?

Is the InferMappingFor method instructing the client to use the formatting designated by attributes on the C# object? InferMappingFor方法是否指示客户端使用C#对象上的属性指定的格式?

Your assumption is not quite right. 您的假设不太正确。 The format on a date field controls how the date string in the request will be parsed on the server side in Elasticsearch ie it tells Elasticsearch in what format to expect the string. date字段上格式控制如何在Elasticsearch的服务器端解析请求中的日期字符串,即,它告诉Elasticsearch期望该字符串的格式。

Now, you could use format in conjunction with a JsonConverter to serialize DateTime into a format that does not emit the milliseconds. 现在,您可以将formatJsonConverter结合使用以将DateTime序列化为不发出毫秒的格式。 However, I would probably approach this instead by removing the milliseconds from the DateTime instance within application code (perhaps in the property setter) and leaving the format and serialization as is. 但是,我可能会通过从应用程序代码中的DateTime实例中删除毫秒(也许在属性设置器中)并保留format和序列化来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM