一、集群管理
集群狀態查詢
# 無密碼訪問
$ curl 'localhost:9200/_cat/health?v'
$ curl 'localhost:9200/_cat/nodes?v'
$ curl 'localhost:9200/_cat/nodes?v&h=hc,hm,rc,rm'
# 有密碼訪問
$ curl 'localhost:9200/_cat/health?v' --user elastic:admin@123
$ curl 'localhost:9200/_cat/nodes?v' --user elastic:admin@123
$ curl 'localhost:9200/_cat/nodes?v&h=hc,hm,rc,rm' --user elastic:admin@123
啟動節點
$ bin/elasticsearch -d
關閉節點
$ curl -XPOST 'http://localhost:9200/_cluster/nodes/n2/_shutdown'
$ curl -XPOST 'http://localhost:9200/_shutdown'
$ curl -XPOST 'http://localhost:9200/_cluster/nodes/_local/_shutdown?delay=10s'
二、索引管理
$ curl 'localhost:9200/_cat/indices?v'
# 查詢索引狀態
$ curl -XDELETE http://localhost:9200/索引名稱
{"acknowledged":true}
# 刪除指定索引
$ curl -XDELETE http://localhost:9200/索引名稱1,索引名稱2
# 刪除多個索引,中間用逗號隔開
$ curl -XDELETE -u elastic:changeme http://localhost:9200/索引名前綴*
# 模糊匹配刪除
# 使用通配符,刪除所有索引
$ curl -XDELETE http://localhost:9200/_all
$ curl -XDELETE http://localhost:9200/*
# 二選一都可以
# _all ,* 通配所有的索引,通常不建議使用通配符,誤刪了后果就很嚴重了,所有的index都被刪除了,禁止通配符為了安全起見,可以在elasticsearch.yml配置文件中設置禁用_all和*通配符`action.destructive_requires_name = true`這樣就不能使用_all和*了!
創建別名
$ curl -XPOST localhost:9200/_aliases -d '
{
"actions": [
{ "add": {
"alias": "goods",
"index": "goods_v1"
}}
]
}'
刪除並更新別名
$ curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "goods_v2", "alias" : "goods" } },
{ "add" : { "index" : "goods_v1", "alias" : "goods" } }
]
}'
$ curl -XGET 'localhost:9200/_cat/aliases'
# 查看已有別名
$ curl -XGET 'localhost:9200/_alias/help'
# 查看別名對應的索引
創建mapping
curl -XPOST http://localhost:9200/goods_v1/fulltext/_mapping -d'
{
"fulltext": {
"_all": {
"indexAnalyzer": "ik",
"searchAnalyzer": "ik_syno",
"term_vector": "no",
"store": "false"
},
"properties": {
"sku_id" :{
"type": "string"
},
"product_id":{
"type": "string"
},
"product_name":{
"type": "string",
"store": "no",
"term_vector": "with_positions_offsets",
"indexAnalyzer": "ik",
"searchAnalyzer": "ik_syno",
"include_in_all": "true",
"boost": 8
}
}
}
}'
$ curl -XGET 'http://localhost:9200/help/_mapping/fulltext?pretty'
# 獲取mapping
$ curl 'localhost:9200/_cat/shards?v'
$ curl -XGET 'http://localhost:9200/_cat/shards' | grep INIT
# 查看分區
三、文檔管理
$ curl -XGET 'localhost:9200/goods/fulltext/S201406251699?pretty'
# 獲取文檔
$ curl -XDELETE 'localhost:9200/goods/fulltext/1?pretty'
# 刪除文檔
獲取索引中前10個文檔
curl -XPOST 'localhost:9200/goods/_search?pretty' -d '
{
"query": { "match_all": {} }
}'
四、緩存管理
創建時顯式開啟緩存
curl -XPUT localhost:9200/my_index -d'
{
"settings": {
"index.cache.query.enable": true
}
}'
更新設置開啟緩存
curl -XPUT localhost:9200/goods/_settings -d'
{ "index.cache.query.enable": true }'
$ curl 'localhost:9200/_nodes/stats/indices/query_cache?pretty&human'
# 查詢各節點緩存狀態
五、索引副本
$ curl -XPUT -H 'Content-Type: application/json' 'http://192.168.100.134:9200/entbaseinfo_ceb20200619/_settings' -d'
{
"number_of_replicas": 1
}'
# 指定索引entbaseinfo_ceb20200619副本數為1
六、Es平衡機制
$ curl -XPUT -H 'Content-Type: application/json' '192.168.100.134:9200/_cluster/settings' -d'
{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}'
# 開啟es平衡機制
$ curl -XPUT -H 'Content-Type: application/json' '192.168.100.134:9200/_cluster/settings' -d'
{
"transient" : {
"cluster.routing.allocation.enable" : "none"
}
}'
# 關閉es平衡機制