elasticsearch提供比较齐全的各种功能点的API,如/_cat,/_xpack等,该文档是基于ElasticSearch8.0版本进行梳理
1、Info API
提供关于x-pack特征信息,负责安全问题使用该功能
http://ip:9200/_xpack
GET /_xpack?categories=build,features 该请求只会返回build和features
GET /_xpack?human=false 该请求会移除human描述
2、CAT API
提供关于elasticsearch运行状态等信息检查,是比较常见的一种接口
http://ip:9200/_cat,提供了很多中检查接口,可以查看很多基本信息
/_cat/allocation 提供分配给每个数据节点的分片数量及其磁盘空间的快照。 <!--分片信息--> /_cat/shards /_cat/shards/{index} /_cat/master 返回有关主节点的信息,包括ID、绑定IP地址和名称 /_cat/nodes 返回节点信息 /_cat/tasks 返回正在执行的任务信息 <!--返回索引信息--> /_cat/indices /_cat/indices/{index} <!--返回索引分片中Lucene段的低级信息,类似于索引段API--> /_cat/segments /_cat/segments/{index} <!--计数--> /_cat/count /_cat/count/{index} <!--返回正在进行或者已完成的碎片恢复信息--> /_cat/recovery /_cat/recovery/{index} <!--返回群集的运行状况状态,类似于群集运行状况--> /_cat/health /_cat/pending_tasks 返回尚未执行的集群级更改,类似于挂起的集群任务API /_cat/aliases 应用程序消耗 /_cat/aliases/{alias} <!--返回每个节点的线程池信息--> /_cat/thread_pool /_cat/thread_pool/{thread_pools} /_cat/plugins 返回运行在每个节点上的插件 <!--返回集群中每个数据节点上的字段数据缓存当前使用的堆内存量--> /_cat/fielddata /_cat/fielddata/{fields} /_cat/nodeattrs 返回节点属性信息 /_cat/repositories 返回快照仓库信息 /_cat/snapshots/{repository} 返回快照信息 /_cat/templates 返回索引模板信息 <!-- 返回有关异常检测作业的配置和信息--> /_cat/ml/anomaly_detectors /_cat/ml/anomaly_detectors/{job_id} <!--返回关于推理训练模型的配置和使用信息--> /_cat/ml/trained_models /_cat/ml/trained_models/{model_id} <!--返回关于数据提要的配置和使用信息--> /_cat/ml/datafeeds /_cat/ml/datafeeds/{datafeed_id} <!--返回有关数据帧分析作业的配置和使用信息--> /_cat/ml/data_frame/analytics /_cat/ml/data_frame/analytics/{id} <!--返回有关转换的配置和使用信息--> /_cat/transforms /_cat/transforms/{transform_id}如
/_cat/master?v=true可以查看主节点的节点信息
/_cat/indices?pretty可以查看索引信息 /_cat/templates?v=true&s=order:desc,index_patterns 实现模板排序
3、Cluster API
介绍集群节点通用API
集群相关
提供分片分配解释信息
GET _cluster/allocation/explain POST _cluster/allocation/explain如:
GET _cluster/allocation/explain { "index": "my-index-000001", "shard": 0, "primary": true }
提供集群范围设置
GET /_cluster/settings
提供集群健康状态
GET /_cluster/health?wait_for_status=yellow&timeout=50s
提供改变集群分片分配
POST /_cluster/reroute { "commands": [ { "move": { "index": "test", "shard": 0, "from_node": "node1", "to_node": "node2" } }, { "allocate_replica": { "index": "test", "shard": 1, "node": "node3" } } ] }
返回群集状态的内部表示形式,用于调试或诊断目的
GET /_cluster/state
返回集群统计数据
如:GET /_cluster/stats?human&pretty 严格限制节点 GET /_cluster/stats/nodes/node1,node*,master:false
配置动态集群设置
PUT /_cluster/settings { "persistent" : { "indices.recovery.max_bytes_per_sec" : "50mb" } }PUT /_cluster/settings { "transient" : { "indices.recovery.*" : null } }
返回尚未执行的集群级更改
GET /_cluster/pending_tasks
返回配置的远程集群信息
GET /_remote/info
返回集群目前执行的任务
GET /_tasks/<task_id>
GET /_tasks
如:
GET _tasks GET _tasks?nodes=nodeId1,nodeId2 GET _tasks?nodes=nodeId1,nodeId2&actions=cluster:*
从投票配置表里删除或者添加符合主节点资格的节点
POST /_cluster/voting_config_exclusions?node_names=<node_names>
POST /_cluster/voting_config_exclusions?node_ids=<node_ids>
DELETE /_cluster/voting_config_exclusions
如:
POST /_cluster/voting_config_exclusions?node_names=nodeName1,nodeName2 DELETE /_cluster/voting_config_exclusions
节点相关
返回节点使用属性信息
GET /_nodes/usage
GET /_nodes/<node_id>/usage
GET /_nodes/usage/<metric>
GET /_nodes/<node_id>/usage/<metric>
返回集群中每个选定节点的热线程
GET /_nodes/hot_threads
GET /_nodes/<node_id>/hot_threads
返回节点信息
GET /_nodes
GET /_nodes/<node_id>
GET /_nodes/<metric>
GET /_nodes/<node_id>/<metric>
如:
# return just process GET /_nodes/process # same as above GET /_nodes/_all/process # return just jvm and process of only nodeId1 and nodeId2 GET /_nodes/nodeId1,nodeId2/jvm,process # same as above GET /_nodes/nodeId1,nodeId2/info/jvm,process # return all the information of only nodeId1 and nodeId2 GET /_nodes/nodeId1,nodeId2/_all # return all the information of plugins GET /_nodes/plugins
返回节点安全存储键等信息
POST /_nodes/reload_secure_settings
POST /_nodes/<node_id>/reload_secure_settings
返回节点统计状态
GET /_nodes/stats
GET /_nodes/<node_id>/stats
GET /_nodes/stats/<metric>
GET /_nodes/<node_id>/stats/<metric>
GET /_nodes/stats/<metric>/<index_metric>
GET /_nodes/<node_id>/stats/<metric>/<index_metric>
如:
# return just indices GET /_nodes/stats/indices # return just os and process GET /_nodes/stats/os,process # return just process for node with IP address 10.0.0.1 GET /_nodes/10.0.0.1/stats/process
4、Cross-cluster 复制API
状态:GET /_ccr/stats
创建复制索引:
PUT /<follower_index>/_ccr/follow?wait_for_active_shards=1 { "remote_cluster" : "<remote_cluster>", "leader_index" : "<leader_index>" }暂停复制索引:
POST /<follower_index>/_ccr/pause_follow恢复复制索引:
POST /<follower_index>/_ccr/resume_follow { }将复制索引变成常规索引
POST /<follower_index>/_ccr/unfollow获取索引状态
GET /<index>/_ccr/stats获取索引信息
GET /<index>/_ccr/info
5、Document API
添加或者更新索引
将JSON文档添加到指定的数据流或索引,并使其可搜索。如果目标是索引并且文档已经存在,则请求更新文档并增加其版本
PUT /<target>/_doc/<_id>
POST /<target>/_doc/
PUT /<target>/_create/<_id>
POST /<target>/_create/<_id>
如:创建文档id自动添加的
POST my-index-000001/_doc/ { "@timestamp": "2099-11-15T13:12:00", "message": "GET /search HTTP/1.1 200 1070000", "user": { "id": "kimchy" } }创建指定id的文档
PUT my-index-000001/_doc/1 { "@timestamp": "2099-11-15T13:12:00", "message": "GET /search HTTP/1.1 200 1070000", "user": { "id": "kimchy" } }PUT my-index-000001/_doc/1?version=2&version_type=external { "user": { "id": "elkbee" } }
获取某个索引的文档信息
GET <index>/_doc/<_id>
HEAD <index>/_doc/<_id>
GET <index>/_source/<_id>
HEAD <index>/_source/<_id>
如:
GET my-index-000001/_doc/0 GET my-index-000001/_source/1 HEAD my-index-000001/_source/1
删除某个索引的文档信息
DELETE /<index>/_doc/<_id>
POST /<target>/_delete_by_query 删除查询到条件的文档记录
如:
DELETE /my-index-000001/_doc/1?routing=shard-1
POST /my-index-000001/_delete_by_query { "query": { "match": { "user.id": "elkbee" } } }
POST /my-index-000001,my-index-000002/_delete_by_query { "query": { "match_all": {} } }
更新文档信息
POST /<index>/_update/<_id>
POST /<target>/_update_by_query 按条件查询进行更新
通过脚本进行更新,主要在source里进行条件判定
POST test/_update/1 { "script" : { "source": "ctx._source.counter += params.count", "lang": "painless", "params" : { "count" : 4 } } }更新部分
POST test/_update/1 { "doc": { "name": "new_name" } }按条件查询进行更新
POST my-index-000001/_update_by_query?conflicts=proceed { "query": { "term": { "user.id": "kimchy" } } }
检索多个文档
GET /_mget { "docs": [ { "_index": "my-index-000001", "_id": "1" }, { "_index": "my-index-000001", "_id": "2" } ] }
复制索引
POST _reindex { "source": { "index": "my-index-000001" }, "dest": { "index": "my-new-index-000001" } }
6、Enrich 策略API
类似mysql的join方法
PUT /_enrich/policy/<enrich-policy>
DELETE /_enrich/policy/<enrich-policy>
GET /_enrich/policy/<name>
GET /_enrich/policy
GET /_enrich/policy/policy1,policy2
PUT /_enrich/policy/<enrich-policy>/_execute
POST /_enrich/policy/<enrich-policy>/_execute
PUT /_enrich/policy/my-policy { "match": { "indices": "users", "match_field": "email", "enrich_fields": ["first_name", "last_name", "city", "zip", "state"] } }DELETE /_enrich/policy/my-policyGET /_enrich/policy/my-policyGET /_enrich/policy/my-policy,other-policyPUT /_enrich/policy/my-policy/_executeGET /_enrich/_stats
7、Index API
索引相关的API集合
检查别名是否存在
HEAD _alias/<alias>
HEAD <target>/_alias/<alias>
添加别名
POST _aliases { "actions": [ { "add": { "index": "my-data-stream", "alias": "my-alias" } } ] }
文本分析
GET /_analyze { "analyzer" : "standard", "text" : "Quick Brown Foxes!" }
清除索引缓存
POST /my-index-000001/_cache/clear
如:
POST /my-index-000001/_cache/clear?fielddata=true POST /my-index-000001/_cache/clear?query=true POST /my-index-000001/_cache/clear?request=true
克隆索引
POST /my-index-000001/_clone/cloned-my-index-000001
POST /<index>/_clone/<target-index>
PUT /<index>/_clone/<target-index>
如:支持setting和aliases参数
POST /my_source_index/_clone/my_target_index { "settings": { "index.number_of_shards": 5 }, "aliases": { "my_search_indices": {} } }
关闭索引
POST /my-index-000001/_close
响应:
{ "acknowledged": true, "shards_acknowledged": true, "indices": { "my-index-000001": { "closed": true } } }
创建索引
PUT /<index>
如:可以设置settings、mappings、aliases等参数
PUT /my-index-000001 { "settings": { "index": { "number_of_shards": 3, "number_of_replicas": 2 } } }PUT /test { "settings": { "number_of_shards": 1 }, "mappings": { "properties": { "field1": { "type": "text" } } } }
删除别名
DELETE my-data-stream/_alias/my-alias
删除索引
DELETE /my-index-000001
删除索引模板
DELETE /_index_template/my-index-template
检查数据流、索引或者别名是否存在
HEAD my-data-stream
刷新数据流或者索引
POST /my-index-000001/_flush
索引分片融合
POST /my-index-000001/_forcemerge
如:
POST /my-index-000001,my-index-000002/_forcemerge
获取别名
GET my-data-stream/_alias/my-alias
获取属性映射
GET /my-index-000001/_mapping/field/user
获取索引
GET /my-index-000001
获取索引设置
GET /my-index-000001/_settings
获取索引模板
GET /_index_template/template_1
获取所有索引模板
GET /_index_template
获取索引映射
GET /my-index-000001/_mapping
导入悬垂索引
POST /_dangling/<index-uuid>?accept_data_loss=true
索引还原
GET /my-index-000001/_recovery
拆分索引到更多分片
POST /my-index-000001/_split/split-my-index-000001 { "settings": { "index.number_of_shards": 2 } }
更新索引
PUT /my-index-000001/_settings { "index" : { "number_of_replicas" : 2 } }
注释:
ElasticSearch官网文档提供了很多的RESTful API,比如机器学习API、脚本API、安全API、快照和存储API、SQL API、观测API等,具体详情可以查看官网信息,内容较多,本文只是讲解ElasticSearch基本的一些索引相关接口。