前言
在上一篇博客(https://blog.youkuaiyun.com/qq_28757391/article/details/105312677?spm=1001.2014.3001.5501)中我们介绍了简单的索引创建、数据插入、查询等API。今天这篇博客用于记录工作中经常使用的Restful API,以后还有新增将持续更新。
版本说明:Elasticsearch5.4.0,工作中用的版本比较老了,参考时请考虑高版本是否兼容。
常用Restful API
1、设置refresh间隔时间(系统默认1S)
PUT http://IP:9200/索引名/_settings
{
"index":{
"refresh_interval":"5s"
}
}
2、查看集群健康状况及相关信息
GET http://IP:9200/_nodes/process?pretty
3、查看集群的分片存储信息
GET http://IP:9200/_cat/shards?v
4、分片自平衡开关
PUT http://IP:9200/_cluster/settings
{
"transient":{"cluster.routing.allocation.enable":"all"}
}
# "all" 是开启 "none" 是关闭
5、单字段精确检索
POST http://IP:9200/索引名/类型名/_search
{
"query":{
"bool":{
"must":
[
{"term":{"age":"23"}} # age是字段,23是值
]
}
},
"from":0,
"size":10
}
6、查看索引打开状态
GET http://IP:9200/_cat/indices
7、打开/关闭索引
POST http://IP:9200/索引名/_open #开启
或者 http://IP:9200/索引名/_close #关闭
8、清空索引数据
POST http://IP:9200/索引名/类型名/_delete_by_query
{"query":{"match_all":{}}} #删除查询到的数据,可以根据检索条件删除数据
9、设置max_result_window参数
PUT http://IP:9200/索引名/_settings
{
"index":{
"max_result_window":1000000000
}
}
10、磁盘容量检查开关
PUT http://IP:9200/_cluster/settings
{
"transient":{"cluster.routing.allocation.disk.threshold_enabled":false} # true 开启
}
11、重新分配分片
POST http://IP:9200/_cluster/reroute
{
"commands":[
{
"allocate_stale_primary":{
"index":"索引名",
"shard":2, #分片编号
"node":"node2", #集群节点名称
"accept_data_loss":true
}
}
]
}
12、查看mapping
GET http://IP:9200/索引名/_mapping
13、分词器分词
POST http://IP:9200/索引名/_analyze?pretty=
{"text":"日本中国美国俄罗斯","analyzer":"ik_smart"} # ik_smart 分词器