限制Elasticsearch中的字段长度值

MD *_*lam 1 elasticsearch

我有一个 Elasticsearch 集群,其中包含大量带有各个字段及其值的数据。我有一个字段名称“描述”,它有一个小段落作为值。不同字段的值长度不同。我想限制这些字段的值。我尝试过以下方法。

  • 我使用自定义映射创建了一个索引
        "description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 150
            }
          }
        }
Run Code Online (Sandbox Code Playgroud)
  • 索引数据字段值映射显示它们为 150,但我的描述值与以前相同。
  • 我还尝试使用修改数据
PUT index_name/_settings
{
  "index.mapping.field_name_length.limit": 150
} 
Run Code Online (Sandbox Code Playgroud)

但这些都不起作用。我的描述数据还是和以前一样。所以我该怎么做?我应该在将描述值上传到集群时尝试限制描述值,还是可以在上传描述值后限制弹性搜索集群中的值?
我的主要任务是将描述数据的最大长度设置为 150 个字符。提前致谢。

Val*_*Val 5

您添加的配置不会修改_source您发送的文档,只会修改索引的内容。

您有两种选择来缩短给定字段的值:

A.在发送到Elasticsearch之前修改源文档

B. 让您的文档流经摄取管道,该管道将为您执行此操作:

PUT _ingest/pipeline/shorten-description
{
  "description": "Shorten my description field",
  "processors": [
    {
      "script": {
        "if": "ctx.description != null",
        "lang": "painless",
        "source": "ctx.description = ctx.description.substring(0, params.length)",
        "params": {
          "length": 150
        }
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

然后您可以使用该摄取管道为您的文档建立索引:

PUT my-index/_doc/1?pipeline=shorten-description
{
  "description": "...."
}
Run Code Online (Sandbox Code Playgroud)