如何更新Elasticsearch中的映射以更改字段数据类型并更改字符串中的分析器类型

PRI*_*PAI 12 mapping elasticsearch

在尝试更新映射时,我收到以下错误:

{"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}
Run Code Online (Sandbox Code Playgroud)

我试图在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"} }
        }
    }";
Run Code Online (Sandbox Code Playgroud)

如何使用特定格式将日期字段的数据类型从字符串更改为日期类型.

我试图更改字符串数据类型的映射以将其更改为eager加载和not_analyzed分析,但它会出现以下错误:

{"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}
Run Code Online (Sandbox Code Playgroud)

这是我的相同查询:

 curl -XPUT localhost:9200/logstash-*/_mapping/log?update_all_types -d "{
"properties":
    {"AppName":
        {
        "type": "string", 
        "index" : "not_analyzed",
        "fielddata":{"loading" : "eager"}
        }
    }
}"
Run Code Online (Sandbox Code Playgroud)

但是,如果我将其更改not_analyzedanalyzed它会给出一条acknowledged=true消息.如何更改分析仪?

isr*_*lst 15

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

虽然您可以添加到现有映射,但无法更改现有字段映射.如果字段已存在映射,则该字段中的数据可能已编入索引.如果要更改字段映射,则索引数据将出错并且无法正确搜索.

我们可以更新映射以添加新字段,但我们无法将现有字段从已分析更改为not_analyzed.

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


sam*_*sam 5

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

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


为什么不能更改映射?这篇文章以零停机时间更改映射解释说,

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

如果您将字段类型从例如字符串切换为日期,则该字段的所有已编入索引的数据都将变得无用。一种或另一种方式,您需要重新索引该字段。

这不仅适用于 Elasticsearch,也适用于任何使用索引进行搜索的数据库。如果它不使用索引,那么它就是为了灵活性而牺牲速度。


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

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

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

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


我可以将文档编入索引,而忽略格式错误的字段吗?

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

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