方式一:修改文档中的某些特定的字段值
try { Object[] objects = new Object[]{}; // 查询的结果字段,类似于(select id, XSE from ..) String[] fields = {"id", "XSE"}; FetchSourceContext sourceContext = new FetchSourceContext(true, fields, null); SearchSourceBuilder sb = new SearchSourceBuilder(); sb.size(10000); sb.query(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("checkStatus", 1)) .filter(QueryBuilders.termsQuery("DM", storeCodes)) ); sb.fetchSource(sourceContext); sb.sort("id", SortOrder.ASC); SearchRequest searchRequest = new SearchRequest(new String[]{originalNormRe.getAliase(yf)}, sb); SearchResponse searchResponse = originalNormRe.getEsClient().search(searchRequest, RequestOptions.DEFAULT); SearchHit[] searchHits = searchResponse.getHits().getHits(); long totalHits = searchResponse.getHits().totalHits; logger.info("Total Hits --->" + totalHits); int count = 0; while (searchHits != null && searchHits.length > 0) { // 批量修改original_norm_(规范明细数据) BulkRequest request = new BulkRequest(); for (SearchHit hit : searchHits) { objects = hit.getSortValues(); Map<String, Object> map = hit.getSourceAsMap(); String id = (String) map.get("id"); Double xse = Common.getDouble(map.get("XSE")); XContentBuilder xcb = XContentFactory.jsonBuilder().startObject() .field("countryUpXse", Common.mul(xse, upCoefficient, 2)) .field("countryUpXseUpdate", Common.mul(xse, upCoefficient, 2)) .endObject(); request.add(new UpdateRequest(hit.getIndex(), "data", id) .doc(xcb)); } request.setRefreshPolicy(RefreshPolicy.IMMEDIATE); request.timeout(TimeValue.timeValueMinutes(2)); request.timeout("10m"); BulkResponse bulkResponse = originalNormRe.getEsClient().bulk(request, RequestOptions.DEFAULT); if (bulkResponse.hasFailures()) { logger.error(bulkResponse.buildFailureMessage()); } else { logger.info("=========================================OK========================================="); } logger.info(String.valueOf(count += searchHits.length)); sb.searchAfter(objects); searchResponse = originalNormRe.getEsClient().search(searchRequest, RequestOptions.DEFAULT); searchHits = searchResponse.getHits().getHits(); } } catch (Exception e) { logger.error(e.getMessage(), e); }