查看集群状态信息

  1. curl http://10.110.106.173:9200/_cluster/health?pretty

当集群状态为yellow,unassigned_shards的数值几百上千时,此时为了加快索引shards重新分配的速度,可以设置一下参数值,默认为2

  1. curl -XPUT 10.117.106.11:9200/_cluster/settings -d '
  2. {
  3. "transient":{"cluster.routing.allocation.node_concurrent_recoveries": 8}
  4. }'

重建索引,reindex

  1. curl -XPOST 10.110.106.87:9200/_reindex -d '
  2. {
  3. "conflicts": "proceed",
  4. "source": {
  5. "remote": {"host": "http://10.116.106.139:9200"},
  6. "index": "uss_operation_waybill~201806","size":"10000"},
  7. "dest": {
  8. "index": "uss_operation_waybill~201806",
  9. "version_type": "external"}
  10. }'

添加别名

  1. curl -XPOST 'http://localhost:9200/_aliases' -d '
  2. { "actions": [ {"add": {"index": "test1", "alias": "alias1"}} ] }'

删除别名,可提高查询速度

  1. curl -XPOST http://10.116.106.139:9200/_aliases -d '
  2. {"actions":[
  3. { "remove":{"index":"uss_operation_waybill~201804","alias":"uss_operation_waybill" }},
  4. { "remove":{"index":"uss_operation_waybill~201805","alias":"uss_operation_waybill" }},
  5. { "remove":{"index":"uss_operation_waybill~201806","alias":"uss_operation_waybill" }}
  6. ]}'

增加字段
openapi_profile~2020:索引名,openapi_profile:type名,cust_name_text:新增的字段名,type:新增字段的类型

  1. curl -XPUT 'http://10.110.106.208:9200/openapi_profile~2020/_mapping/openapi_profile?pretty' -H 'Content-Type: application/json' -d '
  2. {
  3. "properties": {
  4. "cust_name_text": {
  5. "type": "text",
  6. "analyzer": "ngram_analyzer"
  7. }
  8. }
  9. }'

查看分片重新均衡

  1. curl 192.168.56.10:9200/_cat/shards | fgrep RELO

查看集群磁盘IO情况

  1. iostat -x 2 10
  2. mount 查看磁盘

强制merge,可提高查询速度

  1. curl -XPOST 10.116.106.139:9200/uss_operation_waybill~201904/_forcemerge?max_num_segments=1
  2. /index/_forcemerge?only_expunge_deletes=true
  1. 官方说明: The force merge API allows to force merging of one or more indices through an API. The merge relates to the number of segments a Lucene index holds within each shard. The force merge operation allows to reduce the number of segments by merging them.
  2. This call will block until the merge is complete. If the http connection is lost, the request will continue in the background, and any new requests will block until the previous force merge is complete.

注意:在业务使用高峰期间避免执行强制merge,会导致对集群的读写性能下降,且merge的整个过程会比较久。
使用场景:

  1. segments的数量较多时。一个ES索引由若干个分片组成,一个分片有若干个Lucene分段,较大的Lucene分段可以更有效的存储数据。
  2. 使用query_by_delete删除数据时,此时数据只是被标记为deleted,并未真正的物理删除,在search的时候依旧会去遍历该部分数据,使用merge为提升查询效率

查询未分配分片,场景:当集群监控度低于100%,

  1. curl 10.110.106.22:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason| grep UNASSIGN

查看具体未分配分片的原因

  1. cutl -s http://masterip:9200/_cluster/allocation/explain?pretty |less

查看具体节点

  1. curl 10.110.106.22:9200/_nodes/节点名

修改是否重新分配,none为取消重新分配,all为开启重新分配,场景:节点维护时间短

  1. curl -XPUT "http://10.116.106.35:9200/_cluster/settings" -d'
  2. {
  3. "transient" : {
  4. "cluster.routing.allocation.enable" : "none"
  5. }
  6. }'
  7. curl -XPUT "http://10.116.106.35:9200/_cluster/settings" -d'
  8. {
  9. "transient" : {
  10. "cluster.routing.allocation.enable" : "all"
  11. }
  12. }'

设置重平衡

  1. curl -XPUT "http://10.116.106.35:9200/PUT _cluster/settings" -d
  2. {
  3. "transient": {
  4. "cluster.routing.rebalance.enable":"none"
  5. }
  6. }

节点间迁移分片 fromnode tonode

  1. curl -XPOST 10.116.106.147:9200/_cluster/reroute -d '
  2. {"commands":[
  3. {"move":{
  4. "index":"dms_ebil_discount_waybilldto~2018-11",
  5. "shard":1,
  6. "from_node" : "10.116.106.147:9303",
  7. "to_node" : "10.116.106.152:9301"
  8. }}]}'

动态添加慢日志,场景:查询时间超过2s的查询信息会输出在日志上,常用于问题诊断

  1. curl -XPUT 10.116.106.205:9200/waybillinfo_prophet_monitor/_settings -d' {"index.search.slowlog.threshold.query.warn":"2s",
  2. "index.search.slowlog.threshold.fetch.warn":"2s"
  3. }'


#动态 给集群所有索引添加慢日志(ES7.6)

  1. curl -XPUT -u elastic:bdp@elastic -H "Content-Type:application/json" 10.116.107.36:9200/ all/settings -d '{
  2. "index.search.slowlog.threshold.query.warn":"5s",
  3. "index.search.slowlog.threshold.query.info":"2s"
  4. }'

查看节点nodeId

  1. curl 10.116.106.35:9200/_cluster/state/nodes

查看节点状态

  1. curl 10.116.106.35:9200/_nodes/节点名/stats

去除IP节点,场景:主机硬件维护

  1. curl -X PUT "http://10.116.106.35:9200/_cluster/settings" -d '
  2. {"transient":{"cluster.routing.allocation.exclude.ip":"10.116.106.47" }
  3. }'

去除name节点,场景:节点异常

  1. curl -X PUT "http://10.116.106.35:9200/_cluster/settings" -d '
  2. {"transient":{"cluster.routing.allocation.exclude.name":"10.116.106.47:9300" }
  3. }'

设置副本,可提高reindex和写入速度

  1. curl -XPUT 10.116.106.35:9200/mcs_cas_ali_routequery~2019-05-10/_settings -d '
  2. {"index" : { "number_of_replicas" : 0}}'

查看状态内存

  1. curl 10.202.116.33:9200/_cluster/stats

修改副本数

  1. curl -XPUT 10.117.106.9:9200/risp_waybills_v1/_settings -d
  2. '{"index":{"number_of_replicas":0}}'

修改索引刷新间隔时间settings,可提高写入速度

  1. curl -XPUT 10.117.106.9:9200/twitter/_settings -d'
  2. {
  3. "index" : {
  4. "refresh_interval" : "30s"
  5. }
  6. }'

条件删除数据,生产环境删数据、删索引需谨慎,必须邮件跟业务确认,同时知会组内相关同事

  1. curl 100.65.101.21:9200/risp_waybills_v1_3/_delete_by_query -d '{"query":{"match_all":{}}}'

设置更新间隔 不更新为-1

  1. curl -XPUT 10.116.106.188:9200/tradition_*/_settings -d '
  2. {"index" : {"refresh_interval" : "60s"}}'

防止同一个分片(shard)的主副本存在同一个物理机上

  1. cluster.routing.allocation.same_shard.host: true

丢数据的索引重新分配分片

  1. POST /_cluster/reroute -d '
  2. {"commands" : [ {
  3. "allocate_empty_primary" : {
  4. "index" : "awsm_cos_message_to_bdp_es~2019-09",
  5. "shard" : 24,
  6. "node" : "10.216.21.62:9300",
  7. "accept_data_loss":true
  8. }
  9. }
  10. ]
  11. }'

添加mapping

  1. curl -XPUT 10.117.106.17:9200/delayed_waybill_for_sme/_mapping/delayed_waybill_for_sme -d '{"properties": {"codMoney": {"type": "float"}}}'

设置索引每个节点只能保留1个分片

  1. curl -XPUT master:9200/索引名称/_settings?pretty -d '
  2. {
  3. "index": {
  4. "routing": {
  5. "allocation": {
  6. "total_shards_per_node": "1"
  7. }
  8. }
  9. }
  10. }'

官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/allocation-total-shards.html

修改节点最大分片数(ES7.6)

  1. curl -s -XPUT -u elastic:Bdp1@elastic -H "Content-Type:application/json" " http://100.80.72.125:9200/_cluster/settings" -d
  2. '{
  3. "transient": {
  4. "cluster": {
  5. "max_shards_per_node":15000
  6. }
  7. }
  8. }'

取消task任务

  1. POST tasks/node_id:task_id/cancel