Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving date in microsecond format in ElasticSearch

I am trying to save set of events from MySQL database into elastic search using jdbc input plugin with Logstash. The event record in database contains date fields which are in microseconds format. Practically, there are records in database between set of microseconds.

While importing data, Elasticsearch is truncating the microseconds date format into millisecond format. How could I save the data in microsecond format? The elasticsearch documentation says they follow the JODA time API to store date formats, which is not supporting the microseconds and truncating by adding a Z at the end of the timestamp.

Sample timestamp after truncation : 2018-05-02T08:13:29.268Z

Original timestamp in database : 2018-05-02T08:13:29.268482

like image 697
BarathVutukuri Avatar asked May 02 '18 08:05

BarathVutukuri


People also ask

How do I create a timestamp field for an Elasticsearch index?

If you're running Elasticsearch version 6.5 or newer, you can use the index. default_pipeline settings to create a timestamp field for an index. This can be accomplished by using the Ingest API and creating a pipeline at the time your index is created.

What is @timestamp in Elasticsearch?

[@timestamp] is used by multiple types. Set update_all_types to true to update [format] across all types Elasticsearch.


2 Answers

The Z is not a result of the truncation but the GMT timezone.

ES supports microseconds, too, provided you've specified the correct date format in your mapping.

If the date field in your mapping is specified like this:

    "date": {
      "type": "date",
      "format": "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
    }

Then you can index your dates with the exact microsecond precision as you have in your database

UPDATE

Here is a full re-creation that shows you that it works:

PUT myindex
{
  "mappings": {
    "doc": {
      "properties": {
        "date": {
          "type": "date",
          "format": "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
        }
      }
    }
  }
}

PUT myindex/doc/1
{
  "date": "2018-05-02T08:13:29.268482"
}
like image 129
Val Avatar answered Sep 23 '22 19:09

Val


Side note, "date" datatype stores data in milliseconds in elasticsearch so here in case nanoseconds precision level are wanted in date ranges queries; the appropriate datatype is date_nanos

like image 45
JulienG Avatar answered Sep 24 '22 19:09

JulienG