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

集群相关配置

查看相关信息

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'

 

ElasticSearch 7.3.0 

设置Setting  

curl -XPOST 'http://192.168.0.1:9200/book/_close'

curl -H "Content-Type: application/json" -XPUT 'http://192.168.0.1:9200/book/_settings' -d '
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "pinyin_analyzer" : {
                    "tokenizer" : "pinyin_tokenizer"
                }
            },
            "tokenizer" : {
                "pinyin_tokenizer" : {
                    "type" : "pinyin",
                    "keep_separate_first_letter" : false,
                    "keep_full_pinyin" : true,
                    "keep_original" : true,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "remove_duplicated_term" : true
                }
            }
        }
    }
}
'

curl -XPOST 'http://192.168.0.1:9200/book/_open'

设置Mapping

curl -H "Content-Type: application/json" -XPUT 'http://192.168.0.1:9200/book/_mapping' -d '
{
    "dynamic": "strict",
    "_source": {
        "excludes": ["id"]
    },
    "properties": {
        "id": {
            "type": "keyword"
        },
        "author": {
            "analyzer": "ik_max_word",
            "type": "text",
            "fields": {
                "raw": {
                      "type": "keyword"
                },
                "pinyin1": {
                    "analyzer": "pinyin",
                      "type": "text"
                },
                "pinyin2": {
                    "analyzer": "pinyin_analyzer",
                      "type": "text"
                },
                "traditional": {
                    "analyzer": "stconvert",
                      "type": "text"
                }
          }
        },
        "title": {
            "analyzer": "ik_smart",
            "type": "text"
        },
        "describe": {
            "analyzer": "ik_smart",
            "type": "text"
        },
        "publish_time": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "type": {
            "type": "keyword"
        }
    }
}
'

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值