1. 程式人生 > >ElasticSearch5.4 基本增刪查改、搜尋模板類

ElasticSearch5.4 基本增刪查改、搜尋模板類

import lombok.Data;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilder;

import java.util.List;

/**
 * Created by T T on 2017/6/27.
 */
@Data
public class SearchTemplate{
    private Client client;

    /**
     * 批量建立文件,需指定索引和型別
     * @param index 索引
     * @param type  型別
     * @param docs  文件
     * @return
     */
    public BulkResponse createIndex(String index, String type, List docs){
        BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
        for(String doc : docs){
            bulkRequestBuilder.add(client.prepareIndex(index,type).setSource(doc));
        }
        BulkResponse bulkResponse = bulkRequestBuilder.get();
        return bulkResponse;
    }

    /**
     * 單個建立文件,需指定索引和型別
     * @param index 索引
     * @param type  型別
     * @param doc   文件
     * @return
     */
    public IndexResponse createIndex(String index, String type, String doc){
        IndexResponse indexResponse = client.prepareIndex(index, type).setSource(doc).get();
        return indexResponse;
    }

    /**
     * 指定索引、型別和搜尋型別進行搜尋,若型別為空預設對整個索引進行搜尋
     * @param index 索引
     * @param type  型別
     * @param queryBuilder  搜尋型別
     * @return
     */
    public SearchResponse search(String index, String type, QueryBuilder queryBuilder){
        if (type == null|| type.length() == 0){
            return client.prepareSearch(index).setQuery(queryBuilder).get();
        }
        return client.prepareSearch(index).setTypes(type).setQuery(queryBuilder).get();
    }

    /**
     * 根據id刪除指定索引、型別下的文件,id需先通過搜尋獲取
     * @param index 索引
     * @param type  型別
     * @param id    文件id
     * @return
     */
    public DeleteResponse deleteIndex(String index,String type,String id) {
        DeleteResponse deleteResponse= client.prepareDelete(index, type, id).get();
        return deleteResponse;
    }
    /**
     * 刪除指定索引,慎用
     * @param index 索引
     * @return
     */
    public DeleteIndexResponse deleteAll(String index){
        DeleteIndexResponse deleteIndexResponse = client.admin().indices().prepareDelete(index).get();
        return deleteIndexResponse;
    }

    /**
     * 根據index、type、id更新文件
     * @param index 索引
     * @param type  文件
     * @param id    文件id
     * @param newDoc 更新後的文件json字串
     * @return
     */
    public Boolean update(String index,String type,String id,String newDoc){
        UpdateResponse updateResponse = client.prepareUpdate(index,type,id).setDoc(newDoc).get();
        if(updateResponse.getResult()!= DocWriteResponse.Result.UPDATED){
            return false;
        }
        return true;
    }

    /**
     * 判斷指定Index是否存在
     * @param index
     * @return
     */
    public Boolean indexExist(String index){
        IndicesExistsRequest request = new IndicesExistsRequest(index);
        IndicesExistsResponse response = client.admin().indices().exists(request).actionGet();
        if (response.isExists()) {
            return true;
        }
        return false;
    }
}