目录
目标
- 了解索引的aliases(别名)属性,通过设置aliases,在不停机的情况下实现更换索引的配置。
- 熟悉Reindex(索引重建)的语法,实现修改索引的mappings(映射)、分片计数,副本数量等。
ES版本信息
7.17.5
官方文档
Reindex APIhttps://www.elastic.co/guide/en/elasticsearch/reference/7.17/docs-reindex.html
相关术语
静态映射
ES在索引文档前可以设置文档的字段名称、字段数量,分词器等,这称为静态映射。
索引的别名
一个索引有不同的索引名称,这个名称放在aliases里面,用逗号分隔。
Reindex实战
全量复制
将文档从一个索引复制到另一个索引,比如将test_db索引复制到test_db2索引。
POST _reindex
{
"source": {
"index": "test_db"
},
"dest": {
"index": "test_db2"
}
}
根据条件复制
只复制province字段分词后有湖南的文档。
POST _reindex
{
"source": {
"index": "my-index-000001",
"query": {
"term": {
"province": "湖南"
}
}
},
"dest": {
"index": "my-new-index-000001"
}
}
增量复制
实质也是上述的条件复制,比如根据日期、时间,id等复制数据。将日期在2022-03-01至2022-04-30的文档复制过来。
POST _reindex
{
"source": {
"index": "my-index-000001",
"query": {
"range": {
"date": {
"from": "2022-03-01",
"to": "2022-04-30"
}
}
}
},
"dest": {
"index": "my-new-index-000001"
}
}
POST _reindex
{
"source": {
"index": "my-index-000001",
"query": {
"range": {
"date": {
"gte": "2022-03-01",
"lte": "2022-04-30"
}
}
}
},
"dest": {
"index": "my-new-index-000001"
}
}
复制部分字段
只复制省和市字段,其余字段忽略。
POST _reindex
{
"source": {
"index": "my-index-000001",
"_source": ["province", "city"]
},
"dest": {
"index": "my-new-index-000001"
}
}
迁移版本
上述基本用法中没有指定dest内的version_type属性,该属性默认为internal,即重置version=1,如果需要得到以前的老版本,则需要指定为external。
POST _reindex
{
"source": {
"index": "test_db"
},
"dest": {
"index": "test_db2",
"version_type" :"external"
}
}
复制指定数量的文档
只复制10条数据。
POST _reindex
{
"size": 10,
"source": {
"index": "test_db"
},
"dest": {
"index": "test_db2"
}
}
复制指定数量的文档并排序
比如:根据date和age字段排序,复制出前2条数据。官方不推荐这种做法。
POST _reindex
{
"size": 2,
"source": {
"index": "test_db",
"sort": {
"date": "desc",
"age":"desc"
}
},
"dest": {
"index": "test_db2"
}
}
更改mappings配置
将字段添加到现有映射
需求:在一个已经定义好的索引中加入新的字段。注意:该方法不能变更现有字段。
第一步:创建索引。
PUT /my-index-000001
{
"mappings": {
"dynamic": "strict",
"properties": {
"name": {
"type": "keyword",
"null_value": "value_is_null"
},
"sex": {
"type": "boolean"
},
"age": {
"type": "long"
}
}
},
"settings": {
"index": {
"analysis.analyzer.default.type": "ik_max_word"
}
}
}
第二步:实现需求。
PUT /my-index-000001/_mapping
{
"properties": {
"weight": {
"type": "float"
},
"height": {
"type": "float"
}
}
}
第三步:查看映射。
GET /my-index-000001/_mapping
在不停机的情况下实现更换索引的配置
第一步:创建索引,并在该索引下索引新文档。
PUT /test_db
PUT /test_db/_bulk
{ "index": { "_id": "1"} }
{"province": "湖南省","city": "长沙市","county":"天心区"}
{ "index": { "_id": "2"} }
{"province": "湖南省","city": "长沙市","county":"芙蓉区"}
{ "index": { "_id": "3"} }
{"province": "广东省","city": "广州市","county":"白云区"}
{ "index": { "_id": "4"} }
{"province": "湖北省","city": "武汉市","county":"江夏区"}
GET /test_db/_search
第二步:查看该索引的别名,发现该索引没有别名。
GET /test_db/_alias
第三步:创建新的索引,该索引就是我们的目标索引,可以配置我们想要的分片数、副本数、默认分词器,mappings静态映射等。
PUT /test_db2
{
"mappings": {
"properties": {
"province": {
"type": "text",
"copy_to": "fullAddress"
},
"city": {
"type": "text",
"copy_to": "fullAddress"
},
"county": {
"type": "text",
"copy_to": "fullAddress"
}
}
},
"settings": {
"analysis.analyzer.default.type":"ik_max_word",
"number_of_replicas": 1,
"number_of_shards": 1
}
}
第四步:索引文档到目标索引。
POST _reindex
{
"source": {
"index": "test_db"
},
"dest": {
"index": "test_db2"
}
}
第四步:删除旧索引。
DELETE test_db
第五步:为新索引设置别名,别名为旧的索引名。
PUT /test_db2/_alias/test_db
第六步:验证。
#查看数据
GET /test_db/_search
#查看映射
GET test_db2/_mapping
#查看基本配置
GET test_db2/_settings