ES中删除指定记录中的一个字段
需求描述
项目中的所有客户信息都保存在ES中,由于以前的接口调用链太深,修改起来,你懂的,所以决定重新写一个删除字段的方法
方法尽量通用
实现
依赖
dx-commons-elasticsearch
1.0.0
elasticsearch
5.6.3
transport
5.6.3
elasticsearch-rest-high-level-client
5.6.3
```
2. JSON
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "asst_customers_v1",
"_type": "default",
"_id": "1000212",
"_score": 1,
"_source": {
"createTime": 1513059302000,
"updateTime": 1513058943000,
"customerGroupId": 6,
"crmCustomerId": ccc,
"sourcePlatform": "schoki",
"custGroupId": -6,
"followWechatOfficialAccountFlag": true
}
}
]
}
} ```
实现类
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .slf4j.Slf4j;
/**
* @description :
* @author :
* @date : 2018/6/14 下午4:44
*/
@Slf4j
@Service
public class CustomerOperationEsServiceTest {
private static final String DEFAULT_TYPE = "default";
@Autowired
private RestHighLevelClient restHighLevelClient;
//说明,这个remove里面如果没有单引号的话,会报错
private final String updateCustomerScript = "ctx._source.remove('%s')";
/**
*
* @param customerId 客户记录中的ID
* @param keys 要删除的字段集合
*/
public void deleteByAttrs(Long customerId, Iterable keys) {
(customerId, "customerId is null");
(keys, "keys is null");
UpdateRequest updateRequest = new UpdateRequest(, "default", ());
(key -> {
String updateScript = (updateCustomerScript, key);
(new Script(updateScript));
try {
(updateRequest);
} catch (IOException e) {
("when delete customer attr error", e);
throw new RuntimeException("when releaseBindUser error", e);
}
});
}
}
说明
如果想删除JSON中的custGroupId和crmCustomerId,调用方法的参数只要按照以下方式传递就可以了
List keys = (2);
("bindUser");
("crmCustomerId");
(customerId, keys);