0.介绍
工作中常常会使用到Elasticsearch(以下简称ES),有时利用REST API管理ES会比Kibana更加方便。但是,ES的REST API多如牛毛,当想要查询某个信息时,需要访问ES官网并在大量的API文档中苦苦寻觅,效率极低。因此,在这里记录平日里使用频率较高的API,以供快速查找。
该笔记的目标是能够方便地对请求进行粘贴复制,文中所有API都可以在ES官网上找到。下列所有命令中,-u elastic是ES默认管理员账号,在实际使用的过程中请各位读者自行调整。
1. 获取集群健康状态
curl -v -XGET 'http://localhost:9200/_cluster/health?filter_path=status,*_shards' -u elastic
2.获取集群分片信息
curl -v -XGET 'http://localhost:9200/_cat/shards' -u elastic
3.获取Elasticsearch集群数据总条数
curl -v -XGET 'http://localhost:9200/page_access/_count' -u elastic
4.获取索引
curl -v -XGET 'http://localhost:9200/indexname' -u elastic
5.获取集群设置
curl -v -XGET 'http://localhost:9200/_cluster/settings?include_defaults&flat_settings' -u elastic
6.创建索引,并指定分片、副本分片信息
curl -v -XPUT http://localhost:9200/test-index-new/_settings -H "Content-Type: application/json" -d '
{
"index":
{
"number_of_shards": "1",
"number_of_replicas": "0"
}
}' -u elastic
7.设置集群分片分配
curl -v -XPOST 'http://localhost:9300/_cluster/reroute' -H 'Content-Type:application/json' -d '
{
"transient":
{
"cluster.routing.alloction.enable": "all"
}
}' -u elastic
8.强制分配索引主分片
curl -XPOST 'http://localhost:9200/_cluster/reroute' -H 'Content-Type:application/json' -d '
{
"commands":
[
{
"allocate":
{
"index": "index_name",
"shard": "0",
"node": "<node-1>",
"allow_primary": "true"
}
}
]
}' -u elastic
9.创建快照仓库
curl -v -XPUT 'http://localhost:9200/_snapshot/reponame' -d '{
"type": fs,
"settings": {
"location": "/path/to/snapshot/repo"
}
}' -u elastic
10.创建快照
curl -v -XPUT 'http://localhost:9300/_snapshot/ming/snapshot_name?wait_for_completion=true' -d '
{
"indices": "indexname"
}' -u elastic
执行第9、10条的命令是ES配置中配置path.repo选项,并且第9条中的location需要与配置文件中的路径一直。如图1
图1
11.清除索引缓存
curl -v -XPUT 'http://locahost:9300/index/_cache/clear' -u elastic
12.查询索引生命周期
curl -v -XGET 'http://localhost:9200/index-name/_ilm/explain' -u elastic
13.查询生命周期策略的详细信息
curl -v -XGET 'http://locahost:9200/_ilm/policy/my_policy'-u elastic
14.查看集群节点最大文件描述符限制
curl -v -XGET 'http://localhost:9200/_nodes/stats/process?filter_path=**.max_file_descriptors&pretty' -u elastic
15.查看索引的打开状态
curl -v -XGET "http://localhost:9200/_cat/indices/index-name?h=status" -u elastic
16.关闭索引
curl -v -XGET "http://localhost:9200/index_name/_close" -u elastic
17.查看索引refresh时间
curl -v -XGET "http://localhost:9200/refresh" -u elastic
18.设启动、停止、重启transform
force选项强制执行命令。可根据需要调整。
#启动transform
curl -v -XPOST 'http://localhost:9200/_transform/transform-id/_start?force' -u elastic
#停止transform
curl -v -XPOST 'http://localhost:9200/_transform/transform-id/_stop?force' -u elastic
#重置transform
curl -v -XPOST 'http://localhost:9200/_transform/transform-id/_reset?force' -u elastic
19.查看transform状态
#获取一个transform的状态
curl -v -XGET 'http://localhost:9200/_transform/transform_id/_stats' -u elastic
#获取多个transform的状态
curl -v -XGET 'http://localhost:9200/_transform/transform_id_1,transform_id_2/_stats' -u elastic
#获取所有transform的状态
curl -v -XGET 'http://localhost:9200/_transform/_all/_stats' -u elastic