1. 索引库配置
所谓的settings就是用来修改索引分片和副本数的;
比如有的重要索引, 副本数很少甚至没有副本, 那么我们通过setting来添加副本数.
- 新建一个索引库 document
DELETE document
PUT document
{
"mappings": {
"article" : {
"properties":
{
"title" : {"type": "text"} ,
"author" : {"type": "text"} ,
"titleScore" : {"type": "double"}
}
}
}
}
- 查看副本数
GET /document/_settings
可以看到当前的副本数是1,
- 那么为了提供容错性, 我们可以把副本数改为2;
PUT /document/_settings
{
"number_of_replicas": 2
}
- 副本数能够修改,分片数无法修改,修改分片数会报错
PUT /document/_settings
{
"number_of_shards": 3
}
2. 零停机复制索引库,通过复制索引库来改变分片数
实际生产,对于文档的操作,偶尔会遇到这种问题:
我们发现,setting设置无法修改分片数,但是如果我们后期数据量增多了, 集群规模大了,就要增加分片数来适应大的集群, 怎么办呢? 前提是es集群不能停机
那么我们就复制一个索引库, 对新索引库设置新的配置, 将旧索引库的数据拷贝到新索引库中.
- 新建一个索引库articles1, 并添加数据
DELETE articles1
PUT articles1
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"article":{
"dynamic":"strict",
"properties":{
"id":{"type": "text", "store": true},
"title":{"type": "text","store": true},
"readCounts":{"type": "integer","store": true},
"times": {"type": "text", "index": false}
}
}
}
}
PUT articles1/article/1
{
"id" : "1",
"title" : "世界1",
"readCounts" : 2 ,
"times" : "2018-05-01"
}
get articles1/article/1
- 新建索引库articles2
DELETE articles2
PUT articles2
{
"settings":{
"number_of_shards":5,
"number_of_replicas":1
},
"mappings":{
"article":{
"dynamic":"strict",
"properties":{
"id":{"type": "text", "store": true},
"title":{"type": "text","store": true},
"readCounts":{"type": "integer","store": true},
"times": {"type": "date", "index": false}
}
}
}
}
}
GET articles2/article/1
- 将articles1的数据拷贝到articles2中
POST _reindex
{
"source": {
"index": "articles1"
},
"dest": {
"index": "articles2"
}
}
GET articles2/article/1