1. 基础语法
1.1. 系统相关
# 查看集群信息 GET / # 查看所有索引情况 GET /_cat/indices?v # 查看节点 GET /_nodes/process?pretty # 查看集群健康状态 索引级别 GET /_cluster/health?level=indices # 查看索引恢复时间 GET /_recovery # 索引red,如下命令查看详细原因 GET /_cluster/allocation/explain # 允许集群使用通配符 PUT _cluster/settings { "transient": { "action.destructive_requires_name": "false" } }
1.2. 索引相关
1.2.1. 创建索引
PUT /product_info_sun { "settings": { "number_of_shards": 5, # 不填默认索引是1个分片 "number_of_replicas": 1 # 默认1个副本,最大为(节点-1) }, "mappings": { "products": { "properties": { "productName": { "type": "text", "analyzer": "ik_smart" }, "annual_rate":{ "type":"keyword" # text和keyword都是文本类型,keyword默认不分词 }, "describe": { "type": "text", "analyzer": "ik_smart" } } } } }
1.2.2. 删除索引
# 删除索引order DELETE /order # 删除索引order1和order2 DELETE order1,order2 # 删除全部索引 DELETE _all DELETE * # 正则表达式匹配,删除product_info开头的索引 DELETE product_info*
1.2.3. 增加索引字段
ElasticSearch不支持修改索引,但可以添加新的字段
# 样例PUT /product_info_sun/products/_mapping { "properties": { "age":{ "type": "integer" } } }
1.2.4. 修改索引配置
# 修改索引的setting配置,修改副本数为2 PUT /product_info_sun/_settings { "number_of_replicas": 2 }
1.2.5. 开关索引
索引关闭会使数据flush到盘,对外不能读写,可以通过indices的status来进行判断
# 关闭索引 test0621 POST /test0621/_close # 打开索引 test0621 POST /test0621/_open
1.2.6 查看索引(排序)
# 查看集群下所有索引GET_cat/indices?v# 查看集群下所有索引并排序# 按照docs.count降序排列GET_cat/indices?v&s=docs.count:desc# 按照pri做降序,再按照docs.count做升序排列(默认升序)GET/_cat/indices?v&s=pri:desc,docs.count
1.2.7. 清除索引缓存
GET {索引名称}/_cache/clear
1.2.8. 索引只读
1.2.8.1. 使用场景
- 磁盘使用率超过阈值,索引自动设置为只读
- es实例迁移,设置只读防止在迁移后有脏数据写入旧实例的问题
1.2.8.2. 单条索引只读
# 样例 PUT /{索引名称}/_settings { "index.blocks.read_only": true } # 或(不推荐,这个是kibana做的自动生成,取消只读的时候还是建议写全path) PUT /{索引名称}/_settings { "blocks.read_only": true }
# 样例 设置为false 或者直接设置为 null PUT /{索引名称}/_settings { "index.blocks.read_only": false }
1.2.8.3. 批量索引只读
- 需要先参考「1.1. 系统相关」的内容对通配符的使用限制进行放开
- 在kibana中使用`GET /_cat/indices?v&index={通配符表达式}`来验证通配符匹配上的所有索引是否符合预期
- 参考上文,将单条索引只读的请求中{索引名称}替换为{通配符表达式},对指定索引设置只读
# 样例 # 将product(售卖产品相关索引)均设为只读 PUT /product*/_settings { "index.blocks.read_only": true } # 大招 PUT /_all/_settings { "index.blocks.read_only": true }
1.3. 增删改查-6.7
1.3.1. 写入数据
1.3.1.1. 写入单条
POST /{索引名}/{_type}/{主键id} # 主键id可以不添加,会自动随机生成 { "productName":"666", "age":12 }
1.3.1.2. _bulk
POST /product_info_sun/products/_bulk {"index":{}} {"productName":"理财产品A","annual_rate":"3.2200%","describe":"180天定期理财,最低20000起投,收益稳定,可以自助选择消息推送"} {"index":{}} {"productName":"理财产品B","annual_rate":"3.1100%","describe":"90天定投产品,最低10000起投,每天收益到账消息推送"} {"index":{}} {"productName":"理财产品C","annual_rate":"3.3500%","describe":"270天定投产品,最低40000起投,每天收益立即到账消息推送"} {"index":{}} {"productName":"理财产品D","annual_rate":"3.1200%","describe":"90天定投产品,最低12000起投,每天收益到账消息推送"} {"index":{}} {"productName":"理财产品E","annual_rate":"3.0100%","describe":"30天定投产品推荐,最低8000起投,每天收益会消息推送"} {"index":{}} {"productName":"理财产品F","annual_rate":"2.7500%","describe":"热门短期产品,3天短期,无须任何手续费用,最低500起投,通过短信提示获取收益消息"}
1.3.1.3. _reindex
POST _reindex { "source": { "index": "product_info_sun", "size":1000, "query": { "match_all": {} } }, "dest": { "index": "product_info_sun2" } }
1.3.2. 查询数据
1.3.2.1. 查询索引全部数据
GET _search { "query": { "match_all": {} }, "size":100 }
1.3.2.2. 查询某个索引数据总数
get /_cat/count/product_info_sun?v
1.3.2.3. 查询某个索引单条文档(根据主键查询)
GET /{索引名}/{_type}/id # 样例 GET /product_info_sun/products/1
1.3.2.4. 查询某个索引全部数据
# 某个索引全部的数据 GET /product_info_sun/products/_search # GET 后面的第一个"/"也可以不加 # size控制返回列表多少 GET product_info/products/_search { "query": { "match_all": {} }, "size":5 }
1.3.2.5. match查询某个索引
GET /product_info/products/_search { "query": { "match": { "describe": "每天收益到账消息推送" } } }
1.3.2.6. range查询某个索引
GET /product_info/products/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1300%" } } } }
1.3.3. 删除数据
1.3.3.1. 根据文档主键_id删除
# 用法 DELETE /{索引名}/{_type}/{索引id} # 样例: DELETE /product_info_sun/products/Vq-rzokBZmCAOVEgbP20
1.3.3.2. delete_by_query
POST {索引名}/{_type}/_delete_by_query # 下面就是查询语句 { "query": { "match": { "productName": "产品D" } } }
1.3.4. 修改数据
1.3.4.1. 全量修改
# 用法 PUT /{索引名}/{_type}/{主键id} { 数据 } # 样例 PUT /product_info_sun/products/1 { "productName":"全量修改" ,"age":100 }
1.3.4.2. 增量修改(部分修改)
# 用法 POST /{索引名}/{_type}/{主键id}/_update { "doc": { 数据 } } # 样例 POST /product_info_sun/products/1/_update { "doc": { "age":1 } }
1.3.4.3. 注意事项
- es不同版本update语句写法不同,本样例以6.7的版本为例,7.X及其他版本具体可以参考官网文档如下所示https://www.elastic.co/guide/en/elasticsearch/reference/6.7/docs-update.html
- 如果文档更新时,文档数据中包含了索引mapping中没有的字段,则会按照默认规则创建索引,如果索引类型及参数不符合需求则需要先更改字段类型,再对数据做update或者insert。