To create a timestamp field on my indices, according to this answer, I have created a Ingest Pipeline to run over specific indices:
PUT _ingest/pipeline/auto_now_add
{
"description": "Assigns the current date if not yet present and if the index name is whitelisted",
"processors": [
{
"script": {
"source": """
// skip if not whitelisted
if (![ "my_index_1",
"my_index_2"
].contains(ctx['_index'])) { return; }
// always update updated_at
ctx['updated_at'] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
"""
}
}
]
}
then I apply to all indices settings as the default pipeline
PUT _all/_settings
{
"index": {
"default_pipeline": "auto_now_add"
}
}
After that, I start indexing my objects into those indices. When I query an indexed item, I will get that item with the updated_at
field updated at the time of the indexing like:
{
_index: 'my_index_1',
_type: '_doc',
_id: 'r1285044056',
_version: 11,
_seq_no: 373,
_primary_term: 2,
found: true,
_source: {
updated_at: '2021-07-07 04:35:39',
...
}
}
I would like now to have a created_at
field, that only updates the first time, so I have tried to upsert script above in this way:
PUT _ingest/pipeline/auto_now_add
{
"description": "Assigns the current date if not yet present and if the index name is whitelisted",
"processors": [
{
"script": {
"source": """
// skip if not whitelisted
if (![ "my_index_1",
"my_index_2",
"..."
].contains(ctx['_index'])) { return; }
// always update updated_at
ctx['updated_at'] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
// don't overwrite if present
if (ctx != null && ctx['created_at'] != null) { return; }
ctx['created_at'] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
"""
}
}
]
}
but this solution does not seem to work: the condition
if (ctx != null && ctx['created_at'] != null) { return; }
will always fail, thus resulting in a update of the created_at
at every object update on the index, in the same way of the updated_at
field, making it useless.
So, how to prevent that, and make sure that that field created_at
exists after it has been created by the Ingestion Pipeline?