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

时间:2015-12-01 05:26:29

标签: elasticsearch elasticsearch-mapping

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

  1. 有没有办法在没有重建索引的情况下更改字段的数据类型?
  2. 如果没有reindex,如果不可能,我如何删除评级字段并添加带有“整数”数据类型的评级字段?
  3. 帮我解决这个问题。

    更新

    使用以下命令

    删除索引中的类型
    curl -XDELETE 'http://localhost:9300/feedbacks_16/responses'
    

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

    { "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 }
    

2 个答案:

答案 0 :(得分:2)

除了少数例外,还有映射cannot be updated。有一些例外:

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

因此,如果您希望将rating字段从字符串转换为整数而不重新创建新索引,则唯一的解决方案是创建类型为{的子字段(例如,名为rating.int) {1}}。

请注意,您仍然需要重新编制数据索引,以便填充新的子字段。但是,如果您这样做,只需从头开始重新创建一个干净的索引并重新填充它就会好得多。

答案 1 :(得分: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"
      }
    }
  }
}'

然后重新索引

但更好的方法是使用上面的映射和reindex with zero downtime

创建新索引
相关问题