gpt4 book ai didi

elasticsearch - 如何在ES 7.3v中检查ES文档的创建时间?

转载 作者:行者123 更新时间:2023-12-03 00:34:31 26 4
gpt4 key购买 nike

我想知道我在Elasticsearch中创建文档的时间,是否有任何保留此信息的元字段,或者如果不存在该字段,那么我该如何实现它。

我每次都提出增补要求,因此无法提供时间。

追加请求是:

POST test/_update/document_id
{
"doc": {
"field1": "value1",
"field2": "value2",
"field3": "value3",
"relationship": {
"parent": "child"
}
},
"doc_as_upsert": "true"
}

使用elasticsearch npm和以下代码:
esDoc.doc['relationship'] = { name: "test" };
esDoc['doc_as_upsert'] = true;
bulkQueue.add({ update: { _index: ES_INDEX_PREFIX + testId, _id: _id } }, esDoc)

最佳答案

实现此目的的一种方法是利用ingest pipelines并在文档中记录 _ingest.timestamp 的值。

首先创建以下摄取管道:

PUT _ingest/pipeline/set_timestamp
{
"description": "adds the timestamp when a document is indexed",
"processors": [
{
"set": {
"field": "indexed_at",
"value": "{{_ingest.timestamp}}"
}
}
]
}

然后在索引新文档时,只需在您的请求中引用该管道即可:
PUT tmp/_doc/1?pipeline=set_timestamp
{
"test": "foo"
}

您的文档将包含一个名为 indexed_at的新字段,其中包含对其进行索引的确切时间戳:
GET tmp/_doc/1
{
"test" : "foo",
"indexed_at" : "2019-10-03T13:19:03.181Z"
}

更新:

由于您将Update API与文档upsert一起使用,因此您无权访问提取管道。我建议这样做是一个 scripted_upsert ,就像这样:
POST test/_update/document_id
{
"scripted_upsert":true,
"script": {
"source": """
// update all fields
ctx._source.putAll(params);

// add timestamp the first time
if (ctx._source.indexed_at == null) {
def now = Instant.ofEpochMilli(new Date().getTime());
ZonedDateTime zdt = ZonedDateTime.ofInstant(now, ZoneId.of('Z'));
ctx._source.indexed_at = zdt.format(DateTimeFormatter.ISO_INSTANT);
}
""",
"params": {
"field1": "value1",
"field2": "value2",
"field3": "value3",
"relationship": {
"parent": "child"
}
}
},
"upsert": {}
}

关于elasticsearch - 如何在ES 7.3v中检查ES文档的创建时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58219945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com