创建新索引
PUT /twitter
curl -X PUT "localhost:9200/twitter?pretty"
索引设置:创建的每个索引都可以有与之关联的特定设置。
PUT /twitter { "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } }
curl -X PUT "localhost:9200/twitter?pretty" -H 'Content-Type: application/json' -d' { "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } } '
索引设置-简化
PUT /twitter { "settings" : { "number_of_shards" : 3, "number_of_replicas" : 2 } }
curl -X PUT "localhost:9200/twitter?pretty" -H 'Content-Type: application/json' -d' { "settings" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } '
mappings:映射
PUT /test { "settings" : { "number_of_shards" : 1 }, "mappings" : { "properties" : { "field1" : { "type" : "text" } } } }
curl -X PUT "localhost:9200/test?pretty" -H 'Content-Type: application/json' -d' { "settings" : { "number_of_shards" : 1 }, "mappings" : { "properties" : { "field1" : { "type" : "text" } } } } '
Aliases:别名
PUT /test { "aliases" : { "alias_1" : {}, "alias_2" : { "filter" : { "term" : {"user" : "kimchy" } }, "routing" : "kimchy" } } }
curl -X PUT "localhost:9200/test?pretty" -H 'Content-Type: application/json' -d' { "aliases" : { "alias_1" : {}, "alias_2" : { "filter" : { "term" : {"user" : "kimchy" } }, "routing" : "kimchy" } } } '
Wait For active shards
默认情况下,索引创建只会在每个分片的主副本启动或请求超时时向客户端返回响应。
{ "acknowledged": true, "shards_acknowledged": true, "index": "test" }
改变只等待主分片启动的默认值:改变这个设置也会影响后续所有写操作的wait_for_active_shards值
PUT /test { "settings": { "index.write.wait_for_active_shards": "2" } }
curl -X PUT "localhost:9200/test?pretty" -H 'Content-Type: application/json' -d' { "settings": { "index.write.wait_for_active_shards": "2" } } '
PUT /test?wait_for_active_shards=2
curl -X PUT "localhost:9200/test?wait_for_active_shards=2&pretty"
删除指定索引
DELETE /twitter
curl -X DELETE "localhost:9200/twitter?pretty"
删除多个索引,中间用逗号隔开
curl -X DELETE http://localhost:9200/索引名称1,索引名称2
模糊匹配删除
curl -X DELETE -u elastic:changeme http://localhost:9200/索引名前缀*
使用通配符,删除所有索引
curl -X DELETE http://localhost:9200/_all curl -X DELETE http://localhost:9200/*
定时删除ES索引
30 2 * * * /usr/bin/curl -X DELETE http://localhost:9200/*-$(date -d '-3days' +'%Y.%m.%d') >/dev/null 2>&1
定时删除脚本
#!/bin/bash time=$(date -d '-3days' +'%Y.%m.%d') curl -X DELETE -u elastic:changeme http://localhost:9200/*-${time}
获取一个或多个索引信息
GET /twitter
curl -X GET "localhost:9200/twitter?pretty"
检查索引是否存在。
HEAD /twitter
curl -I "localhost:9200/twitter?pretty"
POST /twitter/_open
curl -X POST "localhost:9200/twitter/_open?pretty"
POST /twitter/_close
curl -X POST "localhost:9200/twitter/_close?pretty"
缩小索引:将现有索引收缩为具有较少主分片的新索引,例:my_source_index缩小为名为my_target_index的新索引。
POST /my_source_index/_shrink/my_target_index { "settings": { "index.routing.allocation.require._name": null, //清除从源索引复制的分配要求。 "index.blocks.write": null //清除从源索引复制的索引写入块。 } }
curl -X POST "localhost:9200/my_source_index/_shrink/my_target_index?pretty" -H 'Content-Type: application/json' -d' { "settings": { "index.routing.allocation.require._name": null, "index.blocks.write": null } } '
前提条件:可通过请求实现
- 索引必须是只读的。
- 索引中每个分片的副本必须驻留在同一个节点上。
- 集群健康状态必须为绿色。
PUT /my_source_index/_settings { "settings": { "index.routing.allocation.require._name": "shrink_node_name", "index.blocks.write": true } }
curl -X PUT "localhost:9200/my_source_index/_settings?pretty" -H 'Content-Type: application/json' -d' { "settings": { "index.routing.allocation.require._name": "shrink_node_name", "index.blocks.write": true } } '
settings+aliases:类似create index API,接受目标索引的设置和别名参数
POST /my_source_index/_shrink/my_target_index { "settings": { "index.number_of_replicas": 1, "index.number_of_shards": 1, "index.codec": "best_compression" }, "aliases": { "my_search_indices": {} } }
curl -X POST "localhost:9200/my_source_index/_shrink/my_target_index?pretty" -H 'Content-Type: application/json' -d' { "settings": { "index.number_of_replicas": 1, "index.number_of_shards": 1, "index.codec": "best_compression" }, "aliases": { "my_search_indices": {} } } '
创建或更新索引别名。
PUT /twitter/_alias/alias1
curl -X PUT "localhost:9200/twitter/_alias/alias1?pretty"
添加基于时间的别名
PUT /logs_20302801/_alias/2030
curl -X PUT "localhost:9200/logs_20302801/_alias/2030?pretty"
添加基于用户的别名
PUT /users { "mappings" : { "properties" : { "user_id" : {"type" : "integer"} } } }
curl -X PUT "localhost:9200/users?pretty" -H 'Content-Type: application/json' -d' { "mappings" : { "properties" : { "user_id" : {"type" : "integer"} } } } '
PUT /users/_alias/user_12 { "routing" : "12", "filter" : { "term" : { "user_id" : 12 } } }
curl -X PUT "localhost:9200/users/_alias/user_12?pretty" -H 'Content-Type: application/json' -d' { "routing" : "12", "filter" : { "term" : { "user_id" : 12 } } } '
创建索引时添加别名
可以使用 create index API 在索引创建过程中添加索引别名。
PUT /logs_20302801 { "mappings" : { "properties" : { "year" : {"type" : "integer"} } }, "aliases" : { "current_day" : {}, "2030" : { "filter" : { "term" : {"year" : 2030 } } } } }
curl -X PUT "localhost:9200/logs_20302801?pretty" -H 'Content-Type: application/json' -d' { "mappings" : { "properties" : { "year" : {"type" : "integer"} } }, "aliases" : { "current_day" : {}, "2030" : { "filter" : { "term" : {"year" : 2030 } } } } } '
检查索引别名是否存在。
HEAD /_alias/alias1
curl -I "localhost:9200/_alias/alias1?pretty"
返回有关一个或多个索引别名的信息。
GET /twitter/_alias/alias1
curl -X GET "localhost:9200/twitter/_alias/alias1?pretty"
单索引状态
GET /twitter/_stats
curl -X GET "localhost:9200/twitter/_stats?pretty"
多索引状态
GET /index1,index2/_stats
curl -X GET "localhost:9200/index1,index2/_stats?pretty"
全部索引状态
GET /_stats
curl -X GET "localhost:9200/_stats?pretty"
返回所有索引的合并和刷新统计信息。
GET /_stats/merge,refresh
curl -X GET "localhost:9200/_stats/merge,refresh?pretty"
返回 group1 和 group2 搜索组的搜索统计信息。
GET /_stats/search?groups=group1,group2
curl -X GET "localhost:9200/_stats/search?groups=group1,group2&pretty"
单索引段信息:返回有关索引分片中 Lucene 段的低级信息。
GET /test/_segments
curl -X GET "localhost:9200/test/_segments?pretty"
http://41.1.31.21:39200/_cat/segments/vehicle_pic_index_20200629-20200705?v
多个索引段信息
GET /test1,test2/_segments
curl -X GET "localhost:9200/test1,test2/_segments?pretty"
多个索引段信息
GET /_segments
curl -X GET "localhost:9200/_segments?pretty"
单索引:返回有关正在进行和已完成的分片恢复的信息。
GET /twitter/_recovery
curl -X GET "localhost:9200/twitter/_recovery?pretty"
获取多个索引的恢复信息
GET index1,index2/_recovery?human
curl -X GET "localhost:9200/index1,index2/_recovery?human&pretty"
获取所有索引的段信息
GET /_recovery?human
curl -X GET "localhost:9200/_recovery?human&pretty"
获取详细的恢复信息
GET _recovery?human&detailed=true
curl -X GET "localhost:9200/_recovery?human&detailed=true&pretty"
清除单个索引的缓存
POST /twitter/_cache/clear
curl -X POST "localhost:9200/twitter/_cache/clear?pretty"
清除多个索引的缓存
POST /kimchy,elasticsearch/_cache/clear
curl -X POST "localhost:9200/kimchy,elasticsearch/_cache/clear?pretty"
清除全部索引的缓存
POST /_cache/clear
curl -X POST "localhost:9200/_cache/clear?pretty"
只清除字段缓存
POST /twitter/_cache/clear?fielddata=true
curl -X POST "localhost:9200/twitter/_cache/clear?fielddata=true&pretty"
只清除查询缓存
POST /twitter/_cache/clear?query=true
curl -X POST "localhost:9200/twitter/_cache/clear?query=true&pretty"
只清除请求缓存
POST /twitter/_cache/clear?request=true
curl -X POST "localhost:9200/twitter/_cache/clear?request=true&pretty"
清除特定字段的缓存
POST /twitter/_cache/clear?fields=foo,bar
curl -X POST "localhost:9200/twitter/_cache/clear?fields=foo,bar&pretty"
刷新单个索引
POST /my-index-000001/_refresh
curl -X POST "localhost:9200/my-index-000001/_refresh?pretty"
刷新多个索引
POST /my-index-000001,my-index-000002/_refresh
curl -X POST "localhost:9200/my-index-000001,my-index-000002/_refresh?pretty"
刷新全部索引
POST /_refresh
curl -X POST "localhost:9200/_refresh?pretty"
刷新单个索引
POST /my-index-000001/_flush
curl -X POST "localhost:9200/my-index-000001/_flush?pretty"
刷新多个索引
POST /my-index-000001,my-index-000002/_flush
curl -X POST "localhost:9200/my-index-000001,my-index-000002/_flush?pretty"
刷新全部索引
POST /_flush
curl -X POST "localhost:9200/_flush?pretty"
刷新单个索引
POST /kimchy/_flush/synced
curl -X POST "localhost:9200/kimchy/_flush/synced?pretty"
刷新多个索引
POST /kimchy,elasticsearch/_flush/synced
curl -X POST "localhost:9200/kimchy,elasticsearch/_flush/synced?pretty"
刷新全部索引
POST /_flush/synced
curl -X POST "localhost:9200/_flush/synced?pretty"
强制合并特定索引
POST /twitter/_forcemerge
curl -X POST "localhost:9200/twitter/_forcemerge?pretty"
强制合并多个索引
POST /kimchy,elasticsearch/_forcemerge
curl -X POST "localhost:9200/kimchy,elasticsearch/_forcemerge?pretty"
强制合并所有索引
POST /_forcemerge
curl -X POST "localhost:9200/_forcemerge?pretty"
基于时间的索引
强制合并对于基于时间的索引很有用,尤其是在使用翻转时。在这些情况下,每个索引仅在特定时间段内接收索引流量。一旦索引不再收到写入,它的分片就可以被强制合并到一个段中。
POST /logs-000001/_forcemerge?max_num_segments=1
curl -X POST "localhost:9200/logs-000001/_forcemerge?max_num_segments=1&pretty"
shard(分片数)= ES节点数 / (副本数+1)
shard.size(GB) < elasticsearch.jvm.size(GB) shard.number = index.size(GB) / elasticsearch.jvm.size(GB) + 1
/etc/rc.d/init.d/cluster001-SERVICE-ELASTICSEARCH-retro-1-NODE start
su - elasticsearch -c '/usr/lib/cluster001/SERVICE-ELASTICSEARCH-retro-1/bin/elasticsearch -d -p /var/run/cluster001/SERVICE-ELASTICSEARCH-retro-1/elasticsearch.pid'
PUT /_cluster/settings { "transient": { "cluster.routing.allocation.cluster_concurrent_rebalance":15, "cluster.routing.allocation.node_concurrent_recoveries":5 } } curl -X PUT "localhost:39200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d' { "transient": { "cluster.routing.allocation.cluster_concurrent_rebalance":68, "cluster.routing.allocation.node_concurrent_recoveries":2 } } '
Elasticsearch可以动态设置某些属性,并且可以通过API来进行设置,包括transient和persistent两种方式,
PUT /_cluster/settings { "persistent" : { "discovery.zen.minimum_master_nodes" : 2 //这个永久设置会在全集群重启时存活下来。 }, "transient" : { "indices.store.throttle.max_bytes_per_sec" : "50mb" // 这个临时设置会在第一次全集群重启后被移除。 } }
http://41.1.31.21:39200/_cat/master
curl -XPUT 'localhost:39200/_settings' -H "Content-Type: application/json" -d '{ "index.max_result_window" :"10000"}'
curl 'localhost:39200/_cat/nodes?v&h=http,version,jdk,disk.total,disk.used,disk.avail,disk.used_percent,heap.current,heap.percent,heap.max,ram.current,ram.percent,ram.max,master'
curl -XPUT -H "Content-Type: application/json" http://localhost:39200/_all/_settings -d '{"index.blocks.read_only_allow_delete": false}'
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:39200/_all/_settings -d '{"number_of_replicas": "0"}'
curl -X DELETE 'http://21.26.96.204:39200/_template/template_copy'
curl -X PUT 'http://localhost:9200/_cluster/settings' -H "Content-Type: application/json" -d '{ "persistent": { "cluster.max_shards_per_node": "5000" } }'
PUT /_cluster/settings { "transient": { "cluster.routing.allocation.cluster_concurrent_rebalance":15, "cluster.routing.allocation.node_concurrent_recoveries":5 } } curl -X PUT "localhost:39200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d' { "transient": { "cluster.routing.allocation.cluster_concurrent_rebalance":68, "cluster.routing.allocation.node_concurrent_recoveries":2 } } '
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:39200/_cluster/settings -d '{ "transient" : { "cluster.routing.allocation.enable" : "none" } }'
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:39200/_cluster/settings -d '{ "transient" : { "cluster.routing.allocation.enable" : null } }'
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:39200/_cluster/settings -d '{ "transient" : { "cluster.routing.rebalance.enable" : "none" } }'
curl -s 'http://localhost:39200/_cat/shards'|awk '$4 != "STARTED" {print $0}'
curl -XPOST 'http://localhost:9200/_cluster/reroute?retry_failed=true'
curl -XPOST 'http://localhost:9200/_cache/clear?fielddata=true'
PUT /_all/_settings { "number_of_replicas": 1 } #节点退役(除了_ip 之外, 还可以用_name、_host进行匹配) PUT /_cluster/settings { "transient": { "cluster.routing.allocation.exclude._ip":"33.95.253.143" } }
curl -X POST "localhost:9200/_cluster/reroute?pretty" -H 'Content-Type: application/json' -d' { "commands": [ { "move": { "index": "test", "shard": 0, "from_node": "node1", "to_node": "node2" } }, { "allocate_replica": { "index": "test", "shard": 1, "node": "node3" } } ] } '
POST /vehicle_pic_index_20210517-20210523/_forcemerge?max_num_segments=1
indices.requests.cache.size: 2% indices.queries.cache.size: 2% indices.fielddata.cache.size: 40%
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:39200/zs0612/_settings -d '{ "refresh_interval": "-1", "number_of_replicas": "0", "index.translog.durability": "async", "index.translog.sync_interval": "60s" }'
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:39200/zs0612/_settings -d '{ "number_of_replicas": "1", "refresh_interval": null, "index.translog.durability": null, "index.translog.sync_interval": null }'
获取插件命令使用说明
sudo bin /elasticsearch - plugin -h
指定URL直接从自定义位置下载插件
sudo bin /elasticsearch - plugin install [url]
UNIX环境:插件位置指定
sudo bin /elasticsearch - plugin install file: ///path/to/plugin.zip
windows环境:插件位置指定
bin \elasticsearch - plugin install file: ///C:/path/to/plugin.zip
HTTP:插件位置指定
sudo bin /elasticsearch - plugin install http: //path/to/plugin.zip
sudo bin /elasticsearch - plugin install [plugin_name]
sudo bin /elasticsearch - plugin list
sudo bin /elasticsearch - remove [plugin_name]
插件是为特定版本的elasticsearch构建的,因此每次更新都必须重新安装插件
sudo bin /elasticsearch - remove [plugin_name] sudo bin /elasticsearch - plugin install [plugin_name]