索引 index
创建
sh
复制代码
# 1.创建索引 - PUT/索引名====> PUT /products - 注意: 1.ES中索引健康转态red(索引不可用) 、yellwo(索引可用,存在风险)、green(健康) 2.默认ES在创建索引时回为索引创建1个备份索引和一个primary索引 # 2.创建索引进行索引分片配置 PUT /products { "settings" : { "number_of_shards" : 1,#指定主分片的数量 "number_of_replicas" : 0 #指定副本分片的数量 } }
查询
bash
复制代码
# 查看es中的索引 GET /_cat/indices # 查看es中的索引,同时查看状态 GET /_cat/indices?v
删除
bash
复制代码
# 删除索引 DELETE /products
没有修改的命令,除非删除
映射 mapping
创建
类型 | |
字符串类型 | keyword(关键字 关键词) 、 text(一段文本) |
数字类型 | integer long |
小数类型 | float double |
布尔类型 | boolean |
日期类型 | date |
bash
复制代码
# 创建商品索引 products 指定 mapping{id,title,price,created_at,description} PUT /products { "settings": { "number_of_replicas": 0, "number_of_shards": 1 }, "mappings": { "properties": { "id":{ "type": "integer" }, "title":{ "type": "keyword" }, "price":{ "type": "double" }, "created_at":{ "type": "date" }, "description":{ "type": "text" } } } }
查看
bash
复制代码
# 查看某个索引的映射信息 mapping GET /products/_mapping
需要注意,索引一旦创建,不能修改和删除。要删除只能通过删除映射再重新创建
文档 document
创建文档
在创建 索引 & 映射 之后 Json格式
sh
复制代码
POST /products/_doc/1 #指定文档id { "title" : "iphone13" , "price" : 8999.99, "created_at" : "2821-09-15”, "description" : "iPhone 13屏幕采用6.1英寸OLED屏幕。" }
运行结果
json
复制代码
{ "_index": "products", "_id": "1", "_version": 6, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 5, "_primary_term": 1 }
sh
复制代码
POST /products/_doc/ #自动生成文档id { "title" : "iphone14" , "price" : 8999.99, "created_at" : "2021-09-15", "description" : "iPhone 13屏幕采用6.8英寸OLED屏幕” } }
运行结果
json
复制代码
{ "_index": "products", "_id": "CurihYsBq0CcYHMB1rPB", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 6, "_primary_term": 1 }
文档查询
bash
复制代码
# 文档查询 基于 id 查询 GET /products/_doc/CurihYsBq0CcYHMB1rPB
删除文档
bash
复制代码
DELETE /products/_doc/CurihYsBq0CcYHMB1rPB
运行结果
bash
复制代码
{ "_index": "products", "_id": "CurihYsBq0CcYHMB1rPB", "_version": 2, "result": "deleted", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 7, "_primary_term": 1 } # 查询后 { "_index": "products", "_id": "CurihYsBq0CcYHMB1rPB", "found": false }
更新文档
bash
复制代码
# 更新文档 PUT /products/_doc/1 { "description":"小老鼠真香" }
运行结果
bash
复制代码
{ "_index": "products", "_id": "1", "_version": 7, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 8, "_primary_term": 1 } # 查看内容 { "_index": "products", "_id": "1", "_version": 7, "_seq_no": 8, "_primary_term": 1, "found": true, "_source": { "description": "小老鼠真香" } }
bash
复制代码
# 更新文档,基于指定字段进行更新 POST /products/_doc/1/_update { "doc":{ "price":56.4, "title":"我是cfd" } }
上面的是视频里面7.10的版本的写法,笔者所用的版本是8.10,产生了如下报错
json
复制代码
{ "error": "no handler found for uri [/products/_doc/1/_update?pretty=true] and method [POST]" }
在官方文档查看正确用法如下
sh
复制代码
# 更新文档,基于指定字段进行更新 POST /products/_update/1 { "doc":{ "price":1.0, "title":"我是cfddfc" } }
【ElasticSearch(四)】PUT&POST更新数据、DELETE删除数据、_bulk批量操作 - musecho - 博客园 (cnblogs.com)Update API 官方8.10版本关于post更新的介绍,相比视频里面7的版本有些不同 | Elasticsearch Guide [8.10] | Elastic
批量操作
sh
复制代码
# 文档的批量操作 _bulk # 不能有回车换行 POST ./products/_doc/_bulk {"index":{"_id":2}} {"id" : 2,"title":"大老鼠","price":0.5,"created_at":"2012-11-12","description":"小浣熊真香!"} {"index":{"_id":3}} {"id" : 2,"title":"与馒头","price":0.5,"created_at":"2012-11-12","description":"馒头真香啊"}
同样,7.0版本上面的用法是正确的,放在8.0版本里面就会报错,如下
sh
复制代码
{ "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "Malformed content, found extra data after parsing: START_OBJECT" } ], "type": "illegal_argument_exception", "reason": "Malformed content, found extra data after parsing: START_OBJECT" }, "status": 400 }
正确的用法
sh
复制代码
POST _bulk {"index":{"_id":2,"_index" : "products"} } {"id" : 2,"title":"大老鼠","price":0.5,"created_at":"2012-11-12","description":"小浣熊真香!"} {"index":{"_id":3,"_index" : "products"} } {"id" : 2,"title":"与馒头","price":0.5,"created_at":"2012-11-12","description":"馒头真香啊"}
批量不同操作
bash
复制代码
# 文档批量操作 添加 更新 删除 POST _bulk {"index":{"_id":4,"_index" : "products"} } {"id" : 4,"title":"好吃的薯片","price":100,"created_at":"2012-11-12","description":"薯片真香!"} {"update":{"_id":3,"_index" : "products"} } {"doc":{"description":"馒头太好吃了吧"}} {"delete":{"_id":2,"_index" : "products"} }
结果如下,_bulk
里的操作是非原子的,一个执行出错并不会妨碍其他的操作继续执行,在返回时按照执行的顺序返回消息。
json
复制代码
{ "errors": false, "took": 4, "items": [ { "index": { "_index": "products", "_id": "4", "_version": 2, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 28, "_primary_term": 2, "status": 200 } }, { "update": { "_index": "products", "_id": "3", "_version": 3, "result": "noop", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 26, "_primary_term": 2, "status": 200 } }, { "delete": { "_index": "products", "_id": "2", "_version": 4, "result": "not_found", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 29, "_primary_term": 2, "status": 404 } } ] }