【问题标题】:Limit field length value in Elasticsearch限制 Elasticsearch 中的字段长度值
【发布时间】:2021-03-31 14:53:49
【问题描述】:

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

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

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

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

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

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

    B.让您的文档通过ingest pipeline 为您执行此操作:

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

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

    PUT my-index/_doc/1?pipeline=shorten-description
    {
      "description": "...."
    }
    

    【讨论】:

    • 酷,很高兴它有帮助!
    猜你喜欢
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2019-01-02
    • 2020-04-16
    • 2017-12-08
    • 1970-01-01
    • 2017-03-10
    相关资源
    最近更新 更多