bulk api可以在单个请求中一次执行多个索引或者删除操作,使用这种方式可以极大的提升索引性能

每一个操作要两个json串,语法如下:

{“action”: {“metadata”}}
{“data”}

举例,比如你现在要创建一个文档,放bulk里面 :

{“index”: {“_index”: “test_index”, “_type”, “test_type”, “_id”: “1”}}
{“test_field1”: “test1”, “test_field2”: “test2”}

注意: 每个一个jason 不能换行,两个json并且只有一个换行

有哪些类型的操作可以执行呢?
(1)delete:删除一个文档,只要1个json串就可以了
(2)create:PUT /index/type/id/_create,强制创建
(3)index:普通的put操作,可以是创建文档,也可以是全量替换文档
(4)update:执行的partial update操作

bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,会告诉你异常日志

3 请求路径 post

bulk请求的路径有三种和前面的mget的请求类似:url里面有了,body体里面就不用加了。
(1) /_bulk

(2)/{index}/_bulk

(3)/{index}/{type}/_bulk

  1. POST /test_index/_bulk
  2. { "delete": { "_type": "test_type", "_id": "3" }}
  3. { "create": { "_type": "test_type", "_id": "12" }}
  4. { "test_field": "test12" }
  5. { "index": { "_type": "test_type" }}
  6. { "test_field": "auto-generate id test" }
  7. { "index": { "_type": "test_type", "_id": "2" }}
  8. { "test_field": "replaced test2" }
  9. { "update": { "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
  10. { "doc" : {"test_field2" : "bulk test1"} }
  11. POST /test_index/test_type/_bulk
  12. { "delete": { "_id": "3" }}
  13. { "create": { "_id": "12" }}
  14. { "test_field": "test12" }
  15. { "index": { }}
  16. { "test_field": "auto-generate id test" }
  17. { "index": { "_id": "2" }}
  18. { "test_field": "replaced test2" }
  19. { "update": { "_id": "1", "_retry_on_conflict" : 3} }
  20. { "doc" : {"test_field2" : "bulk test1"} }