官网文档

1.查看索引解释

curl -X GET "localhost:9200/_cluster/allocation/explain?pretty" -H 'Content-Type: application/json' -d'
{
  "index": "myindex",
  "shard": 0,
  "primary": true
}
'

返回结果有对应的 explanation 查看对应的原因。

查询某个索引内容

curl -X GET "localhost:9200/indexName/_search -H 'Content-Type: application/json' -d'
{
	"from": 0,
	"size": 200
}
'

2.查看 _cat

查看集群
curl -X GET "localhost:9200/_cat/master?v&pretty"
curl -X GET "localhost:9200/_cat/master?help"
查看帮助
curl -X GET "localhost:9200/_cat/master?help&pretty"
curl -X GET "localhost:9200/_cat/indices?bytes=b&s=store.size:desc&v&pretty"

提供有关为每个数据节点分配了多少分片以及它们正在使用多少磁盘空间的快照

get _cat/allocation?v
get _cat/allocation?v&s=ip
curl -X GET “lalhost:9200/_cat/allocation?v&pretty”
查看索引
过滤基于时间的索引:
curl -s localhost:9200/_cat/indices?h=i | grep 2018.11.11 > /tmp/111
确认下索引:
cat /tmp/111
批量关闭索引:
for i in `cat /tmp/111`;do curl -XPOST localhost:9200/$i/_close;done
查看索引状态:
命令行:
curl -XGET ‘localhost:9200/_cat/indices?v&pretty’
浏览器:
http://localhost:9200/_cat/indices?v&pretty
查看分片信息:
查看未分配的分片
curl -XGET localhost:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason|grep UNASSIGNED
查看segment:

查看官网文档API

ES 的 data node 存储数据并非只是耗费磁盘空间的,为了加速数据的访问,每个 segment 都有会一些索引数据驻留在 heap 里。因此 segment 越多,瓜分掉的 heap 也越多,并且这部分 heap 是无法被 GC 掉的!

  • 删除不用的索引
  • 关闭索引 (文件仍然存在于磁盘,只是释放掉内存)。需要的时候可以重新打开。
  • force merge api,可以节省大量的 segment memory。
命令行:
curl -XGET ‘localhost:9200/_cat/segments?v’
浏览器查看:
http://localhost:9200/_cat/segments?v&h=shard,segment,size,size.memory&s=size:desc
查看一个node上所有segment占用的memory总和
http://localhost:9200/_cat/nodes?v&ip,heap.percent,name,sm
强制合并:多个索引可以逗号
curl -X POST "localhost:9200/.ds-logs-000001/_forcemerge?max_num_segments=1&pretty"
控制合并资源


查看分片信息
curl -XGET http://localhost:9200/_cat/shards?v
查看索引信息
curl -XGET http://localhost:9200/_cat/indices?v
查看集群恢复信息
curl http://10.155.208.146:9200/_cat/recovery |grep north_rbet-zb-infolog2_2020w50
查看es插入完成拒绝数目
http://192.168.13.25:9200/_cat/thread_pool/write?v&h=node_name,name,active,rejected,completed

3.关闭集群路由

PUT _cluster/settings
{
  "transient": {
    "cluster.routing.rebalance.enable":"none"
  }
}

cluster.routing.rebalance.enable 为特定种类的分片启用或禁用重新平衡:

all -(默认)允许所有类型的分片进行分片平衡。

primaries -仅允许对主要分片进行分片平衡。

replicas -仅允许副本分片进行分片平衡。

none -任何索引都不允许任何形式的分片平衡。

4. 复制旧索引到新集群

将以下配置参数添加到新的 7.x Elasticsearch 集群的 elasticsearch.yml 文件,以支持其访问旧的 Elasticsearch 集群:

reindex.remote.whitelist: oldhost:*

oldhost 替换为 Elasticsearch 主机。

不要指定 Elasticsearch 端口,但将值设为 *,否则可能收到以下错误消息:"[oldhost:9200] not whitelisted in reindex.remote.whitelist"

重新启动 Elasticsearch 集群。

curl -XPOST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d '{
    "source": {
        "remote": {
            "host": "http://192.168.13.21:9200",
            "username": "username",
            "password": "pwd"
        },
        "index": "old_base_line_202004",
        "query": {
            "match_all": {}
        }
    },
    "dest": {
        "index": "new_base_line_202004"
    }
}
'

可以配置 elasticsearch.yml 或者动态使用 Cluster Settings API 配置

  • 使用配置的形式

    search:
        remote:
            south:
                seeds: 127.0.0.1:9301
                seeds: 127.0.0.1:9302
            north:
                seeds: 127.0.0.1:9303
                seeds: 127.0.0.1:9304
    
  • 使用命令设置

    curl -XPUT -H'Content-Type: application/json' localhost:9201/_cluster/settings -d '
    {
        "persistent": {
            "search.remote": {
                "south": {
                    "skip_unavailable": "true",
                    "seeds": ["192.168.13.25:9300"]
                },
                "north": {
                    "skip_unavailable": "true",
                    "seeds": ["192.168.13.23:9300","192.168.13.100:9300"]
                }
            }
        }
    }'
    
查看文件大小
curl http://localhost:9200/_cluster/settings
{
    "persistent": {
        "search": {
            "remote": {
                "south": {
                    "skip_unavailable": "true",
                    "seeds": [
                        "192.168.13.25:9300"
                    ]
                },
                "north": {
                    "skip_unavailable": "true",
                    "seeds": [
                        "192.168.13.23:9300",
                        "192.168.13.100:9300"
                    ]
                }
            }
        }
    },
    "transient": {}
}

然后搜索可以直接使用索引拼接集群前缀的方式进行请求数据,如south:xxxIndex,搜索数据可以,但是其他的一些 api 初始化客户端的时候因为往往只有一边的 es 集群,比如获取字段,获取其他数据只能有一半的数据,解决方案,在具体的获取客户端的时候获取远程 remote 的信息,再回头初始化多个客户端,对不同的集群索引进行分组使用对应的客户端进行请求,得到结果在合并到一起。

6.查看 task 任务

http://188.4.15.119:9200/_tasks

http://188.4.15.119:9200/_cat/tasks

取消任务

POST _tasks/YhYJAf--Q0Wt7H-OTpLwBw:5895289684/_cancel
POST _tasks/_cancel?nodes=node-320&actions=*search
POST _tasks/_cancel?actions=*search*

7.es7 版本设置每个节点最大分片数

elasticsearch7以上版本,默认只允许1000个分片,问题是因为集群分片数不足引起的,永久(Persistent),临时(Transient)

curl -XPUT -H "Content-Type:application/json" http://localhost:9200/_cluster/settings -d '{"persistent":{"cluster":{"max_shards_per_node":2000}}}'
上次更新时间: 2024/5/7 05:59:02