ES03-常用API
文章目录

1-参考网址
- elasticsearch官网地址:https://www.elastic.co/
- 安装elasticsearch9.0.0参考:https://zhuanlan.zhihu.com/p/1920780524991017021
- 安装elasticsearch9.0.0参考:http://www.rhkb.cn/news/51907.html
2-知识总结
- 1)Index操作-增删改查
- 2)DOC操作-增删改查
- 3)query操作
- 4)Aggregations聚合
3-高频核心操作速查表
1-L1-Index
# | 动作 | 一句话 | 请求示例 |
---|---|---|---|
1.1 | 创建索引 | 指定 mapping/settings/alias | PUT /shop_v1 { "settings":{ "number_of_shards":3 }, "mappings":{ "properties":{ "title":{ "type":"text" } } }, "aliases":{ "shop":{} } } |
1.2 | 删除索引 | 危险,谨慎 | DELETE /shop_v1 |
1.3 | 索引是否存在 | HEAD 判断 | HEAD /shop_v1 |
1.4 | 关闭/打开 | 临时下线 | POST /shop_v1/_close / _open |
1.5 | 刷新 | 刷缓存可见 | POST /shop_v1/_refresh |
1.6 | 别名切换 | 零停机重建 | POST _aliases { "actions":[ { "remove":{ "index":"shop_v1","alias":"shop" } },{ "add":{ "index":"shop_v2","alias":"shop" } } ] } |
2-L1-Doc
# | 动作 | 一句话 | 请求示例 |
---|---|---|---|
2.1 | 新建/更新 | 指定 ID 幂等 | PUT /shop/_doc/1001 { "title":"小米","price":4999 } |
2.2 | 局部更新 | 不改其他字段 | POST /shop/_update/1001 { "doc":{ "price":4899 } } |
2.3 | 获取 | 拿整条 | GET /shop/_doc/1001 |
2.4 | 删除 | 单条 | DELETE /shop/_doc/1001 |
2.5 | 批量写入 | 一行指令一行数据 | POST _bulk\n{"index":{"_index":"shop","_id":"2001"}}\n{"title":"iPhone"}\n{"delete":{"_index":"shop","_id":"2002"}} |
3-L1-Query
# | 动作 | 一句话 | 请求示例 |
---|---|---|---|
3.1 | match | 全文分词 | { "query":{ "match":{ "title":"小米手机" } } } |
3.2 | term | 精准匹配 | { "query":{ "term":{ "price":4999 } } } |
3.3 | range | 区间 | { "query":{ "range":{ "price":{ "gte":2000,"lte":5000 } } } } |
3.4 | bool | 组合过滤 | { "query":{ "bool":{ "must":[ { "match":{ "title":"小米" } } ], "filter":[ { "range":{ "price":{ "gte":3000 } } } ] } } } |
3.5 | match_all + 分页 | 全量+from/size | { "query":{ "match_all":{} }, "from":0,"size":20 } |
4-L1-Aggregations
# | 动作 | 一句话 | 请求示例 |
---|---|---|---|
4.1 | avg/max/min | 指标 | { "size":0, "aggs":{ "avg_price":{ "avg":{ "field":"price" } } } } |
4.2 | terms桶 | 分组计数 | { "size":0, "aggs":{ "by_tag":{ "terms":{ "field":"tags.keyword" } } } } |
4.3 | date_histogram | 时间直方图 | { "size":0, "aggs":{ "sales_per_day":{ "date_histogram":{ "field":"created","calendar_interval":"1d" } } } } |
4.4 | range桶 | 区间 | { "size":0, "aggs":{ "price_ranges":{ "range":{ "field":"price","ranges":[ {"to":2000},{"from":2000,"to":5000},{"from":5000} ] } } } } |
4.5 | 嵌套聚合 | 桶内再算指标 | { "size":0, "aggs":{ "by_tag":{ "terms":{ "field":"tags.keyword" }, "aggs":{ "avg_price":{ "avg":{ "field":"price" } } } } } } |