The GET operation won't be necessary.
Depending on how easily you can configure how your writes happen, you could do the following:
- Store a script which expects the doc-to-be-updated content as
params
:
POST _scripts/manage_time_tags
{
"script": {
"lang": "painless",
"source": """
if (ctx._source.FirstTimestamp != null && params.FirstTimestamp != null) {
ctx._source.SecondTimestamp = params.FirstTimestamp;
ctx._source.TimeTag = ctx._source.SecondTimestamp - ctx._source.FirstTimestamp;
}
"""
}
}
- Instead of directly writing to ES as you were up until now, use the
upsert
method of the Update API:
POST myindex/_update/1
{
"upsert": {
"id": 1,
"FirstTimestamp": 15974343498
},
"script": {
"id": "manage_time_tags",
"params": {
"id": 1,
"FirstTimestamp": 15974343498
}
}
}
This will ensure that if the document does not exist yet, the contents of upsert
are synced and the script doesn't even run.
- As new events come in, simply call
/_update/your_id
again but with the most recent contents of id
and FirstTimestamp
.
POST myindex/_update/1
{
"upsert": {
"id": 1,
"FirstTimestamp": 15974344498
},
"script": {
"id": "manage_time_tags",
"params": {
"id": 1,
"FirstTimestamp": 15974344498
}
}
}
Note: this should not be confused with a rather poorly named scripted upsert
which'll run the script irregardless of whether the doc already exists or not. This option should be omitted (or set to false
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…