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

nwlqm0z1  于 2022-12-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(130)

我正在寻找一种方法来存储一个字段的大小(字节)在一个新的领域的文件。
也就是说,当创建一个文档时,其中的字段message包含值hello,我希望写入另一个字段message_size_bytes,在本例中,该字段的值为5
我知道使用_update_by_query_search并使用脚本字段的可能性,但是我有太多的数据,我不想在查询时计算大小,而是在索引时。
是否有可能只使用Elasticsearch 7.17来实现这一点?在将数据传递给Elasticsearch之前,我无法访问数据。

zd287kbt

zd287kbt1#

可以将"摄取管道"与Script processor配合使用。
可使用以下命令创建管线:

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
        }
      }
    ]

相关问题