ES中刪除指定記錄的某個(gè)字段(field)

ES中刪除指定記錄中的一個(gè)字段

需求描述

  1. 項(xiàng)目中的所有客戶信息都保存在ES中,由于以前的接口調(diào)用鏈太深,修改起來,你懂的,所以決定重新寫一個(gè)刪除字段的方法
  2. 方法盡量通用

實(shí)現(xiàn)

  1. 依賴
        <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
            }
        }
    ]
}

} ```

  1. 實(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);
                }
            });
        }
        }

  1. 說明
    如果想刪除JSON中的custGroupId和crmCustomerId,調(diào)用方法的參數(shù)只要按照以下方式傳遞就可以了

           List<String> keys = Lists.newArrayListWithCapacity(2); 
           keys.add("bindUser");
           keys.add("crmCustomerId");
           customerClientService.deleteAttrsById(customerId, keys);
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容