当前位置:  首页>> 技术小册>> ElasticSearch零基础到实战

一、概述
Elasticsearch 提供了一系列集群管理 API,可以用来管理集群状态、节点、分片等信息。本文将介绍 Elasticsearch 集群管理 API 的使用,包括集群状态 API、节点信息 API、索引管理 API、分片管理 API 等。

二、集群状态 API

获取集群健康状态
通过 _cat/health API 可以获取集群的健康状态,包括集群名称、状态、节点数、分片数等信息。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 获取集群健康状态
  5. health_res = es.cat.health()
  6. print(health_res)

输出结果如下:

  1. yellow open my_index 1 1 1 0 4.7kb 2.3kb
  2. yellow open my_index_2 1 1 1 0 4.7kb 2.3kb
  3. yellow open my_index_3 1 1 1 0 4.7kb 2.3kb

获取集群节点信息
通过 _cat/nodes API 可以获取集群的节点信息,包括节点 ID、名称、IP 地址、角色等信息。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 获取集群节点信息
  5. nodes_res = es.cat.nodes()
  6. print(nodes_res)

输出结果如下:

  1. 127.0.0.1 16 57 8 0.24 0.15 0.13 cdhilmrstw - node-1
  2. 127.0.0.1 16 57 8 0.24 0.15 0.13 cdhilmrstw * node-2

三、索引管理 API

创建索引
通过 indices.create() API 可以创建一个新的索引,需要指定索引名称和索引设置等信息。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 创建索引
  5. mapping = {
  6. "properties": {
  7. "name": {"type": "keyword"},
  8. "age": {"type": "integer"}
  9. }
  10. }
  11. es.indices.create(index="my_index", body={"mappings": mapping})

删除索引
通过 indices.delete() API 可以删除一个索引。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 删除索引
  5. es.indices.delete(index="my_index")

查看索引列表
通过 _cat/indices API 可以查看当前集群中的所有索引。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 查看索引列表
  5. indices_res = es.cat.indices()
  6. print(indices_res)

输出结果如下:

  1. yellow open my_index 1 1 1 0 4.7kb 2.3

四、分片管理 API

查看分片状态
通过 _cat/shards API 可以查看当前集群中所有索引的分片状态。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 查看分片状态
  5. shards_res = es.cat.shards()
  6. print(shards_res)

输出结果如下:

  1. my_index 0 p STARTED 1 1.8kb 127.0.0.1 node-1
  2. my_index 0 r UNASSIGNED
  3. my_index_2 0 p STARTED 1 1.8kb 127.0.0.1 node-1
  4. my_index_2 0 r UNASSIGNED
  5. my_index_3 0 p STARTED 1 1.8kb 127.0.0.1 node-1
  6. my_index_3 0 r UNASSIGNED

手动分配分片
通过 cluster.reroute() API 可以手动分配分片,需要指定目标节点和分片信息等。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 手动分配分片
  5. body = {
  6. "commands": [
  7. {
  8. "allocate": {
  9. "index": "my_index",
  10. "shard": 0,
  11. "node": "node-1",
  12. "allow_primary": True
  13. }
  14. }
  15. ]
  16. }
  17. es.cluster.reroute(body=body)

暂停和恢复分片分配
通过 cluster.routing.allocation.enable 配置项可以控制是否允许分片分配。将其设置为 none 可以暂停分配,设置为 all 可以恢复分配。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 暂停分片分配
  5. es.cluster.put_settings(body={"transient": {"cluster.routing.allocation.enable": "none"}})
  6. # 恢复分片分配
  7. es.cluster.put_settings(body={"transient": {"cluster.routing.allocation.enable": "all"}})

五、节点管理 API

添加节点
通过 nodes.hot_threads() API 可以获取当前节点的热点线程信息,包括线程 ID、名称、状态、运行时间等。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 获取节点热点线程信息
  5. hot_threads_res = es.nodes.hot_threads(node_id="_local")
  6. print(hot_threads_res)

删除节点
通过 nodes.shutdown() API 可以关闭一个节点。

  1. from elasticsearch import Elasticsearch
  2. # 创建连接对象
  3. es = Elasticsearch()
  4. # 关闭节点
  5. es.nodes.shutdown(node_id="_local")

小结

本文介绍了 Elasticsearch 集群管理 API 的使用,包括集群状态 API、节点信息 API、索引管理 API、分片管理 API 等。这些 API 可以用来管理集群状态、节点、分片等信息,对于保证集群高可用、提高搜索性能等方面有很大的作用。在使用时需要注意安全性,防止恶意操作对集群造成损害


该分类下的相关小册推荐: