一.文档批量操作
这里多个文档是指,批量操作多个文档,搜索查询文档将在之后的章节讲解
1.批量获取文档数据
批量获取文档数据是通过_mget的API来实现的
(1)在URL中不指定index和type
请求方式:GET
请求地址:_mget
功能说明 : 可以通过ID批量获取不同index和type的数据
请求参数:
- docs : 文档数组参数
- _index : 指定index
- _type : 指定type
- _id : 指定id
- _source : 指定要查询的字段
GET _mget { "docs": [ { "_index": "es_db", "_type": "_doc", "_id": 1 }, { "_index": "es_db", "_type": "_doc", "_id": 2 } ] }
响应结果如下:
{ "docs" : [ { "_index" : "es_db", "_type" : "_doc", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "name" : "张三", "sex" : 1, "age" : 25, "address" : "广州天河公园", "remark" : "java developer" } }, { "_index" : "es_db", "_type" : "_doc", "_id" : "2", "_version" : 1, "_seq_no" : 1, "_primary_term" : 1, "found" : true, "_source" : { "name" : "李四", "sex" : 1, "age" : 28, "address" : "广州荔湾大厦", "remark" : "java assistant" } } ] }
(2)在URL中指定index
请求方式:GET
请求地址:/{{indexName}}/_mget
功能说明 : 可以通过ID批量获取不同index和type的数据请求参数:
docs : 文档数组参数
_index : 指定index
_type : 指定type
_id : 指定id
_source : 指定要查询的字段
GET /user/_mget { "docs": [ { "_type":"_doc", "_id": 3 }, { "_type":"_doc", "_id": 4 } ] }
返回结果(由于我没有创建 /user 索引所以返回 index_not_found_exception):
#! Deprecation: [types removal] Specifying types in multi get requests is deprecated. { "docs" : [ { "_index" : "user", "_type" : "_doc", "_id" : "3", "error" : { "root_cause" : [ { "type" : "index_not_found_exception", "reason" : "no such index [user]", "resource.type" : "index_expression", "resource.id" : "user", "index_uuid" : "_na_", "index" : "user" } ], "type" : "index_not_found_exception", "reason" : "no such index [user]", "resource.type" : "index_expression", "resource.id" : "user", "index_uuid" : "_na_", "index" : "user" } }, { "_index" : "user", "_type" : "_doc", "_id" : "4", "error" : { "root_cause" : [ { "type" : "index_not_found_exception", "reason" : "no such index [user]", "resource.type" : "index_expression", "resource.id" : "user", "index_uuid" : "_na_", "index" : "user" } ], "type" : "index_not_found_exception", "reason" : "no such index [user]", "resource.type" : "index_expression", "resource.id" : "user", "index_uuid" : "_na_", "index" : "user" } } ] }
(3)在URL中指定index和type
请求方式:GET
请求地址:/{{indexName}}/{{typeName}}/_mget
功能说明 : 可以通过ID批量获取不同index和type的数据
请求参数:
docs : 文档数组参数
_index : 指定index
_type : 指定type
_id : 指定id
_source : 指定要查询的字段
GET /es_db/_doc/_mget { "docs": [ { "_id": 1 }, { "_id": 2 } ] }
返回结果
#! Deprecation: [types removal] Specifying types in multi get requests is deprecated. { "docs" : [ { "_index" : "es_db", "_type" : "_doc", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "name" : "张三", "sex" : 1, "age" : 25, "address" : "广州天河公园", "remark" : "java developer" } }, { "_index" : "es_db", "_type" : "_doc", "_id" : "2", "_version" : 1, "_seq_no" : 1, "_primary_term" : 1, "found" : true, "_source" : { "name" : "李四", "sex" : 1, "age" : 28, "address" : "广州荔湾大厦", "remark" : "java assistant" } } ] }
2.批量操作文档数据
批量对文档进行写操作是通过_bulk的API来实现的
请求方式:POST
请求地址:_bulk
请求参数:通过_bulk操作文档,一般至少有两行参数(或偶数行参数)
第一行参数为指定操作的类型及操作的对象
(index,type和id)
第二行参数才是操作的数据
参数类似于:
{"actionName":{"_index":"indexName", "_type":"typeName","_id":"id"}} {"field1":"value1", "field2":"value2"}
actionName:表示操作类型,主要有create,index,delete和update
(1)批量创建文档create
POST _bulk {"create":{"_index":"article", "_type":"_doc", "_id":3}} {"id":3,"title":"李白","content":"李白666","tags":["java", "面向对 象"],"create_time":1554015482530} {"create":{"_index":"article", "_type":"_doc", "_id":4}} {"id":4,"title":"李白","content":"李白NB","tags":["java", "面向对 象"],"create_time":1554015482530}
返回结果
{ "error" : { "root_cause" : [ { "type" : "json_parse_exception", "reason" : "Invalid UTF-8 start byte 0xb1\n at [Source: (byte[])\"POST /_bulk?pretty=true HTTP/1.1\r\nx-forwarded-for: 127.0.0.1\r\nx-forwarded-port: 61337\r\nx-forwarded-proto: http\r\nx-forwarded-host: localhost:5601\r\ncontent-type: application/json\r\nhost: localhost\r\ntransfer-encoding: chunked\r\nConnection: close\r\n\r\n14d\r\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":3}}\n{\"id\":3,\"title\":\"李白\",\"content\":\"李白666\",\"tags\":[\"java\", \"面向对 \n象\"],\"create_time\":1554015482530}\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":4}}\n{\"id\":4,\"title\":\"李白\",\"c\"[truncated 89 bytes]; line: 1, column: 3]" } ], "type" : "json_parse_exception", "reason" : "Invalid UTF-8 start byte 0xb1\n at [Source: (byte[])\"POST /_bulk?pretty=true HTTP/1.1\r\nx-forwarded-for: 127.0.0.1\r\nx-forwarded-port: 61337\r\nx-forwarded-proto: http\r\nx-forwarded-host: localhost:5601\r\ncontent-type: application/json\r\nhost: localhost\r\ntransfer-encoding: chunked\r\nConnection: close\r\n\r\n14d\r\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":3}}\n{\"id\":3,\"title\":\"李白\",\"content\":\"李白666\",\"tags\":[\"java\", \"面向对 \n象\"],\"create_time\":1554015482530}\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":4}}\n{\"id\":4,\"title\":\"李白\",\"c\"[truncated 89 bytes]; line: 1, column: 3]" }, "status" : 400 }
(2)普通创建或全量替换index
POST _bulk {"index":{"_index":"article", "_type":"_doc", "_id":3}} {"id":3,"title":"杜甫(一)","content":"杜甫666","tags":["java", "面向对象"],"create_time":1554015482530} {"index":{"_index":"article", "_type":"_doc", "_id":4}} {"id":4,"title":"杜甫(二)", "content":"杜甫NB","tags":["java", "面向对象"],"create_time":1554015482530}
**如果原文档不存在,则是创建 **
**如果原文档存在,则是替换(全量修改原文档) **
返回结果
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated. { "took" : 651, "errors" : false, "items" : [ { "index" : { "_index" : "article", "_type" : "_doc", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "article", "_type" : "_doc", "_id" : "4", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201 } } ] }
(3)批量删除delete
POST _bulk {"delete":{"_index":"article", "_type":"_doc", "_id":3}} {"delete":{"_index":"article", "_type":"_doc", "_id":4}}
返回结果
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated. { "took" : 32, "errors" : false, "items" : [ { "delete" : { "_index" : "article", "_type" : "_doc", "_id" : "3", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1, "status" : 200 } }, { "delete" : { "_index" : "article", "_type" : "_doc", "_id" : "4", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1, "status" : 200 } } ] }
(4)批量修改update
POST _bulk {"update":{"_index":"article", "_type":"_doc", "_id":3}} {"doc":{"title":"ES大法必修内功"}} {"update":{"_index":"article", "_type":"_doc", "_id":4}} {"doc":{"create_time":1554018421008}}
返回结果
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated. { "took" : 3, "errors" : true, "items" : [ { "update" : { "_index" : "article", "_type" : "_doc", "_id" : "3", "status" : 404, "error" : { "type" : "document_missing_exception", "reason" : "[_doc][3]: document missing", "index_uuid" : "uTqmplLRR3CpwQXWtAvJ7Q", "shard" : "0", "index" : "article" } } }, { "update" : { "_index" : "article", "_type" : "_doc", "_id" : "4", "status" : 404, "error" : { "type" : "document_missing_exception", "reason" : "[_doc][4]: document missing", "index_uuid" : "uTqmplLRR3CpwQXWtAvJ7Q", "shard" : "0", "index" : "article" } } } ] }