### 状态查询
---
- 常用命令
```
PUT _cluster/settings
{
"transient": {
"cluster.routing.allocation.enable": "all"
}
}
// 查看问题原因
GET /_cluster/allocation/explain?pretty
// 查看集群节点状态
GET _cat/nodeattrs?v&s=name
DELETE s_traefik-2022.02.21
// 查看索引的状态
GET _cat/nodes?v
GET _cat/shards?h=index,shard,prirep,state,unassigned.reason
// 查看索引的设置
GET _all/_settings
GET s_traefik-2022.02.22/_settings
// 查看生命周期策略
GET _ilm/policy/s_log-ilm
// 查看索引创建模版
GET _template
// 查看集群状态
GET _cluster/health?pretty
GET _cluster/settings?pretty
```
- 获取所有`_cat`系列的操作
```shell
curl http://localhost:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
```
>可以后面加一个v,让输出内容表格显示表头; pretty则让输出缩进更规范
- 集群状态
```shell
curl -X GET "localhost:9200/_cluster/health?pretty"
```
- 集群设置
```shell
curl 127.0.0.1:9200/_cluster/settings?pretty
```
- 节点简要信息
```shell
curl -X GET "localhost:9200/_cat/nodes?pretty&v"
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.58.101 69 99 71 12.67 12.25 11.71 mdi - node-101
192.168.58.103 23 99 70 14.64 13.45 12.68 mdi - node-103
192.168.58.105 60 97 69 11.17 10.96 10.88 mdi * node-105
```
- 节点详细信息
```shell
curl -X GET "localhost:9200/_nodes/stats/http?pretty"
```
>后面的http是查看的属性,另外还有indices, fs, http, jvm, os, process, thread_pool, discovery等,支持组合(如indices,fs,http)
- 分片状态
```shell
curl -X GET "localhost:9200/_cat/shards?v&pretty"
index shard prirep state docs store ip node
tenmao_index_153915944934 1 p STARTED 39931 4.1mb 172.17.0.14 35S66p1
tenmao_index_153915944934 1 r STARTED 39931 4mb 172.17.0.3 DPKsmMN
tenmao_index_153915944934 0 p STARTED 39634 4mb 172.17.0.2 PE8QHxz
tenmao_index_153915944934 0 r STARTED 39634 4mb 172.17.0.3 DPKsmMN
```
>分片中如果存在未分配的分片, 可以查看未分片的原因:_cat/shards?h=index,shard,prirep,state,unassigned.reason&v
- 索引列表
```shell
curl -X GET "localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open tenmao_index_153915944934 Z6BV1VaMRc-tC-7IucJE2w 5 1 198444 0 40.9mb 20.4mb
```
- 索引详细信息
```shell
curl -X GET "localhost:9200/chat_index_alias/_stats?pretty"
```
- 新建索引
```shell
curl -X PUT "localhost:9200/test_index" -d '
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'
```
- 删除索引
```shell
curl -X DELETE "localhost:9200/test_index"
```
- 分词搜索
```shell
curl -X POST "localhost:9200/test_index/_search" -d '
{
"query": {
"match": {
"question": "吃饭了吗"
}
}
}'
```
- 完全匹配搜索
```shell
curl -X POST "localhost:9200/test_index/_search" -d '
{
"query": {
"match_phrase": {
"question": "你吃饭了"
}
}
}'
```
ES常用命令