ElasticSearch
用作全文檢索康嘉,一直沒有好好研究它的命令晦雨,每次使用的時候都要谷歌搜索,效率太低。 本文把一些特別常用的運維及操作命令整理一下互例,方便歸類記憶
狀態(tài)查詢
- 獲取所有
_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
奢入,讓輸出內(nèi)容表格顯示表頭;pretty
則讓輸出縮進更規(guī)范
集群狀態(tài)
- 集群狀態(tài)
curl -X GET "localhost:9200/_cluster/health?pretty"
節(jié)點狀態(tài)
- 節(jié)點簡要信息
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
- 節(jié)點詳細信息
curl -X GET "localhost:9200/_nodes/stats/http?pretty"
后面的http是查看的屬性,另外還有
indices, fs, http, jvm, os, process, thread_pool, discovery
等媳叨,支持組合(如indices,fs,http
)
分片狀態(tài)
- 分片
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"
- 數(shù)據(jù)量
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" } }
]
}'