目录

Document APIs - Bulk API

优质
小牛编辑
139浏览
2023-12-01

Bulk API

Bulk API,批量插入:

  1. import static org.elasticsearch.common.xcontent.XContentFactory.*;
  1. BulkRequestBuilder bulkRequest = client.prepareBulk();
  2. // either use client#prepare, or use Requests# to directly build index/delete requests
  3. bulkRequest.add(client.prepareIndex("twitter", "tweet", "1")
  4. .setSource(jsonBuilder()
  5. .startObject()
  6. .field("user", "kimchy")
  7. .field("postDate", new Date())
  8. .field("message", "trying out Elasticsearch")
  9. .endObject()
  10. )
  11. );
  12. bulkRequest.add(client.prepareIndex("twitter", "tweet", "2")
  13. .setSource(jsonBuilder()
  14. .startObject()
  15. .field("user", "kimchy")
  16. .field("postDate", new Date())
  17. .field("message", "another post")
  18. .endObject()
  19. )
  20. );
  21. BulkResponse bulkResponse = bulkRequest.get();
  22. if (bulkResponse.hasFailures()) {
  23. // process failures by iterating through each bulk response item
  24. //处理失败
  25. }