需求:为每一条document添加时间戳
解决:es5.0之前是使用 @timestamp 方式解决,es5.0之后该方式废弃,改用 pipeline 方式。
步骤:
1、配置时间戳pipeline:my_timestamp_pipeline
PUT _ingest/pipeline/my_timestamp_pipeline
{
"description": "Adds a field to a document with the time of ingestion",
"processors": [
{
"set": {
"field": "timestamp",
"value": "{{_ingest.timestamp}}"
}
}
]
}
2、创建索引时使用时间戳pipeline
PUT yzh_index
{
"settings": {
"default_pipeline": "my_timestamp_pipeline"
}
}
3、新增数据
PUT yzh_index/_doc/1
{
"content": "hello es!"
}
4、验证
GET yzh_index/_doc/1
结果:注意时间是国际标准时间,不是东八区的时间!
{
"_index" : "yzh_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"content" : "hello es!",
"timestamp" : "2022-01-24T09:00:31.571Z"
}
}