【笔记】Elasticsearch REST API 使用参考手册

本文档汇总了Elasticsearch中常用的RESTAPI命令,包括集群健康检查、分片管理、索引操作、设置、快照创建、缓存清理等,以提高查询效率。
摘要由CSDN通过智能技术生成

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
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值