Elasticsearch 增删改操作

定义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);
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值