ES索引修改mappings与重建reindex详解之修改字段类型

概要

elasticsearch一直在使用,这里总结一下mappings的修改方法,分为两种情况:

  1. 增加新的字段,这种很简单;
  2. 修改已有的字段类型,这种就比较麻烦了,需要reindex,对索引进行迁移重建。

一、创建索引

curl -XPUT 'http://127.0.0.1:9200/test?pretty' -H  'Content-Type: application/json' -d '{"settings":{},"mappings":{}}'
{
	"settings": {
		"index": {
			"number_of_shards": 2,
			"number_of_replicas": 3,
			"analysis": {
				"analyzer": {
					"char_analyzer": {
						"filter": ["lowercase"],
						"type": "custom",
						"tokenizer": "char_split"
					}
				},
				"tokenizer": {
					"char_split": {
						"token_chars": ["letter", "digit", "punctuation", "symbol"],
						"min_gram": "1",
						"type": "nGram",
						"max_gram": "1"
					}
				}
			}
		}
	},
	"mappings": {
		"doc": {
			"properties": {
				"id": {
					"type": "long"
				},
				"pd_name": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					},
					"analyzer": "char_analyzer"
				},
				"pd_uname": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"product_id": {
					"type": "long"
				}
			}
		}
	}
}
1.1、获取mappings
curl -XGET 'http://127.0.0.1:9200/test/_mappings?pretty'
{
  "test" : {
    "mappings" : {
      "doc" : {
        "properties" : {
          "id" : {
            "type" : "long"
          },
          "pd_name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            },
            "analyzer" : "char_analyzer"
          },
          "pd_uname" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "product_id" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

二、新增字段修改mappings

增加一个 new_stocks 字段,如下:

curl -XPUT 'http://127.0.0.1:9200/test/doc/_mapping?pretty' -H  'Content-Type: application/json' -d '{"properties":{"new_stocks":{"type":"nested","properties":{"value":{"type":"long"},"conversion":{"type":"long"}}}}}'
{
  "acknowledged" : true
}

再查一下:

curl -XGET 'http://127.0.0.1:9200/test/_mappings?pretty'                                                                                                          {
  "test" : {
    "mappings" : {
      "doc" : {
        "properties" : {
          "id" : {
            "type" : "long"
          },
          "new_stocks" : {
            "type" : "nested",
            "properties" : {
              "conversion" : {
                "type" : "long"
              },
              "value" : {
                "type" : "long"
              }
            }
          },
          "pd_name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            },
            "analyzer" : "char_analyzer"
          },
          "pd_uname" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "product_id" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

可以看到new_stocks字段已经加上去了。

三、修改OR删除mappings已有字段

如果想把product_id字段类型由long改成text,并删除id字段呢?此时用第二章节的方案就不行了,需要借用reindex命令重做索引。

3.1、创建新索引,将要改字段加进去
curl -XPUT 'http://127.0.0.1:9200/new_test?pretty' -H  'Content-Type: application/json' -d '{"settings":{},"mappings":{}}'

{
	"settings": {
		"index": {
			"number_of_shards": 2,
			"number_of_replicas": 3,
			"analysis": {
				"analyzer": {
					"char_analyzer": {
						"filter": ["lowercase"],
						"type": "custom",
						"tokenizer": "char_split"
					}
				},
				"tokenizer": {
					"char_split": {
						"token_chars": ["letter", "digit", "punctuation", "symbol"],
						"min_gram": "1",
						"type": "nGram",
						"max_gram": "1"
					}
				}
			}
		}
	},
	"mappings": {
		"doc": {
			"properties": {
				"id": {
					"type": "long"
				},
				"pd_name": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					},
					"analyzer": "char_analyzer"
				},
				"pd_uname": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"product_id": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				}
			}
		}
	}
}
3.2、同步数据
curl -XPOST 'http://127.0.0.1:9200/_reindex?pretty' -H  'Content-Type: application/json' -d '{"source":{"index":"test"},"dest":{"index":"new_test"}}'

3.3、删除原索引并对新索引重命名
#删除
curl -XDELETE 'http://127.0.0.1:9200/test?pretty'
#设置别名
curl -XPOST 'http://127.0.0.1:9200/_aliases?pretty' -H  'Content-Type: application/json' -d '{"actions":[{"add":{"index":"new_test","alias":"test"}}]}'

3.4、同步数据的技巧
  1. 同步部分字段
#从源头过滤, _source 参数,只同步 id,pd_name两个字段
curl -XPOST 'http://127.0.0.1:9200/_reindex?pretty' -H  'Content-Type: application/json' -d '{"source":{"index":"test","_source":["id","pd_name"]},"dest":{"index":"new_test"}}'
#从目的地移除 脚本控制,移除id字段
curl -XPOST 'http://127.0.0.1:9200/_reindex?pretty' -H  'Content-Type: application/json' -d '{"source":{"index":"test"},"dest":{"index":"new_test"},"script":{"lang":"groovy","inline":"ctx._source.remove(\"id\");ctx._source.remove(\"pd_uname\")"}}'

  1. 提高同步速率
    调整参数size大小,默认1000每批
curl -XPOST 'http://127.0.0.1:9200/_reindex?pretty' -H  'Content-Type: application/json' -d '{"source":{"index":"test","size":5000},"dest":{"index":"new_test"}}'

四、参考文献

1]:官方文档
2]:ES索引重建reindex详解
3]:ES迁移效率

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值