elasticsearch提供比较齐全的各种功能点的API,如/_cat,/_xpack等,该文档是基于ElasticSearch8.0版本进行梳理
提供关于x-pack特征信息,负责安全问题使用该功能
http://ip:9200/_xpack
GET /_xpack?categories=build,features 该请求只会返回build和features
GET /_xpack?human=false 该请求会移除human描述
提供关于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} /_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 实现模板排序
介绍集群节点通用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/
GET /_tasks
如:
GET _tasks GET _tasks?nodes=nodeId1,nodeId2 GET _tasks?nodes=nodeId1,nodeId2&actions=cluster:*
从投票配置表里删除或者添加符合主节点资格的节点
POST /_cluster/voting_config_exclusions?node_names=
POST /_cluster/voting_config_exclusions?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/
/usage
GET /_nodes/usage/
GET /_nodes/
/usage/
返回集群中每个选定节点的热线程
GET /_nodes/hot_threads
GET /_nodes/
/hot_threads
返回节点信息
GET /_nodes
GET /_nodes/
GET /_nodes/
GET /_nodes/
/
如:
# 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/
/reload_secure_settings
GET /_nodes/stats
GET /_nodes/
/stats
GET /_nodes/stats/
GET /_nodes/
/stats/
GET /_nodes/stats/
/
GET /_nodes/
/stats/ /
如:
# 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
状态:GET /_ccr/stats
创建复制索引:
PUT //_ccr/follow?wait_for_active_shards=1 { "remote_cluster" : " ", "leader_index" : " " } 暂停复制索引:
POST //_ccr/pause_follow 恢复复制索引:
POST //_ccr/resume_follow { } 将复制索引变成常规索引
POST //_ccr/unfollow 获取索引状态
GET //_ccr/stats 获取索引信息
GET //_ccr/info
将JSON文档添加到指定的数据流或索引,并使其可搜索。如果目标是索引并且文档已经存在,则请求更新文档并增加其版本
PUT /
/_doc/<_id>
POST /
/_doc/
PUT /
/_create/<_id>
POST /
/_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
/_doc/<_id>
HEAD
/_doc/<_id>
GET
/_source/<_id>
HEAD
/_source/<_id>
如:
GET my-index-000001/_doc/0 GET my-index-000001/_source/1 HEAD my-index-000001/_source/1
DELETE /
POST /
如:
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 /
POST /
通过脚本进行更新,主要在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" } }
类似mysql的join方法
PUT /_enrich/policy/
DELETE /_enrich/policy/
GET /_enrich/policy/
GET /_enrich/policy
GET /_enrich/policy/policy1,policy2
PUT /_enrich/policy/
POST /_enrich/policy/
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
索引相关的API集合
HEAD _alias/
HEAD
/_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 /
/_clone/
PUT /
/_clone/
如:支持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 /
如:可以设置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/?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基本的一些索引相关接口。
官网:欢迎来到 Elastic — Elasticsearch 和 Kibana 的开发者 | Elastic