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


(Paulo Silva) #1

I have end up here with a odd scenario. I have this aggregation to perform using Elasticsearch and 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:

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:

A) Find a way to convert this value ("1453305924000") to a understandable date format. The Unix conversion didn't worked as I said...

B) Using C#, add the "format" property in the Elastic query. 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#.

Any thoughts for A or B?

Thanks a lot!!


(system) #2