ES中刪除指定記錄中的一個(gè)字段
需求描述
- 項(xiàng)目中的所有客戶信息都保存在ES中,由于以前的接口調(diào)用鏈太深,修改起來,你懂的,所以決定重新寫一個(gè)刪除字段的方法
- 方法盡量通用
實(shí)現(xiàn)
- 依賴
<dependency>
<groupId>dx.commons</groupId>
<artifactId>dx-commons-elasticsearch</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>5.6.3</version>
</dependency>```
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
}
}
]
}
} ```
- 實(shí)現(xiàn)類
package com.dx.asst.customer.service;
import java.io.IOException;
import org.apache.http.util.Asserts;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.script.Script;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dx.asst.comm.constants.EsIndexConstants;
import lombok.extern.slf4j.Slf4j;
/**
* @description :
* @author : zhubing.ji
* @date : 2018/6/14 下午4:44
*/
@Slf4j
@Service
public class CustomerOperationEsServiceTest {
private static final String DEFAULT_TYPE = "default";
@Autowired
private RestHighLevelClient restHighLevelClient;
//說明,這個(gè)remove里面如果沒有單引號(hào)的話,會(huì)報(bào)錯(cuò)
private final String updateCustomerScript = "ctx._source.remove('%s')";
/**
*
* @param customerId 客戶記錄中的ID
* @param keys 要?jiǎng)h除的字段集合
*/
public void deleteByAttrs(Long customerId, Iterable<String> keys) {
Asserts.notNull(customerId, "customerId is null");
Asserts.notNull(keys, "keys is null");
UpdateRequest updateRequest = new UpdateRequest(EsIndexConstants.CUSTOMER, "default", customerId.toString());
keys.forEach(key -> {
String updateScript = String.format(updateCustomerScript, key);
updateRequest.script(new Script(updateScript));
try {
restHighLevelClient.update(updateRequest);
} catch (IOException e) {
log.error("when delete customer attr error", e);
throw new RuntimeException("when releaseBindUser error", e);
}
});
}
}
-
說明
如果想刪除JSON中的custGroupId和crmCustomerId,調(diào)用方法的參數(shù)只要按照以下方式傳遞就可以了List<String> keys = Lists.newArrayListWithCapacity(2); keys.add("bindUser"); keys.add("crmCustomerId"); customerClientService.deleteAttrsById(customerId, keys);