es5.1.1 修改索引数据

# 环境描述
使用es版本为:5.1.1
使用client为:TransportClient client,不是highLevelClient



# 1. 知道当前数据rowid时
UpdateRequest updateRequest = new UpdateRequest("index-2021.08.16","type","rowid")
                .doc(Map.of("agent","aa"));
updateRequest.docAsUpsert(true);
client.update(updateRequest);


# 2. 知道当前数据rowid时 (第二种)
// 创建更新对象
        UpdateRequest updateRequest = new UpdateRequest(index,type,id);
        // 创建一个indexRequest对象指定索引名,类型,和文档id
        IndexRequest indexRequest = new IndexRequest(index,type,id);
        // 将需要修改的字段放进source里面,这里的ma可以是map,json,string,以及xtContentBuilder对象
        indexRequest.source(data);
        // 创建一个updateRequest对象指定索引名,类型,id,然后将indexRequest传入upsert方法中
        updateRequest.upsert(indexRequest);
        // 再将indexRequest放入doc中,猜测实际做了两次修改
        updateRequest.doc(indexRequest);
        // 最后发送请求
        try {
            UpdateResponse updateResponse= client.update(updateRequest).get();
            return updateResponse.status().getStatus();
        } catch (Exception e) {
            e.printStackTrace();
        }


# 3. 不知道当前数据rowid时
// 创建更新对象
        UpdateRequest updateRequest = new UpdateRequest(index,type,id);
        // 创建一个indexRequest对象指定索引名,类型,和文档id
        IndexRequest indexRequest = new IndexRequest(index,type,id);
        // 将需要修改的字段放进source里面,这里的ma可以是map,json,string,以及xtContentBuilder对象
        indexRequest.source(data);
        // 创建一个updateRequest对象指定索引名,类型,id,然后将indexRequest传入upsert方法中
        updateRequest.upsert(indexRequest);
        // 再将indexRequest放入doc中,猜测实际做了两次修改
        updateRequest.doc(indexRequest);
        // 最后发送请求
        try {
            UpdateResponse updateResponse= client.update(updateRequest).get();
            return updateResponse.status().getStatus();
        } catch (Exception e) {
            e.printStackTrace();
        }


# 4. 不知道当前数据rowid时 (第二种,highLevelClient时使用)
UpdateByQueryRequest request = new UpdateByQueryRequest();
        request.setDocTypes("student");
        request.setQuery(new TermQueryBuilder("name","黑骑一户"));
        client.updateByQuery(request);


# 5. 当不知道rowid时 (实时刷新更新数据的)
BulkIndexByScrollResponse bulkIndexByScrollResponse = UpdateByQueryAction.INSTANCE.newRequestBuilder(client)    // 创建新的请求builder
                .source(index)    // 设置索引index
                .filter(QueryBuilders.termQuery("msg", alertId))    // 设置过滤条件
                .script(new Script(ScriptType.INLINE, "painless", "ctx._source.msg='cc'", Collections.EMPTY_MAP))    // 设置批量修改脚本
                .abortOnVersionConflict(false)    // 设置ES版本导致问题失败是否停止运行
                .get();// 执行请求命令

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值