es批量修改数组动态增加并去重

 

es创建测试的index和type(对应mysql的数据库和表)

PUT /uniq_test_idx1
{
  "mappings": {
    "uniq_test": {
      "properties": {
        "comId": {
          "type": "long"
        },
        "customerName": {
          "type": "keyword",
          "index": false
        }
      }
    }
  }
}

测试详情:

我会准备两条数据,comId和customerName均不一致。当我执行了批量修改后,要达到的效果,只入一条数据,comId为两条数据集合,customerName以后面的数据为准。

java中的测试代码如下:

@Data
    private static class SimpleEsModel {
        private String customerPhone;
        private String customerName;
        private Long[] comId;

        public SimpleEsModel(String customerPhone, String customerName, Long[] comId) {
            this.customerPhone = customerPhone;
            this.customerName = customerName;
            this.comId = comId;
        }
    }

    @Test
    public void test1() {
        List<SimpleEsModel> clueList = Lists.newArrayList();
        SimpleEsModel modal1 = new SimpleEsModel("133", "wang1", new Long[]{12L});
        SimpleEsModel modal2 = new SimpleEsModel("133", "wang2", new Long[]{13L});
        clueList.add(modal1);
        clueList.add(modal2);
        EsResponse esResponse = bulkUpsert(clueList);
        System.out.println(JacksonUtil.serialize(esResponse));
    }

    public EsResponse bulkUpsert(List<SimpleEsModel> vars) {
        if (CollectionUtils.isEmpty(vars)) {
            return EsResponse.success();
        }
        long start = System.currentTimeMillis();
        try {
            BulkRequest bulkRequest = new BulkRequest();
            for (SimpleEsModel var : vars) {
                String customerPhone = var.getCustomerPhone();

                String index = "uniq_test_idx1";
                String type = "uniq_test";
                Script script = generateScript(var);
                
                UpdateRequest updateRequest = new UpdateRequest(index, type, customerPhone)
                        .script(script)
                        .upsert(BeanMap.create(var));
                bulkRequest.add(updateRequest);
            }
            BulkResponse bulkResponse = esOperationService.bulk(bulkRequest);
            if (bulkResponse.hasFailures()) {
                String failureMessage = bulkResponse.buildFailureMessage();
                FkMonitor.recordOne("datacenter_es_bulkIndex_exception");
                return EsResponse.fail();
            }
            return EsResponse.success();
        } catch (Exception e) {
            return EsResponse.fail();
        } finally {
            FkMonitor.recordOne("datacenter_es_bulkIndex_total", System.currentTimeMillis() - start);
        }
    }

    public Script generateScript(SimpleEsModel simpleModel) {
        String scriptStr = "def comIdTargets = ctx._source.comId.findAll(oneComId -> oneComId == params.comId);\n" +
                "if(comIdTargets.size() == 0){\n" +
                "   ctx._source.comId.add(params.comId[0])\n" +
                "}" +
                "if(params.customerName!=null){ctx._source.customerName=params.customerName}\n";
        return new Script(ScriptType.INLINE, "painless", scriptStr, BeanMap.create(simpleModel));
    }

这段代码的核心是脚本的使用。

动态添加去重后comId的实现关键在于:传入的参数的comId类型为数组。

至于脚本使用的espainless脚本,关于这个脚本的语法规则我没有在网上找到过多的资料。

如果您有相关的资料,欢迎留言

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值