目录
es版本为6.7.2,kibana版本6.7.2
一、索引操作
1、添加索引
#带结构添加索引
PUT user
{
"mappings": {
"doc": {
"properties": {
"name":{
"type": "text",
"analyzer": "ik_max_word"
},
"age":{
"type": "long"
},
"address":{
"type": "keyword"
}
}
}
}
}
#在es7后就不存在type类型了,直接创建索引即可
#不带结构添加索引
PUT user
成功结果:
2、查看索引结构
GET user/_mapping
成功结果:
3、查看索引是否存在
HEAD user
存在时:
不存在时:
4、删除索引
DELETE user
成功结果:
5、修改索引
mappings一旦创建,其字段类型就不能再被修改。对于需要修改字段的场景,一般都是建新的mapping,把原有的数据拷贝过来,字段虽然不能再修改,但可以新增新的字段。
5.1 创建新的索引映射
PUT user_new
{
"mappings": {
"doc": {
"properties": {
"name":{
"type": "text",
"analyzer": "ik_max_word"
},
"age":{
"type": "long"
},
"address":{
"type": "text"
},
"phone":{
"type": "keyword"
}
}
}
}
}
5.2 使用Reindex API进行数据迁移:
POST _reindex
{
"source": {
"index": "user"
},
"dest": {
"index": "user_new"
}
}
成功结果:
5.3 删除旧索引user
DELETE user
5.4 修改新索引 user_new 名字为 user
POST _aliases
{
"actions": [
{
"add": {
"index": "user_new",
"alias": "user"
}
}
]
}
成功结果:
注意:如果新索引的字段名 和 旧索引的字段名 不相同,该怎么迁移数据
场景:user_new这个新索引的地址字段是addr ,而老索引里是address,执行数据迁移时需要特殊指定reindex的迁移字段转换逻辑。
# 步骤1: 创建新索引
PUT /新索引名(user_new)
{
"mappings": {
"properties": {
"new_field": {
"type": "text"
}
// 其他新字段映射...
}
}
}
# 步骤2: 使用_reindex API复制数据
POST /_reindex
{
"source": {
"index": "旧索引名(user)"
},
"dest": {
"index": "新索引名(user_new)"
},
"script": {
"source": "ctx._source.新字段名(addr)= ctx._source.remove('旧字段名(address)')",
"lang": "painless"
}
}
# 步骤3: 确认数据迁移成功
GET /新索引名(user_new)/_search
# 步骤4: 如果一切正常,可以删除旧索引
DELETE /旧索引名(user)