ElasticSearch学习笔记-常用操作记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wulinshishen/article/details/79298167
集群相关配置

查看相关信息
curl -XGET 'http://192.168.0.1:9200/_cat/health?v'
curl -XGET 'http://192.168.0.1:9200/_cat/nodes?v'
curl -XGET 'http://192.168.0.1:9200/_cat/allocation?v'
curl -XGET 'http://192.168.0.1:9200/_cat/plugins?v'
curl -XGET 'http://192.168.0.1:9200/_cat/count?v'
curl -XGET 'http://192.168.0.1:9200/_cat/count/index?v'
curl -XGET 'http://192.168.0.1:9200/_cat/indices?v'
curl -XGET 'http://192.168.0.1:9200/_cat/indices/{index}?v'
curl -XGET 'http://192.168.0.1:9200/_cat/shards?v'
curl -XGET 'http://192.168.0.1:9200/_cat/shards/{index}?v'
curl -XGET 'http://192.168.0.1:9200/_cat/segments?v'
curl -XGET 'http://192.168.0.1:9200/_cat/segments/{index}?v'
curl -XGET 'http://192.168.0.1:9200/_cat/aliases?v'
curl -XGET 'http://192.168.0.1:9200/_cat/aliases/{alias}?v'
curl -XGET 'http://192.168.0.1:9200/_cat/thread_pool?v'
curl -XGET 'http://192.168.0.1:9200/_cat/recovery?v'

查看集群健康状态
curl -XGET 'http://192.168.0.1:9200/_cluster/health?pretty=true'

查看集群状态
curl -XGET 'http://192.168.0.1:9200/_cluster/stats?pretty=true'

查看集群配置
curl -XGET 'http://192.168.0.1:9200/_cluster/settings?pretty=true'

修改集群配置
persistent:持久配置 transient:临时配置

线程池相关配置
generic:通用操作,如node discovery。它的类型默认为cached。
index:此线程池用于索引和删除操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为200。
search:此线程池用于搜索和计数请求。它的类型默认为fixed,size默认为(可用处理器的数量* 3) / 2) + 1,队列的size默认为1000。
suggest:此线程池用于建议器请求。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为1000。
get:此线程池用于实时的GET请求。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为1000。
bulk:此线程池用于批量操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为50。
percolate:此线程池用于预匹配器操作。它的类型默认为fixed,size默认为可用处理器的数量,队列的size默认为1000。

curl -XPUT 'http://192.168.0.1:9200/_cluster/settings' -d '{"persistent":{"threadpool.bulk.type": "fixed", "threadpool.bulk.size" : 12, "threadpool.bulk.queue_size" : 1200}}'

curl -XPUT 'http://192.168.0.1:9200/_cluster/settings' -d '{"transient":{"threadpool.bulk.type": "fixed", "threadpool.bulk.size" : 12, "threadpool.bulk.queue_size" : 1200}}'

磁盘相关常用配置
"cluster.routing.allocation.disk.threshold_enabled" : false

cluster.routing.allocation.disk.watermark.low:控制磁盘使用的低水位。默认为85%,意味着如果节点磁盘使用超过85%,则ES不允许在分配新的分片。

cluster.routing.allocation.disk.watermark.high:控制磁盘使用的高水位。默认为90%,意味着如果磁盘空间使用高于90%时,ES将尝试分配分片到其他节点。

上述两个配置可以使用API动态更新,ES每隔30s获取一次磁盘的使用信息,该值可以通过cluster.info.update.interval来设置。

curl -XPUT 'http://192.168.0.1:9200/_cluster/settings' -d' {"persistent" : {"cluster.routing.allocation.disk.watermark.low" : "90%", "cluster.routing.allocation.disk.watermark.high" : "95%"} }'

分片分配相关配置

设置根据集群中机器的状态来重新分配分片,可以设置为可以设置为always, indices_primaries_active和indices_all_active,默认是设置成indices_all_active来减少集群初始启动时机器之间的交互。
curl -XPUT 'http://192.168.0.1:9200/_cluster/settings' -d '{"transient" : {"cluster.routing.allocation.allow_rebalance" : "indices_all_active"}}'

设置在集群中最大允许同时进行分片分布的个数,默认为2,也就是说整个集群最多有两个分片在进行重新分布。
curl -XPUT 'http://192.168.0.1:9200/_cluster/settings' -d '{"transient" : {"cluster.routing.allocation.cluster_concurrent_rebalance" : "2"}}'

设置在节点中最大允许同时进行分片分布的个数,默认为2
curl -XPUT 'http://192.168.0.1:9200/_cluster/settings' -d '{"transient" : {"cluster.routing.allocation.node_concurrent_recoveries" : "2"}}'
 
指定哪些分片可以参与重新分配。选项有:all(default), primaries(主分片), new_primaries(新增加的主分片), none(禁止自动分配)。
curl -XPUT 'http://192.168.0.1:9200/_cluster/settings' -d '{"transient" : {"cluster.routing.allocation.enable" : "none"}}'

控制延迟多长时间后才开始分配unassigned的分片
curl -XPUT 'http://192.168.0.1:9200/_cluster/settings' -d '{"transient":{"index.unassigned.node_left.delayed_timeout": "5m"}}'

扩容的时候,可以先行设置cluster.routing.allocation.enable=primaries,指只允许移动主shard。当发现shard数已经迁移了一半的时候,改回配置cluster.routing.allocation.enable=all,后面的迁移的是副本shard。这样扩容之后,shard和主shard的分布还是基本均匀的。

重新分配分片
curl -XPOST ‘http://192.168.0.1:9200/_cluster/reroute’ -d ‘{
“commands” : [{“move” : {
“index” : “syslog-v1”, “shard” : 1,
“from_node” : “192.168.0.1”, “to_node” : “192.168.0.2”
}}]}’

curl -XPOST '192.168.0.1:9200/_cluster/reroute' -d '{
"commands" : [ {"allocate" : {
"index" : "syslog-v1", "shard" : 1, "node" : "node-2", "allow_primary" : true
}}]}'


Index索引相关配置

修改副本数量配置
curl -XPUT 'http://192.168.0.1:9200/syslog-v1/_settings' -d '{"index":{"number_of_replicas":"1"}}'

修改数据同步刷新间隔配置
curl -XPUT 'http://192.168.0.1:9200/syslog-v1/_settings' -d '{"index":{"refresh_interval":"60s"}}'

修改索引的缓冲区大小,其默认值为10%,这意味着分配给一个节点的总存储器的10%将被用作索引的缓冲区大小。如果百分比被使用时,也能够设定min_index_buffer_size和max_index_buffer_size
curl -XPUT 'http://192.168.0.1:9200/syslog-v1/settings' -d' {"index" : {"indices.memory.index_buffer_size" : "30%"} }'

修改translog相关配置
curl -XPUT 'http://192.168.0.1:9200/syslog-v1/settings' -d' {"index" : {"index.translog.flush_threshold_ops" : "50000","index.translog.flush_threshold_size":"1gb"} }'

Elastic5 : index.translog.flush_threshold_size

curl -XPOST '192.168.0.1:9200/syslog-v1/_close'
curl -XPUT 'http://192.168.0.1:9200/syslog-v1/_settings' -d '{"index":{
"refresh_interval":"-1",
"indices.memory.index_buffer_size" : "30%",
"index.translog.flush_threshold_ops":"50000",
"index.translog.flush_threshold_size":"1gb"
}}'
curl -XPOST '192.168.0.1:9200/syslog-v1/_open'

别名相关配置
curl -XPUT "http://192.168.0.1:9200/syslog-v1/_alias/syslog" 

curl -XPOST "http://192.168.0.1:9200/_aliases" -d '  {  
  "actions": [  
    {"remove": {"index": "syslog-v2",  "alias": "syslog"}},  
    {"add": {"index": "syslog-v1",  "alias": "syslog"}}  
  ]}'  

Index索引优化相关配置

清除已删除的文件
curl -XPOST 'http://192.168.0.1:9200/syslog-v1/_optimize?only_expunge_deletes=true&wait_for_completion=true'  

删除了的文档数在一个segment里占的百分比,默认是10,大于这个值时,在执行expungeDeletes 操作时将会merge这些segments.
index.merge.policy.expunge_deletes_allowed

显示调用optimize 操作或者 expungeDeletes时可以操作多少个segments,默认是30.
index.merge.policy.max_merge_at_once_explicit

修改segment最大数配置
curl -XPOST 'http://192.168.0.1:9200/syslog-v1/_optimize?max_num_segments=1'

查看一个索引segment的memory占用情况
curl -XGET "http://192.168.0.1:9200/_cat/segments/syslog-v1?v&h=shard,segment,size,size.memory"

查看Node的memory占用情况
curl -XGET "http://192.168.0.1:9200/_cat/nodes?v&h=name,port,sm"

索引分词相关记录
curl -XGET 'http://192.168.0.1:9200/syslog-v1/_analyze?analyzer=ik&pretty=true' -d '{"text":"太极宗师张三丰"}'

curl -XGET 'http://192.168.0.1:9200/syslog-v1/_analyze?analyzer=ik_smart&pretty=true' -d '{"text":"太极宗师张三丰"}'

curl -XGET 'http://192.168.0.1:9200/syslog-v1/_analyze?analyzer=ik_max_word&pretty=true' -d '{"text":"太极宗师张三丰"}'

http://192.168.0.1:9200/syslog-v1/_analyze?analyzer=ik&text=太极宗师张三丰&pretty=true

Mapping相关配置
新建mapping
curl -XPOST 'http://192.168.0.1:9200/syslog-v1/syslog/_mapping/' -d '{
            "dynamic": "strict",
            "properties": {
                "username": {
                    "analyzer": "ik_max_word",
                    "term_vector": "with_positions_offsets",
                    "type": "string"
                }
             }
}'

curl -XPOST 'http://192.168.0.1:9200/syslog-v1/syslog/_mapping' --data-binary @syslog.mapping

读取mapping

curl -XGET 'http://192.168.0.1:9200/syslog-v1/_mappings?pretty=true'



猜你喜欢

转载自blog.csdn.net/wulinshishen/article/details/79298167