定义ES索引的名称
private static final String NGSOC_INTELLIGENCE_REPORT = "es_index_name";//索引名称
ES新增操作
/**
* 新增
* @param param
* @return
*/
@Override
public ResultDO<String> addData(IntelligenceReportDO param) {
try{
RestHighLevelClient client = ESUtil.getRestHighLevelClient();
IndexRequest request = new IndexRequest(NGSOC_INTELLIGENCE_REPORT);
request.source(JSON.toJSONString(param), XContentType.JSON);
request.timeout(TimeValue.timeValueMinutes(10));
//WAIT_UNTIL 一直保持请求连接中,直接当所做的更改对于搜索查询可见时的刷新发生后,再将结果返回
request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
if (indexResponse != null){
return new ResultDO<>(MessageCode.EXECUTE_SUCCESS,indexResponse.getId());
}
return new ResultDO<>(MessageCode.SYSTEM_ERROR);
} catch (Exception e) {
log.error("method:addData, ret:KnowledgeLog error", e);
return new ResultDO<>(MessageCode.SYSTEM_ERROR);
}
}
ES修改操作
/**
* 修改
* @param param
* @return
*/
@Override
public ResultDO<Long> editData(IntelligenceReportDO param) {
try{
IntelligenceReportDO intelligenceReportDO = getObject(param.getId());
if (intelligenceReportDO == null ){
return new ResultDO<>(MessageCode.NOT_FOUND_DATA, null);
}
RestHighLevelClient client = ESUtil.getRestHighLevelClient();
BeanUtils.copyProperties(param,intelligenceReportDO);
UpdateRequest updateRequest = new UpdateRequest(NGSOC_INTELLIGENCE_REPORT,param.getId());
updateRequest.doc(JSON.toJSONString(intelligenceReportDO), XContentType.JSON);
updateRequest.timeout(TimeValue.timeValueMinutes(10));
//WAIT_UNTIL 一直保持请求连接中,直接当所做的更改对于搜索查询可见时的刷新发生后,再将结果返回
updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
client.update(updateRequest, RequestOptions.DEFAULT);
return new ResultDO<>(MessageCode.EXECUTE_SUCCESS);
} catch (Exception e) {
log.error("method:editData, ret:KnowledgeLog error", e);
return new ResultDO<>(MessageCode.SYSTEM_ERROR);
}
}
ES删除
/**
* 删除
* @param param
* @return
*/
@Override
public ResultDO<Long> delData(IntelligenceReportDO param) {
try{
IntelligenceReportDO intelligenceReportDO = getObject(param.getId());
if (intelligenceReportDO == null ){
return new ResultDO<>(MessageCode.NOT_FOUND_DATA, null);
}
RestHighLevelClient client = ESUtil.getRestHighLevelClient();
DeleteRequest deleteRequest = new DeleteRequest(NGSOC_INTELLIGENCE_REPORT,param.getId());
deleteRequest.timeout(TimeValue.timeValueMinutes(10));
//WAIT_UNTIL 一直保持请求连接中,直接当所做的更改对于搜索查询可见时的刷新发生后,再将结果返回
deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
client.delete(deleteRequest,RequestOptions.DEFAULT);
return new ResultDO<>(MessageCode.EXECUTE_SUCCESS);
} catch (Exception e) {
log.error("method:delData, ret:KnowledgeLog error", e);
return new ResultDO<>(MessageCode.SYSTEM_ERROR);
}
}