eran
eran

Reputation: 6921

convert unix timestamp to datetime in elasticsearch

I index my couchbase documents in elasticsearch (using xdcr). In couchbase, we have fields storing unix timestamps, but I want to index them as date-time in ElasticSearch. Is there a way to tell elasticsearch to convert unix timestamp (in seconds) to date time?

thanks.

Upvotes: 1

Views: 7035

Answers (2)

elcrion1090238
elcrion1090238

Reputation: 36

Transform function in mapping is deprecated since elasticsearch 2.0 due to debugging issues. The solution might be this:

  1. Create a new mapping with updated type (you can get that copy pasted from you current mapping using GET your_index/_mapping). Update you date_created type to "epoch_second"

    PUT index2 { "mappings": { "couchbaseDocument": { "properties": { "doc" : { "properties" : { "createdAt" : {"type" : "date","format":"epoch_second"}, "amount": { "type": "float" }, "appGroup": { "type": "long" }, "bmi": { "type": "float" },

  2. Use that mapping to reindex from your current index(use script if need to convert data) :

    POST _reindex { "source": { "index": "index1" }, "dest": { "index": "index2" }, "script": { "lang": "painless", "inline": "if(ctx._source.doc.createdAt !=null){ ctx._source.doc.createdAt = (long)ctx._source.doc.createdAt}" } }

Upvotes: 0

eran
eran

Reputation: 6921

no one answered, so I continued googling and found the answer here:

http://elasticsearch-users.115913.n3.nabble.com/Convert-unix-timestamp-seconds-to-java-milliseconds-td4067144.html

{
    "test" : {
"transform" : {
            "script" : "ctx._source['date_created'] = ctx._source['date_created'].toLong() * 1000",
            "lang": "groovy"
        },
        "properties" : {
            "date_created" : {"type" : "date"},
        }
    }
}

Upvotes: 4

Related Questions