3

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

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

帮我解决这个问题。

更新

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

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

2 回答 2

3

除了少数例外,不能更新映射。有一些例外:

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

因此,如果您希望在rating不重新创建新索引的情况下将字段从字符串转换为整数,那么您唯一的解决方案是创建一个类型为 的子字段(例如,称为rating.intinteger

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

于 2015-12-01T05:38:41.170 回答
1

1)您可以在不重新索引的情况下更改字段的数据类型,但问题是它不会影响您的数据,即rating字段将在string文档存储时保留,immutable segments但新添加的字段将integer再次出现,但这不会解决您的问题

2)您可以从当前索引中删除所有文档,然后PUT API像这样更改映射

$ curl -X PUT 'http://localhost:9200/your_index/your_type/_mapping?ignore_conflicts=true' -d 
'{
  "your_type": {
    "properties": {
      "rating": {
        "type": "integer"
      }
    }
  }
}'

然后重新索引

但更好的是使用上述映射创建新索引并在零停机时间的情况下重新索引

于 2015-12-01T05:41:05.973 回答