删除字段并将新字段添加到弹性搜索索引中的映射

2024-01-06

我在第一次为一个字段创建索引时犯了一个错误。我错误地将“评级”字段指定为“字符串”,而不是“整数”数据类型。但该字段中存储的数据只是整数。当我尝试计算平均评分聚合时,由于字符串数据类型,它引发了错误。

  1. 有没有办法在不重新索引的情况下更改字段的数据类型?
  2. 如果不重新索引就不可能,如何删除评级字段并添加具有“整数”数据类型的评级字段?

帮我解决这个问题。

Update

使用以下命令删除索引中的类型

curl -XDELETE 'http://localhost:9300/feedbacks_16/responses'

删除了类型并创建了具有相同名称的类型,并更改了我的评级字段的数据类型并重新索引了整个数据。一切都很顺利,直到重新索引为止。但平均查询不起作用。 以下是我收到的错误:

{ "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[2NsGsPYLR2eP9p2zYSnKGQ][feedbacks-16][0]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[2NsGsPYLR2eP9p2zYSnKGQ][feedbacks_16][1]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[pcwXn3X-TceUO0ub29ZFgA][feedbacks_16][2]: RemoteTransportException[[tsles02][inet[/localhost:9300]][indices:data/read/search[phase/query]]]; nested: ClassCastException; }{[pcwXn3X-TceUO0ub29ZFgA][feedbacks_16][3]: RemoteTransportException[[tsles02][inet[/localhost:9300]][indices:data/read/search[phase/query]]]; nested: ClassCastException; }{[2NsGsPYLR2eP9p2zYSnKGQ][feedbacks_16][4]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}]", "status": 500 }

除了少数例外,映射无法更新 https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#updating-field-mappings。有一些例外:

  • 您可以添加新属性
  • 您可以将简单字段提升为多字段
  • 你可以禁用doc_values(但不启用它们)
  • 你可以更新ignore_above范围

所以如果你想改变你的rating字段从字符串到整数而不重新创建新索引,唯一的解决方案是创建一个子字段(例如称为rating.int) 类型integer.

请注意,您仍然需要重新索引数据才能填充该新子字段。但是,如果这样做,最好从头开始重新创建一个干净的索引并重新填充它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

删除字段并将新字段添加到弹性搜索索引中的映射 的相关文章

随机推荐