elasticsearch 更改已有字段的数据类型

本文介绍了在Elasticsearch中更改已有字段数据类型的方法。因未指定字段映射会导致动态映射出现数据类型错误,以更改索引blog中priority字段类型为例,详细说明了查看原有映射、新建索引、数据迁移、删除旧索引等步骤及对应命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

elasticsearch 更改已有字段的数据类型

【问题背景】:
在elasticsearch中,如果你没有指定字段映射,那么,elasticsearch将对为指定数据类型的字段做动态映射。
例如,当入库的前期数据字段result为数值型时,elasticsearch将其映射为long类型;当入库的后期数据字段result为字符串时,会报数据类型错误。

更改索引blog中的动态映射字段priority数据类型:由long更改为keyword

一:查看原有字段动态映射类型

查看mapping
命令:GET http://localhost:9002/index[索引名]/_mapping

只能创建index时手动建立mapping,或者新增field mapping,但是不能update field mapping

{
	"blog": {
		"mappings": {
			"article": {
				"properties": {
					"content": {
						"type": "text",
						"fields": {
							"keyword": {
								"type": "keyword",
								"ignore_above": 256
							}
						}
					},
					"counter": {
						"type": "long"
					},
					"id": {
						"type": "text",
						"fields": {
							"keyword": {
								"type": "keyword",
								"ignore_above": 256
							}
						}
					},
					"priority": {
						"type": "long"
					},
					"tags": {
						"type": "text",
						"fields": {
							"keyword": {
								"type": "keyword",
								"ignore_above": 256
							}
						}
					},
					"title": {
						"type": "text",
						"fields": {
							"keyword": {
								"type": "keyword",
								"ignore_above": 256
							}
						}
					}
				}
			}
		}
	}
}

二:更改指定字段类型,新建索引blog_new

命令:PUT http://localhost:9002/blog_new

{
	"mappings": {
		"article": {
			"properties": {
				"content": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"counter": {
					"type": "long"
				},
				"id": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"priority": {
					"type": "keyword"
				},
				"tags": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"title": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				}
			}
		}
	}
}

三:将blog数据reindex到blog_new

命令:POST http://localhost:9002/_reindex

{
  "source": {
    "index": "blog"
  },
  "dest": {
    "index": "blog_new"
  }
}

四:删除索引blog

命令:DELETE http://localhost:9002/blog

五:将blog_new数据reindex到blog

命令:POST http://localhost:9002/_reindex

{
  "source": {
    "index": "blog_new"
  },
  "dest": {
    "index": "blog"
  }
}

六:删除索引blog_new

命令:DELETE http://localhost:9002/blog_new

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值