es批量修改数组动态增加并去重
es创建测试的index和type(对应mysql的数据库和表)
PUT /uniq_test_idx1
性冷淡 英语
{
"mappings": {
"uniq_test": {
over night"properties": {
"comId": {
"type": "long"
},
"customerName": {
"type": "keyword",
六年级英语上册"index": fal
}
}
}
}
}
测试详情:
我会准备两条数据,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;
}
}
@Test
public void test1() {damon
List<SimpleEsModel> clueList = wArrayList();
SimpleEsModel modal1 = new SimpleEsModel("133", "wang1", new Long[]{12L});
SimpleEsModel modal2 = new SimpleEsModel("133", "wang2", new Long[]{13L});
clueList.add(modal1);
clueList.add(modal2);
EsRespon esRespon = bulkUprt(clueList);
System.out.println(JacksonUtil.rialize(esRespon));
}
public EsRespon bulkUprt(List<SimpleEsModel> vars) {
if (CollectionUtils.isEmpty(vars)) {
return EsRespon.success();
}
long start = System.currentTimeMillis();
try {
BulkRequest bulkRequest = new BulkRequest();
for (SimpleEsModel var : vars) {
String customerPhone = CustomerPhone();
String customerPhone = CustomerPhone();
String index = "uniq_test_idx1";
String type = "uniq_test";filesize
Script script = generateScript(var);sheeting
UpdateRequest updateRequest = new UpdateRequest(index, type, customerPhone)
.script(script)
.ate(var));
bulkRequest.add(updateRequest);
}
mediumheightBulkRespon bulkRespon = esOperationService.bulk(bulkRequest);
if (bulkRespon.hasFailures()) {
String failureMessage = bulkRespon.buildFailureMessage();
return EsRespon.fail();
}
return EsRespon.success();
} catch (Exception e) {
return EsRespon.fail();
} finally {
假期英文}
}
public Script generateScript(SimpleEsModel simpleModel) {
String scriptStr = "def comIdTargets = ctx._Id.findAll(oneComId -> oneComId == Id);\n" + "if(comIdTargets.size() == 0){\n" +nfdm
" ctx._Id.Id[0])\n" +
"}" +
"if(params.customerName!=null){ctx._source.customerName=params.customerName}\n";
return new Script(ScriptType.INLINE, "painless", scriptStr, ate(simpleModel));
}
这段代码的核⼼是脚本的使⽤。
动态添加去重后comId的实现关键在于:传⼊的参数的comId类型为数组。
⾄于脚本使⽤的espainless脚本,关于这个脚本的语法规则我没有在⽹上找到过多的资料。
escape velocity如果您有相关的资料,欢迎留⾔