简体   繁体   English

解析或格式化最小/最大日期值-用于elaticsearch聚合

[英]Parsing or Formating Min/Max date value - For elaticsearch aggregation

I have end up here with a odd scenario. 我最终遇到了一个奇怪的情况。 I have this aggregation to perform using Elasticsearch and c#: 我具有使用Elasticsearch和c#执行的聚合:

 "aggs": {
    "Max_opened_at": {
      "max": {
        "field": "opened_at"
      }
    },
    "Min_opened_at": {
      "min": {
        "field": "opened_at"
      }
    }
  }

So far, so good as it is returning the aggregated keys to me as following: 到目前为止,效果很好,它可以将聚合密钥返回给我,如下所示:

    Response: {
  "took" : 29,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 30218,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "Min_sys_created_on" : {
      "value" : 1.402508357E12,
      "value_as_string" : "1402508357000"
    },
    "Min_sys_updated_on" : {
      "value" : 1.453305924E12,
      "value_as_string" : "1453305924000"
    }

The problem here is the return itself. 这里的问题是收益本身。 When I tried to convert the value ("1453305924000") assuming it was a Unix format using the standard conversion below, it fails due "out of range value" exception: 当我尝试使用下面的标准转换来转换值(“ 1453305924000”)(假设它是Unix格式)时,由于“超出范围的值”异常而失败:

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date - epoch).TotalSeconds);

I believe I've got 2 alternatives: 我相信我有2种选择:

A) Find a way to convert this value ("1453305924000") to a understandable date format. A)找到一种方法将此值(“ 1453305924000”)转换为可理解的日期格式。 The Unix conversion didn't worked as I said... Unix转换没有像我所说的那样...

B) Using C#, add the "format" property in the Elastic query. B)使用C#,在Elastic查询中添加“ format”属性。 It would look like this: 它看起来像这样:

  "aggs": {
    "test": {
      "min": {
        "field": "sys_created_on", 
        "format" : "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
      }
    }

Unfortunately I have no idea how to do add this "format" property using NEST C#. 不幸的是,我不知道如何使用NEST C#添加此“格式”属性。

Any thoughts for A or B? 对A或B有什么想法吗?

Thanks a lot!! 非常感谢!!

1453305924000 is the number of milliseconds since the epoch, so you don't need to subtract it from the epoch. 1453305924000是纪元以来的毫秒数,因此您无需从纪元中减去它。 So you can go with option A and simply do it like this: 因此,您可以使用选项A并像这样简单地进行操作:

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
return epoch.AddMilliseconds(date).ToLocalTime();

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

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