Skip to content

Basic Usage

Get Health

bash
curl -s "http://localhost:9200/_cluster/health?pretty"
json
{
  "cluster_name" : "docker-cluster",  // 集群名称
  "status" : "green",                 // 集群健康状态,green 表示正常
  "timed_out" : false,                // 查询集群健康时是否超时,false 表示未超时
  "number_of_nodes" : 1,              // 集群中节点总数
  "number_of_data_nodes" : 1,         // 集群中数据节点数量
  "active_primary_shards" : 1,        // 当前激活的主分片数
  "active_shards" : 1,                // 当前激活的所有分片数(主分片+副本分片)
  "relocating_shards" : 0,            // 正在迁移中的分片数
  "initializing_shards" : 0,          // 正在初始化中的分片数
  "unassigned_shards" : 0,            // 未分配的分片数
  "delayed_unassigned_shards" : 0,    // 延迟分配的未分配分片数
  "number_of_pending_tasks" : 0,      // 等待执行的集群任务数
  "number_of_in_flight_fetch" : 0,    // 正在从远程节点拉取数据的任务数
  "task_max_waiting_in_queue_millis" : 0,    // 集群任务队列中最长等待时间(毫秒)
  "active_shards_percent_as_number" : 100.0  // 激活分片占总分片的百分比(数值)
}

Get Index List

bash
curl -X GET "localhost:9200/_cat/indices?v"
txt
health status index            uuid                   pri rep docs.count docs.deleted store.size pri.store.size dataset.size
green  open   teletrace-traces wcEe4_6MRkm32hLvNRZTDg   1   0         81            0    332.2kb        332.2kb      332.2kb
Pretty Output
txt
health status:  green  // 索引健康状态,green 表示正常
index status:   open   // 索引状态,open 表示打开
index name:     teletrace-traces       // 索引名称
uuid:           wcEe4_6MRkm32hLvNRZTDg // 索引的唯一标识符
pri:            1    // 主分片数量
rep:            0    // 副本分片数量
docs.count:     81   // 文档总数
docs.deleted:   0    // 已删除的文档数
store.size:     332.2kb  // 索引总存储大小
pri.store.size: 332.2kb  // 主分片存储大小
dataset.size:   332.2kb  // 数据集大小

Get Index Doc

bash
curl -X GET "localhost:9200/index_name/_search?size=1&pretty"

Get Index Mapping

Full structure.

bash
curl -X GET "localhost:9200/index_name/_mapping?pretty"

Get Index Mapping Fields

Flatten field list, field name and type.

bash
curl -X GET "localhost:9200/index_name/_mapping/field/*"

Get Settings

bash
curl -s localhost:9200/_cluster/settings?include_defaults=true ; echo
grep watermark
bash
cj | grep -C 10 watermark
json
"shard_state": {
  "reroute": {
    "priority": "NORMAL"
  }
},
"type": "balanced",
"disk": {
  "threshold_enabled": "true",
  "watermark": {
    "flood_stage": "95%",
    "high": "90%",
    "low": "85%",
    "enable_for_single_data_node": "false"
  },
  "include_relocations": "true",
  "reroute_interval": "60s"
},

Set Watermark

bash
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d '
{
  "persistent": {
    "cluster.routing.allocation.disk.watermark.low": "85%",
    "cluster.routing.allocation.disk.watermark.high": "90%",
    "cluster.routing.allocation.disk.watermark.flood_stage": "95%",
    "cluster.info.update.interval": "1m"
  }
}'

Index Size Order

bash
curl -s 'localhost:9200/_cat/indices?v&s=store.size:desc' | head -n 20

Delete by Days

bash
curl -X DELETE "localhost:9200/logstash-*-$(date -d '30 days ago' +'%Y.%m.%d')"

Clear Cache

bash
curl -X POST "localhost:9200/_cache/clear"