elasticsearch 根据主键_id更新部分字段

package com.better517na.ebookingbusiservice.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.better517na.component.security.utils.GsonUtil;
import com.better517na.ebookingbusiservice.model.contracthotel.ContractHotelHitVo;
import com.better517na.ebookingbusiservice.util.LogUtils;
import com.better517na.component.security.utils.StringUtil;
import com.google.common.reflect.TypeToken;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
* @author lulin
* @project: ebookingbusiservice
* @description EsHelper
* @date 2021/09/24 11:17
**/
@Component
public class EsHelper {

private static RestHighLevelClient restClient;

@Resource(name = "contractHotelRestClient")
public void setProductRestClient(RestHighLevelClient restClient) {
EsHelper.restClient = restClient;
}


/**
* 查询
*
* @param contractNo
* @param hotelId
* @return
* @throws IOException
*/
public static List<ContractHotelHitVo> queryContractHotelList(String contractNo, String hotelId) throws IOException {
if (StringUtil.isEmptyOrNull(contractNo) || StringUtil.isEmptyOrNull(hotelId)) {
return new ArrayList<>();
}

int from = 0;
int size = 200;
int totalHits = 0;
List<ContractHotelHitVo> resp = new ArrayList<>();
String index = "contract_hotel";
if (StringUtil.isEmptyOrNull(index)) {
return new ArrayList<>();
}
SearchRequest request = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(from);
sourceBuilder.size(size);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
boolBuilder.must(QueryBuilders.termQuery("isDelete", 0));
boolBuilder.must(QueryBuilders.matchQuery("contactno", contractNo).operator(Operator.AND));
boolBuilder.must(QueryBuilders.matchQuery("hotelIDs", hotelId));

sourceBuilder.query(boolBuilder);
request.source(sourceBuilder);
SearchResponse search = restClient.search(request, RequestOptions.DEFAULT);
if (search.status() != RestStatus.OK || search.getHits().getTotalHits().value <= 0) {
return new ArrayList<>();
}
totalHits = (int) search.getHits().getTotalHits().value;
List<ContractHotelHitVo> collect = Arrays.stream(search.getHits().getHits()).map(p -> {
ContractHotelHitVo contractHotelHitVo = JSON.parseObject(JSON.toJSONString(p.getSourceAsMap()), ContractHotelHitVo.class);
contractHotelHitVo.setEsId(p.getId());
return contractHotelHitVo;
}).collect(Collectors.toList());

resp.addAll(collect);
from += size;
while (from < totalHits) {
sourceBuilder.from(from);
SearchResponse response1 = restClient.search(request, RequestOptions.DEFAULT);
if (response1 == null || response1.status() != RestStatus.OK || response1.getHits().getTotalHits().value < 0) {
return resp;
}
List<ContractHotelHitVo> resp1 = Arrays.stream(response1.getHits().getHits()).map(p -> {
ContractHotelHitVo contractHotelHitVo = JSON.parseObject(JSON.toJSONString(p.getSourceAsMap()), ContractHotelHitVo.class);
contractHotelHitVo.setEsId(p.getId());
return contractHotelHitVo;
}).collect(Collectors.toList());
resp.addAll(resp1);
from += size;
}
return resp;
}


/**
* 批量更新酒店产品
*
* @param
* @return
*/
public static boolean updateHotelProdects(ContractHotelHitVo contractHotelHitVo) {
if (contractHotelHitVo == null) {
return true;
}

try {
String index = "contract_hotel";
UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest("_all");
UpdateRequest updateRequest = new UpdateRequest(index, contractHotelHitVo.getEsId()).doc(jsonBuilder().startObject().field("data", contractHotelHitVo.getData())
.field("modifyStaff", contractHotelHitVo.getModifyStaff())
.field("modifyStaffName", contractHotelHitVo.getModifyStaffName())
.field("modifyTime", contractHotelHitVo.getModifyTime())
.endObject());
UpdateResponse updateResponse = restClient.update(updateRequest, RequestOptions.DEFAULT);

if (updateResponse.getResult().toString().equals("UPDATED") || updateResponse.getResult().toString().equals("NOOP")) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
LogUtils.writeExceptionLog(e, "updateHotelPriceInfos");
return false;
}

return false;
}
}
原文地址:https://www.cnblogs.com/lstcJaney/p/15353442.html