1 /** 2 * 批量更新文档 3 * 4 * @param index ES索引 5 * @param documents 待提交的批量文档 6 * @param uuidKey 文档中ID字段对应的key值 7 */ 8 public BulkResponse updateDocumentsAsBatch(String index, List<Map<String, Object>> documents, String uuidKey) { 9 BulkResponse response = null; 10 if (StringUtils.isBlank(index) || CollectionUtils.isEmpty(documents)) { 11 log.warn("Es index is blank or documents is empty."); 12 return response; 13 } 14 15 try { 16 int size = documents.size(); 17 BulkRequest bulkRequest = new BulkRequest(); 18 for (int i = 0; i < size; i++) { 19 Map<String, Object> document = documents.get(i); 20 if (MapUtils.isEmpty(document) || !document.containsKey(uuidKey)) { 21 continue; 22 } 23 bulkRequest.add(new UpdateRequest(index, document.get(uuidKey).toString()).doc(document)); 24 } 25 response = client.bulk(bulkRequest, RequestOptions.DEFAULT); 26 } catch (Exception e) { 27 log.error("Update documents to es as batch failed!", e); 28 } 29 return response; 30 }