Elasticsearch Client 初始化方式以及清除數據方式

public class IndexManager {


    private static Object lock = new Object();
    
    private static TransportClient client;
    
    private static Log LOG = LogFactory.getLog(IndexManager.class);
    
    /**
     * @return
     */
    public static TransportClient getClient() {
        if (client == null) {
            synchronized (lock) {
                Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", ConfigObject.getInstance().getClusterName()).build();
                client = new TransportClient(settings);
                for (ConfigObject.ESNodeObject nodeObject : ConfigObject.getInstance().getEsNodeObjects()) {
                    client.addTransportAddress(new InetSocketTransportAddress(nodeObject.getAddress(), nodeObject.getPort()));
                }
            }
        }
        return client;
    }
    
    /**
     * 保存數據
     * 判斷文檔信息是否存在,如不存在,直接寫入
     * 如果存在,判斷當前文檔信息的contentMD5於索引內的MD5是否一致,如不一致重新寫入,如一致,直接返回
     * @param source
     * @return 返回id
     */
    public static String saveIndexData(AbstractDocument document) {
        if (document == null) {
            throw new IllegalArgumentException();
        }
        
        String contentMD5 = MD5Util.MD5(document.getContent());
        
        SearchResponse searchResponse = getClient().prepareSearch(Constants.ES_INDEX).setTypes(Constants.ES_DATAINFO_TYPE).setQuery(QueryBuilders.termQuery("url", document.getUrl())).execute().actionGet();
        if (searchResponse == null || searchResponse.getHits() == null || searchResponse.getHits().getTotalHits() <=0) {
            //id不存在,寫入新數據
            document.setId(IdBuilder.uuid());
            document.setContentMD5(contentMD5);
            document.setNewUpdatable(YN.Yes);
            document.setInsertTime(new Date());
            document.setUpdateTime(new Date());
            getClient().prepareIndex(Constants.ES_INDEX, Constants.ES_DATAINFO_TYPE).setId(document.getId()).setSource(document.toString()).execute().actionGet();
            if (LOG.isInfoEnabled()) {
                LOG.info("Create Index " + document.getId() + " ok & The url is " + document.getUrl());
            }
            return document.getId();
        }else{
            //有可能更新數據 
            SearchHit searchHit = searchResponse.getHits().getAt(0);
            String oldContentMD5 = (String)searchHit.getSource().get("contentMD5");
            if (oldContentMD5 == null || oldContentMD5.equals("") || !oldContentMD5.equals(contentMD5)) {
                //清除掉相同url的數據
                String id = IdBuilder.uuid();
                getClient().prepareDeleteByQuery(Constants.ES_INDEX).setTypes(Constants.ES_DATAINFO_TYPE).setQuery(QueryBuilders.termQuery("url", document.getUrl())).execute().actionGet();
                document.setId(id);
                getClient().prepareIndex(Constants.ES_INDEX, Constants.ES_DATAINFO_TYPE).setId(id).setSource(document.toString()).execute().actionGet();
                if (LOG.isInfoEnabled()) {
                    LOG.info("ReCreate Index " + id + " ok & The url is " + document.getUrl());
                }
                return id;
            }
            if (LOG.isInfoEnabled()) {
                LOG.info("Exist Index " + searchResponse.getHits().getAt(0).getId() + " ok & The url is " + document.getUrl());
            }
            return searchResponse.getHits().getAt(0).getId();
        }
    }
    
    /**
     * 清除ES_INDEX下的所有數據 
     */
    public static void clearAllDatas() {
        getClient().prepareDeleteByQuery(Constants.ES_INDEX).setTypes(Constants.ES_DATAINFO_TYPE).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
    }



轉:http://www.cnblogs.com/soltex/p/3466714.html

發佈了22 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章