DSL:索引操作

索引模板

在创建索引时,为每个索引写定义信息可能是一件繁琐的事情,ES提供了索引模板功能,让你可以定义一个索引模板,模板中定义好settings、mapping、以及一个模式定义来匹配创建的索引。

注意:模板只在索引创建时被参考,修改模板不会影响已创建的索引

新增/修改名为tempae_1的模板,匹配名称为te* 或 bar*的索引创建:

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}
View Code

查看索引模板

GET /_template/template_1
GET /_template/temp* 
GET /_template/template_1,template_2
GET /_template
View Code

删除模板

DELETE /_template/template_1
View Code

Open/Close Index 打开/关闭索引

POST /my_index/_close
POST /my_index/_open
说明:

关闭的索引不能进行读写操作,几乎不占集群开销。
关闭的索引可以打开,打开走的是正常的恢复流程。

 

修改索引的settings信息

索引的设置信息分为静态信息和动态信息两部分。静态信息不可更改,如索引的分片数。动态信息可以修改。

REST 访问端点:

/_settings 更新所有索引的。

{index}/_settings 更新一个或多个索引的settings。

详细的设置项请参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings

Shrink Index 收缩索引

索引的分片数是不可更改的,如要减少分片数可以通过收缩方式收缩为一个新的索引。

新索引的分片数必须是原分片数的因子值,如原分片数是8,则新索引的分片数可以为4、2、1 。

什么时候需要收缩索引呢?

最初创建索引的时候分片数设置得太大,后面发现用不了那么多分片,这个时候就需要收缩了

收缩的流程:

先把所有主分片都转移到一台主机上;

在这台主机上创建一个新索引,分片数较小,其他设置和原索引一致;

把原索引的所有分片,复制(或硬链接)到新索引的目录下;

对新索引进行打开操作恢复分片数据;

(可选)重新把新索引的分片均衡到其他节点上。

收缩前的准备工作:

将原索引设置为只读;

将原索引各分片的一个副本重分配到同一个节点上,并且要是健康绿色状态

PUT /my_source_index/_settings
{
  "settings": {
    <!-- 指定进行收缩的节点的名称 -->
    "index.routing.allocation.require._name": "shrink_node_name",
    <!-- 阻止写,只读 -->
     "index.blocks.write": true
  }
}
View Code

进行收缩:

POST my_source_index/_shrink/my_target_index
{
  "settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 1,
    "index.codec": "best_compression"
  }}
View Code

监控收缩过程:

GET _cat/recovery?v
GET _cluster/health
View Code

Split Index 拆分索引

当索引的分片容量过大时,可以通过拆分操作将索引拆分为一个倍数分片数的新索引。

能拆分为几倍由创建索引时指定的index.number_of_routing_shards 路由分片数决定。这个路由分片数决定了根据一致性hash路由文档到分片的散列空间。

如index.number_of_routing_shards = 30 ,指定的分片数是5,则可按如下倍数方式进行拆分:

5 → 10 → 30 (split by 2, then by 3)
5 → 15 → 30 (split by 3, then by 2)
5 → 30 (split by 6)

为什么需要拆分索引?

当最初设置的索引的分片数不够用时就需要拆分索引了,和压缩索引相反

注意:只有在创建时指定了index.number_of_routing_shards 的索引才可以进行拆分,ES7开始将不再有这个限制。

和solr的区别是,solr是对一个分片进行拆分,es中是整个索引进行拆分。

拆分步骤:

准备一个索引来做拆分:

PUT my_source_index
{
    "settings": {
        "index.number_of_shards" : 1,
        <!-- 创建时需要指定路由分片数 -->
        "index.number_of_routing_shards" : 2
    }
}
View Code

先设置索引只读:

PUT /my_source_index/_settings
{
  "settings": {
    "index.blocks.write": true
  }
}
View Code

做拆分:

POST my_source_index/_split/my_target_index
{
  "settings": {
    <!--新索引的分片数需符合拆分规则-->
    "index.number_of_shards": 2
  }
}
View Code

监控拆分过程:

GET _cat/recovery?v
GET _cluster/health
View Code

 

 

 

posted @ 2020-08-26 08:39  弱水三千12138  阅读(254)  评论(0)    收藏  举报