计算字段数据大小并在索引时存储到其他字段ElasticSearch 7.17



我正在寻找一种方法来存储一个字段的大小(字节)在一个文档的新字段。

。当用包含值hello的字段message创建文档时,我希望编写另一个字段message_size_bytes,在本例中具有值5

我知道使用_update_by_query_search使用脚本字段的可能性,但我有这么多的数据,我不想在查询时计算大小,但在索引时。

是否有可能只使用Elasticsearch 7.17来做到这一点?在将数据传递给elasticsearch之前,我无法访问数据。

您可以使用脚本处理器的摄取管道。

你可以使用下面的命令创建管道:

PUT _ingest/pipeline/calculate_bytes
{
"processors": [
{
"script": {
"description": "Calculate bytes of message field",
"lang": "painless",
"source": """
ctx['message_size_bytes '] = ctx['message'].length();
"""
}
}
]
}

创建管道后,您可以在索引数据时使用管道名称,如下所示(您也可以在logstash, java或任何其他客户端中使用):

POST 74906877/_doc/1?pipeline=calculate_bytes
{
"message":"hello"
}

结果:

"hits": [
{
"_index": "74906877",
"_id": "1",
"_score": 1,
"_source": {
"message": "hello",
"message_size_bytes ": 5
}
}
]

最新更新