Elasticsearch常用命令
以下是 Elasticsearch 查看集群状态配置和索引完整操作流程的详细命令:
一、查看集群状态与配置
1. 集群健康状态
curl -X GET "localhost:9200/_cluster/health?pretty"
关键参数:
level=indices:显示每个索引的健康状态
level=shards:显示每个分片的详细状态健康状态说明:
- `green`:所有主分片和副本分片都正常。
- `yellow`:主分片正常,但至少有一个副本分片缺失。
- `red`:至少有一个主分片不可用。
输出示例:
{"cluster_name": "my_cluster","status": "green", # 集群状态:green/yellow/red"number_of_nodes": 3,"number_of_data_nodes": 2,"active_primary_shards": 15,"active_shards": 30,...
}
2. 集群节点信息
curl -X GET "localhost:9200/_nodes?pretty"
_nodes/stats:查看节点统计信息(CPU、内存、JVM 等)
_nodes/process:查看进程信息
3. 集群状态详细信息
curl -X GET "localhost:9200/_cluster/state?pretty"
包含分片分配、元数据、路由表等信息
4. 集群设置(动态配置)
#获取当前动态设置
curl -X GET "localhost:9200/_cluster/settings?include_defaults=true&pretty"#临时修改集群块级别(禁止写操作)
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{"transient": {"cluster.blocks.read_only_allow_delete": true}
}
'
二、索引操作完整流程
1. 创建索引(带映射)
curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
{"settings": {"number_of_shards": 3, # 主分片数量"number_of_replicas": 1 # 每个主分片的副本数},"mappings": {"properties": {"title": { "type": "text" },"user": {"type": "nested", # 嵌套对象"properties": {"id": { "type": "keyword" },"name": { "type": "text" }}},"timestamp": { "type": "date" }}}
}
'
创建索引别名
POST /_aliases
{
"actions":[{"add": {"index": "my_index","alias": "my_alias"} ]
}#删除别名
POST /_aliases
{
"actions":[{"remove": {"index": "my_index","alias": "my_alias"} ]
}#更新别名POST /_aliases
{