我在 Elastic 中有一个现有索引“my-index”。我正在尝试使用新字段更新此现有索引而不会丢失数据。
我还尝试使用 RestHighLevelClient 的 .putMapping 函数更新现有索引,但我仍然遇到这样的错误
Elasticsearch 异常 [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters:...
这是我现有索引的映射
{
"mappings": {
"_doc": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 420
}
}
},
"taskType": {
"type": "integer"
},
"updateTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"workType": {
"type": "integer"
}
}
}
}
}
我的预期指数是
{
"mappings": {
"_doc": {
"mappings": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
},
{
"dates": {
"match_mapping_type": "date",
"mapping": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
],
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 420
}
}
},
"date": {
"type": "long"
},
"favorite_color": {
"type": "keyword"
},
"is_active": {
"type": "boolean"
},
"number_float": {
"type": "float"
},
"number_long": {
"type": "long"
},
"taskType": {
"type": "integer"
},
"updateTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"updatedDate": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"workType": {
"type": "integer"
}
}
}
}
}
我可以使用 spring-data-elasticsearch 存储库更新现有索引而不丢失任何数据吗???感谢您的帮助。
您不能更改字段类型,也不能删除任何现有字段,但可以向现有映射添加其他字段。 这是关于它的官方文档。 ESv6 和 ESv8
举个例子:
创建索引
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"name": {
"properties": {
"first": {
"type": "text"
}
}
},
"user_id": {
"type": "keyword"
}
}
}
}
}
更新映射
PUT my_index/_mapping/_doc
{
"properties": {
"name": {
"properties": {
"last": {
"type": "text"
}
}
},
"user_id": {
"type": "keyword",
"ignore_above": 100
}
}
}
要在不丢失数据的情况下更新现有索引映射,您可以使用以下方法之一:
使用 reindex API 来创建一个新的索引和新的映射,并将数据从旧索引复制到新索引。
然后更改别名以指向新索引。
此方法可保留您的数据并允许您更改任何映射参数,但它需要额外的磁盘空间和时间来重新编制索引。 使用更新映射 API 添加新字段或更改一些支持
旧索引中现有字段的映射参数。此方法不需要重建索引或额外的磁盘空间,但它对您可以更改的内容有一些限制。例如,您不能更改现有字段的字段类型或分析器。
使用 spring-data-elasticsearch repository 使用 Java 代码执行上述任一方法。您可以在实体类上使用 @Mapping 注释来指定新索引的映射,并使用 ElasticsearchOperations.reindex() 方法重新索引您的数据
旧索引到新索引。您还可以使用 ElasticsearchOperations.putMapping() 方法通过一些支持的更改来更新现有的索引映射。
不能更改字段类型,也不能删除任何现有字段