How to make elasticsearch add the timestamp field to every document in all indices? How to make elasticsearch add the timestamp field to every document in all indices? elasticsearch elasticsearch

How to make elasticsearch add the timestamp field to every document in all indices?


Elasticsearch used to support automatically adding timestamps to documents being indexed, but deprecated this feature in 2.0.0

From the version 5.5 documentation:

The _timestamp and _ttl fields were deprecated and are now removed. As a replacement for _timestamp, you should populate a regular date field with the current timestamp on application side.


You can do this by providing it when creating your index.

$curl -XPOST localhost:9200/test -d '{"settings" : {    "number_of_shards" : 1},"mappings" : {    "_default_":{        "_timestamp" : {            "enabled" : true,            "store" : true        }    }  }}'

That will then automatically create a _timestamp for all stuff that you put in the index.Then after indexing something when requesting the _timestamp field it will be returned.


Adding another way to get indexing timestamp. Hope this may help someone.

Ingest pipeline can be used to add timestamp when document is indexed. Here, is a sample example:

PUT _ingest/pipeline/indexed_at{  "description": "Adds indexed_at timestamp to documents",  "processors": [    {      "set": {        "field": "_source.indexed_at",        "value": "{{_ingest.timestamp}}"      }    }  ]}

Earlier, elastic search was using named-pipelines because of which 'pipeline' param needs to be specified in the elastic search endpoint which is used to write/index documents. (Ref: link) This was bit troublesome as you would need to make changes in endpoints on application side.

With Elastic search version >= 6.5, you can now specify a default pipeline for an index using index.default_pipeline settings. (Refer link for details)

Here is the to set default pipeline:

PUT ms-test/_settings{  "index.default_pipeline": "indexed_at"}

I haven't tried out yet, as didn't upgraded to ES 6.5, but above command should work.