【问题标题】:How to update the mapping in Elasticsearch to change the field datatype and change the type of analyzers in string如何更新 Elasticsearch 中的映射以更改字段数据类型并更改字符串中的分析器类型
【发布时间】:2016-11-05 21:19:51
【问题描述】:

尝试更新映射时出现以下错误:

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"mapper [timestamp] of different type, current_type [string], merged_type [date]"}],"type":"illegal_argument_exception","reason":"
mapper [timestamp] of different type, current_type [string], merged_type [date]"},"status":400}

我正在尝试在 Windows 上运行以下命令

   curl -XPUT localhost:9200/logstash-*/_mapping/log?update_all_types -d "{
    "properties":
    {
        "timestamp": 
        {
            "type": "date", 
            "format": "MM-dd-yyyy HH:mm:ss",
            "fielddata":{"loading" : "lazy"} }
        }
    }";

如何将日期字段的数据类型从字符串更改为具有特定格式的日期类型。

我尝试更改字符串数据类型的映射,将其更改为 eager loading 和 not_analyzed from mapped,但它给出了以下错误:

{"root_cause":[{"type":"illegal_argument_exception","reason":"Mapper for [AppName] conflicts with existing mapping in other types:\n[mapper [AppName] has different [index] values, mapper [App
 different [doc_values] values, cannot change from disabled to enabled, mapper [AppName] has different [analyzer]]"}],"type":"illegal_argument_exception","reason":"Mapper for [AppName] conflict with
existing mapping in other types:\n[mapper [AppName] has different [index] values, mapper [AppName] has different [doc_values] values, cannot change from disabled to enabled, mapper [AppName]
rent [analyzer]]"},"status":400}

这是我的查询:

 curl -XPUT localhost:9200/logstash-*/_mapping/log?update_all_types -d "{
"properties":
    {"AppName":
        {
        "type": "string", 
        "index" : "not_analyzed",
        "fielddata":{"loading" : "eager"}
        }
    }
}"

但是,如果我将其从 not_analyzed 更改为 analyzed,它会给出 acknowledged=true 消息。如何更换分析仪。

【问题讨论】:

    标签: elasticsearch mapping


    【解决方案1】:

    您无法更改现有数据类型映射。正如 Elastic 文档所说:

    虽然您可以添加到现有映射,但不能更改现有字段映射。如果某个字段已存在映射,则该字段中的数据可能已被索引。如果您要更改字段映射,则索引数据将是错误的,并且无法正确搜索。

    我们可以更新映射以添加新字段,但不能更改 从已分析到未分析的现有字段。

    您唯一的选择是使用新映射创建一个新索引,并将旧索引中的数据重新索引到新索引。

    【讨论】:

      【解决方案2】:

      不,您不能更改单个字段定义。

      如果您想更改单个类型中单个字段的字段定义,您别无选择,只能重新索引索引中的所有文档。


      为什么不能更改映射?这篇文章Changing Mapping with Zero Downtime 解释,

      为了制作 您的数据可搜索,您的数据库需要知道什么类型的数据 每个字段包含以及应如何对其进行索引。

      如果你换一个 字段类型来自例如一个日期的字符串,该日期的所有数据 您已经索引的字段变得无用。一种方式或 另一个,您需要重新索引该字段。

      这不仅适用于 Elasticsearch,也适用于任何使用 搜索的索引。如果它不使用索引,那么它是 牺牲速度换取灵活性。


      当您索引具有不正确字段类型的文档时会发生什么?

      将尝试转换。如果不存在有效的转换,则抛出异常。

      Elasticsearch: The Definitive Guide 有一个关于示例的注释,输入了 string,但预期的是 long。将尝试转换。但是如果不存在有效的转换,仍然会抛出异常。

      [...] 如果该字段已经是 映射为 long 类型,然后 ES 将尝试转换字符串 变成long,如果不能,则抛出异常。


      我是否可以对文档进行索引,忽略格式错误的字段?

      是的。 ES5 提供了一个ignore_malformed mapping 参数。 Elasticsearch Reference 解释说,

      尝试将错误的数据类型索引到字段中会引发异常 默认,并拒绝整个文档。 ignore_malformed 参数,如果设置为 true,则允许忽略异常。 该 格式错误的字段未编入索引,但文档中的其他字段已编入索引 正常处理。

      【讨论】:

        猜你喜欢
        • 2023-02-18
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多