3、查询文档
GET customer/external/1
http://192.168.107.129:9200/customer/external/1
响应
{ "_index": "customer", //在哪个索引 "_type": "external", //在哪个类型 "_id": "1", //记录 id "_version": 2, //版本号 "_seq_no": 1, //并发控制字段,每次更新就会+1,用来做乐观锁 "_primary_term": 1, //同上,主分片重新分配,如重启,就会变化"found": true, "_source": { //真正的内容 "name": "John Doe" } }
更新携带 ?if_seq_no=0&if_primary_term=1
if_seq_no=0乐观锁的版本号,当不符合版本号的时候报409
{ "error": { "root_cause": [ { "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, required seqNo [6], primary term [5]. current document has seqNo [7] and primary term [5]", "index_uuid": "Bdd3svLfS9ioGHqqBnxzdQ", "shard": "0", "index": "customer" } ], "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, required seqNo [6], primary term [5]. current document has seqNo [7] and primary term [5]", "index_uuid": "Bdd3svLfS9ioGHqqBnxzdQ", "shard": "0", "index": "customer" }, "status": 409 }
4、更新文档
带_update或put更新的post请求
POST customer/external/1/_update
{ "doc":{ "name": "John123" } }
响应 :
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 5, "result": "updated",//注意这个地方是updated的更新 "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 8, "_primary_term": 5 }
json内容不变再一次发送响应:
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 7, "result": "noop",//再一次发送的时候不是更新了 "_shards": { "total": 0, "successful": 0, "failed": 0 }, "_seq_no": 10, "_primary_term": 5 }
post带_update对比原来的数据,与原来的一样什么都不做version和sql_no也不会改变
不带_update或put更新的post请求
POST customer/external/1
{ "name": "John Doe2" }
不会去比对信息
PUT customer/external/1
同理
总结
不同:POST 操作会对比源文档数据,如果相同不会有什么操作,文档 version 不增加
PUT 操作总会将数据重新保存并增加 version 版本;
带_update 对比元数据如果一样就不进行任何操作。
看场景;
对于大并发更新,不带 update;
对于大并发查询偶尔更新,带 update;对比更新,重新计算分配规则
更新同时增加属性
POST customer/external/1/_update
1. { 2. "doc": { "name": "Jane Doe", "age": 20 } 3. }
PUT 和 POST 不带_update 也可以
5、删除文档&索引
DELETE customer/external/1
响应:
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 8, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 11, "_primary_term": 5 }
DELETE customer
响应 :
1. { 2. "acknowledged": true 3. }
不存在删除类型type的玩意
6、bulk 批量 API
POST customer/external/_bulk {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" }
语法格式: { action: { metadata }}\n { request body }\n { action: { metadata }}\n { request body }\n
复杂的例子:
POST /_bulk { "delete": { "_index": "website", "_type": "blog", "_id": "123" }} { "create": { "_index": "website", "_type": "blog", "_id": "123" }} { "title": "My first blog post" } { "index": { "_index": "website", "_type": "blog" }} { "title": "My second blog post" } { "update": { "_index": "website", "_type": "blog", "_id": "123"} } { "doc" : {"title" : "My updated blog post"} }
响应
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated. { "took" : 137, "errors" : false, "items" : [ { "delete" : { "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 1, "result" : "not_found", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 404 } }, { "create" : { "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 2, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "website", "_type" : "blog", "_id" : "5ycmXYUB7T24Ga4RRRSB", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1, "status" : 201 } }, { "update" : { "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 3, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1, "status" : 200 } } ] }