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
{
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值