多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**1. 创建文档** (1)语法1:`POST /index/_doc` ```json POST /db_01/_doc { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": "3999.0" } ``` 成功返回的数据如下: ```json { "_index" : "db_01", "_type" : "_doc", "_id" : "gRBajnwBWCWQahLf3sG9", # 随机生成,相当于MySQL中的主键 "_version" : 1, "result" : "created", # created创建成功 "_shards" : { "total" : 2, # 分片总数 "successful" : 1, # 分片成功次数 "failed" : 0 # 分片失败次数 }, "_seq_no" : 0, "_primary_term" : 1 } ``` (2)语法2:`POST /index/_doc/id`、`PUT /index/_doc/id` ```json POST /db_01/_doc/2 { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": "3999.0" } ``` 成功返回的数据如下: ```json { "_index" : "db_01", "_type" : "_doc", "_id" : "2", # id由自己定义 "_version" : 2, #对同一个id的文档增加多次,每增加一次版本号增1 "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 2 } ``` **2. 查看文档** (1)根据`id`查询文档:`GET /index/_doc/id` ```json GET /db_01/_doc/2 ``` 成功返回的数据如下: ```json { "_index" : "db_01", "_type" : "_doc", "_id" : "2", "_version" : 2, "_seq_no" : 3, "_primary_term" : 2, "found" : true, "_source" : { "title" : "小米手机", "category" : "小米", "images" : "http://www.gulixueyuan.com/xm.jpg", "price" : "3999.0" } } ``` **3. 修改文档** (1)根据id修改文档:`POST /index/_doc/id`、`PUT /index/_doc/id` ```json POST /db_01/_doc/2 { "title": "小米手机", "category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg", "price": "3999.0" } ``` 成功后返回如下数据: ```json { "_index" : "db_01", "_type" : "_doc", "_id" : "2", "_version" : 6, #对同一个id每修改一次,版本就会增1 "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 7, "_primary_term" : 2 } ``` **4. 修改字段** (1)`POST /index/_update/id` ```json POST /db_01/_update/2 { "doc": { "price": 3000.0 #修改前是3999.0 } } ``` 查看修改结果`GET /db_01/_doc/2`: ```json { "_index" : "db_01", "_type" : "_doc", "_id" : "2", "_version" : 7, "_seq_no" : 8, "_primary_term" : 2, "found" : true, "_source" : { "title" : "小米手机", "category" : "小米", "images" : "http://www.gulixueyuan.com/xm.jpg", "price" : 3000.0 } } ``` **5. 删除文档** 删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。 (1)根据id删除文档:`DELETE /index/_doc/id` ```json DELETE /db_01/_doc/1 ``` 成功后返回如下数据: ```json { "_index" : "db_01", "_type" : "_doc", "_id" : "1", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 11, "_primary_term" : 2 } ```