• Java 操作 ElasticSearch API 中的 update


    方式一:修改文档中的某些特定的字段值

    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);
    }
    updateSpecificFields
  • 相关阅读:
    java将一个或者多个空格进行分割
    Oracle decode()函数
    javascript 匿名函数和模块化
    javascript Math函数
    javascript 数组Array排序
    jQuery 获取屏幕高度、宽度
    fastJson 转换日期格式
    QNX Development Tools Based on Eclipse IDE
    Eclipse equinox implementation of OSGi
    Eclipse SWT
  • 原文地址:https://www.cnblogs.com/chaoyou/p/14612314.html
Copyright © 2020-2023  润新知