1.新增
使用put进行新增,必须使用id。
PUT /nba/_doc/1 { "name":"哈登", "team_name":"⽕箭", "position":"得分后卫", "play_year":"10", "jerse_no":"13" }
结果:
{ "_index" : "nba", "_type" : "_doc", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 3 }
2.新增
这种不指定id
POST /nba/_doc/ { "name":"哈登", "team_name":"⽕箭", "position":"得分后卫", "play_year":"10", "jerse_no":"13" }
结果:
{ "_index" : "nba", "_type" : "_doc", "_id" : "xpGZJnEB_xXvz4I-POcw", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 3 }
3.查找
GET /nba/_doc/1
4.说明
不存在的索引,新增文档,也可以新建出索引。不过,实验发现都是keyword类型。
PUT /wba/_doc/1 { "name":"哈登2", "team_name":"⽕箭", "position":"得分后卫", "play_year":"10", "jerse_no":"13" }
结果:
{ "_index" : "wba", "_type" : "_doc", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
查看索引:
GET /wba
结果:
{ "wba" : { "aliases" : { }, "mappings" : { "properties" : { "jerse_no" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "play_year" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "position" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "team_name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }, "settings" : { "index" : { "creation_date" : "1585491459227", "number_of_shards" : "1", "number_of_replicas" : "1", "uuid" : "5919dIHxQYGZXQ4DrtuCyQ", "version" : { "created" : "7020099" }, "provided_name" : "wba" } } } }
5.指定操作类型
为啥需要呢
PUT /nba/_doc/1 { "name":"哈登2", "team_name":"⽕箭", "position":"得分后卫", "play_year":"10", "jerse_no":"13" } GET /nba/_doc/1
结果:
{ "_index" : "nba", "_type" : "_doc", "_id" : "1", "_version" : 5, "_seq_no" : 5, "_primary_term" : 3, "found" : true, "_source" : { "name" : "哈登2", "team_name" : "⽕箭", "position" : "得分后卫", "play_year" : "10", "jerse_no" : "13" } }
然后,想新增
PUT /nba/_doc/1 { "name":"哈登", "team_name":"⽕箭", "position":"得分后卫", "play_year":"10", "jerse_no":"13" } GET /nba/_doc/1
结果:
# PUT /nba/_doc/1 { "_index" : "nba", "_type" : "_doc", "_id" : "1", "_version" : 6, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 6, "_primary_term" : 3 } # GET /nba/_doc/1 { "_index" : "nba", "_type" : "_doc", "_id" : "1", "_version" : 6, "_seq_no" : 6, "_primary_term" : 3, "found" : true, "_source" : { "name" : "哈登", "team_name" : "⽕箭", "position" : "得分后卫", "play_year" : "10", "jerse_no" : "13" } }
发现,不小心把前面的给覆盖了。
然后,怎么解决呢?
PUT /nba/_doc/1?op_type=create { "name":"哈登2", "team_name":"⽕箭", "position":"得分后卫", "play_year":"10", "jerse_no":"13" }
结果:
{ "error": { "root_cause": [ { "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, document already exists (current version [6])", "index_uuid": "q_fuLgv1Sjyb8yncRsCWZQ", "shard": "0", "index": "nba" } ], "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, document already exists (current version [6])", "index_uuid": "q_fuLgv1Sjyb8yncRsCWZQ", "shard": "0", "index": "nba" }, "status": 409 }
6.批量查询
post方式,get也可以
POST /_mget { "docs":[ { "_index": "nba", "_type": "_doc", "_id": 2 }, { "_index": "songs", "_type": "_doc", "_id": 1 } ] }
效果:
#! Deprecation: [types removal] Specifying types in multi get requests is deprecated. { "docs" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "2", "_version" : 1, "_seq_no" : 7, "_primary_term" : 3, "found" : true, "_source" : { "name" : "哈登2", "team_name" : "⽕箭", "position" : "得分后卫", "play_year" : "10", "jerse_no" : "13" } }, { "_index" : "songs", "_type" : "_doc", "_id" : "1", "_version" : 8, "_seq_no" : 7, "_primary_term" : 1, "found" : true, "_source" : { "name" : "说好不哭", "author" : "周杰伦" } } ] }
7.更加简化的查询方式
POST /nba/_doc/_mget { "docs":[ { "_id":"1" }, { "_id":"2" } ] }
结果:
#! Deprecation: [types removal] Specifying types in multi get requests is deprecated. { "docs" : [ { "_index" : "nba", "_type" : "_doc", "_id" : "1", "_version" : 6, "_seq_no" : 6, "_primary_term" : 3, "found" : true, "_source" : { "name" : "哈登", "team_name" : "⽕箭", "position" : "得分后卫", "play_year" : "10", "jerse_no" : "13" } }, { "_index" : "nba", "_type" : "_doc", "_id" : "2", "_version" : 1, "_seq_no" : 7, "_primary_term" : 3, "found" : true, "_source" : { "name" : "哈登2", "team_name" : "⽕箭", "position" : "得分后卫", "play_year" : "10", "jerse_no" : "13" } } ] }
8.更加简化的mget
POST /nba/_doc/_mget { "ids":["1","2"] }
9.修改
只更新了一部分字段
POST /nba/_update/2 { "doc":{ "play_year":"1000", "jerse_no":"1300" } } GET /nba/_doc/2
结果:
# POST /nba/_update/2 { "_index" : "nba", "_type" : "_doc", "_id" : "2", "_version" : 8, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 14, "_primary_term" : 3 } # GET /nba/_doc/2 { "_index" : "nba", "_type" : "_doc", "_id" : "2", "_version" : 8, "_seq_no" : 14, "_primary_term" : 3, "found" : true, "_source" : { "name" : "哈登2", "team_name" : "⽕箭", "position" : "得分后卫", "play_year" : "1000", "jerse_no" : "1300" } }
10.新增一个字段
POST /nba/_update/2 { "script":"ctx._source.age=19" } GET /nba/_doc/2
结果:
# POST /nba/_update/2 { "_index" : "nba", "_type" : "_doc", "_id" : "2", "_version" : 9, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 15, "_primary_term" : 3 } # GET /nba/_doc/2 { "_index" : "nba", "_type" : "_doc", "_id" : "2", "_version" : 9, "_seq_no" : 15, "_primary_term" : 3, "found" : true, "_source" : { "name" : "哈登2", "team_name" : "⽕箭", "position" : "得分后卫", "play_year" : "1000", "jerse_no" : "1300", "age" : 19 } }
11.删除一个文档
POST /nba/_update/2 { "script":"ctx._source.remove("age")" } GET /nba/_doc/2
结果:
# POST /nba/_update/2 { "_index" : "nba", "_type" : "_doc", "_id" : "2", "_version" : 10, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 17, "_primary_term" : 3 } # GET /nba/_doc/2 { "_index" : "nba", "_type" : "_doc", "_id" : "2", "_version" : 10, "_seq_no" : 17, "_primary_term" : 3, "found" : true, "_source" : { "name" : "哈登2", "team_name" : "⽕箭", "position" : "得分后卫", "play_year" : "1000", "jerse_no" : "1300" } }
12.根据指定参数,更新文档
POST /nba/_update/3 { "script": { "source": "ctx._source.allstar += params.allstar", "params": { "allstar": 4 } }, "upsert": { "allstar": 1 } }
结果:
# POST /nba/_update/3 { "_index" : "nba", "_type" : "_doc", "_id" : "3", "_version" : 3, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 20, "_primary_term" : 3 } # GET /nba/_doc/3 { "_index" : "nba", "_type" : "_doc", "_id" : "3", "_version" : 3, "_seq_no" : 20, "_primary_term" : 3, "found" : true, "_source" : { "allstar" : 9 } }
发现这里version是3。
第一次不存在则为1,第二次是5,第三次是9.
upsert:不存在,就是后面的数,存在就使用上面的数据
13.删除
DELETE /nba/_doc/3 GET /nba/_doc/3
结果:
# DELETE /nba/_doc/3 { "_index" : "nba", "_type" : "_doc", "_id" : "3", "_version" : 4, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 21, "_primary_term" : 3 } # GET /nba/_doc/3 { "_index" : "nba", "_type" : "_doc", "_id" : "3", "found" : false }