ElasticSearch常用命令
阿新 • • 發佈:2020-08-01
狀態查詢
- 獲取所有
_cat
系列的操作
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
則讓輸出縮排更規範
叢集狀態
- 叢集狀態
curl -X GET "localhost:9200/_cluster/health?pretty"
節點狀態
- 節點簡要資訊
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
- 節點詳細資訊
curl -X GET "localhost:9200/_nodes/stats/http?pretty"
後面的http是檢視的屬性,另外還有
indices, fs, http, jvm, os, process, thread_pool, discovery
等,支援組合(如indices,fs,http
)
分片狀態
- 分片
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
索引
索引管理
- 索引列表
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
條件過濾:
_cat/indices?v&health=yellow
排序:
_cat/indices?v&health=yellow&s=docs.count:desc
- 索引詳細資訊
curl -X GET "localhost:9200/chat_index_alias/_stats?pretty"
- 資料量
curl -X GET "localhost:9200/_cat/count/chat_index_alias?v&pretty"
- 新建索引
curl -X PUT "localhost:9200/my_index" -d '
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'
- 刪除索引
curl -X DELETE "localhost:9200/tenmao_index"
curl -X DELETE "localhost:9200/tenmao_index_1504520299944"
索引使用
- 分詞搜尋
curl -X POST "localhost:9200/chat_index_alias/_search" -d '
{
"query": {
"match": {
"question": "吃飯了嗎"
}
}
}'
- 完全匹配搜尋
curl -X POST "localhost:9200/chat_index_alias/_search" -d '
{
"query": {
"match_phrase": {
"question": "你吃飯了"
}
}
}'
別名
- 檢視別名
curl -X GET "localhost:9200/_alias/chat_index_alias?pretty"
- 增加別名
curl -X PUT "localhost:9200/my_index/_alias/my_index_alias?pretty"
- 刪除別名
curl -X POST 'http://localhost:9200/_aliases' -d '
{
"actions": [
{"remove": {"index": "my_index", "alias": "my_index_alias"}}
]
}'
一般純刪除別名使用的比較少,一般是別名重新繫結(刪除和繫結為一個原子操作)
- 別名重新繫結
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "my_index", "alias" : "my_index_alias" } },
{ "add" : { "index" : "my_index_v2", "alias" : "my_index_alias" } }
]
}'