溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ElasticSearch如何實(shí)現(xiàn)查詢所有數(shù)據(jù)

發(fā)布時(shí)間:2021-12-16 10:26:04 來源:億速云 閱讀:1228 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“ElasticSearch如何實(shí)現(xiàn)查詢所有數(shù)據(jù)”,在日常操作中,相信很多人在ElasticSearch如何實(shí)現(xiàn)查詢所有數(shù)據(jù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”ElasticSearch如何實(shí)現(xiàn)查詢所有數(shù)據(jù)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

1,分頁查詢所有,但最多能查10000 條記錄,超過就會報(bào)錯(cuò)

 public <T>List<T> getAll(String index, String type, QueryBuilder queryBuilder, Class<T> tClass)  {
        SearchResponse searchResponse = buildRequest(index, type, queryBuilder, 0).get();
        SearchHits hits = searchResponse.getHits();
        int total = (int) hits.totalHits;

        List<T> result = new ArrayList<>(total);
        addToResult(hits,result,tClass);

        int pagesize = 1000;
        int page = total / pagesize;
        if(total % pagesize != 0){
            page++;
        }
        for (int i = 2; i <= page; i++) {
            int from = (i - 1) * pagesize;
            searchResponse = buildRequest(index, type, queryBuilder, from).get();
            hits = searchResponse.getHits();
            addToResult(hits,result,tClass);
        }
        return result;
    }




    private SearchRequestBuilder buildRequest(String index,String type,QueryBuilder queryBuilder,int from) {
        TransportClient client = elasticSearchManager.getClient("ad");
        int pagesize = 1000;
        SearchRequestBuilder builder = client
                .prepareSearch(index)
                .setTypes(type)
                .setFrom(from)
                .setSize(pagesize)
                .setQuery(queryBuilder);
        log.info("ElasticsearchDAO query string:\nGET {}/{}/_search\n{} ", index, type, builder.toString());
        return builder;
    }

2,利用scroll,這是官方推薦的方式

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Slf4j
@Component
public class ElasticsearchDAO {

    @Autowired
    private ElasticSearchManager elasticSearchManager;

    private int pagesize = 10000;

    /**
     * es 游標(biāo)有效期
     */
    private final int ES_SCROLL_TIME = 10;

    public List<ReportDO> search(SearchRequest req) throws ExecutionException, InterruptedException {
        BoolQueryBuilder boolQuery = req.getQueryBuilder();

        List<ReportDO> result = getAll(Config.ES_REPORT_INDEX, Config.ES_REPORT_TYPE, boolQuery,ReportDO.class);
        return result;
    }

    /**
     * 查詢所有
     */
    private <T>List<T> getAll(String index, String type, QueryBuilder queryBuilder, Class<T> tClass) throws ExecutionException, InterruptedException {
        SearchResponse searchResponse = buildRequest(index, type, queryBuilder).get();
        String scrollId = searchResponse.getScrollId();
        SearchHits hits = searchResponse.getHits();
        List<T> result = new ArrayList<>();
        addToResult(hits,result,tClass);
        while (true){
            List<T> list = getScrollResult(scrollId, tClass, result);
            if(list == null){
                break;
            }
        }
        return result;

    }
    private  <T>List<T>  getScrollResult(String scrollId,Class<T> tClass, List<T> result) throws ExecutionException, InterruptedException {
        TransportClient client = elasticSearchManager.getClient("ad");
        SearchResponse searchResponse = client.prepareSearchScroll(scrollId)  //設(shè)置游標(biāo)
                .setScroll(TimeValue.timeValueSeconds(ES_SCROLL_TIME))  //設(shè)置游標(biāo)有效期
                .execute()
                .get();
        SearchHits hits = searchResponse.getHits();
        SearchHit[] searchHits = hits.getHits();
        if(searchHits == null || searchHits.length == 0){
            return null;
        }
        addToResult(hits,result,tClass);

        return result;
    }

    private SearchRequestBuilder buildRequest(String index,String type,QueryBuilder queryBuilder) {
        TransportClient client = elasticSearchManager.getClient("ad");
        SearchRequestBuilder builder = client
                .prepareSearch(index)
                .setTypes(type)
                .setScroll(TimeValue.timeValueSeconds(ES_SCROLL_TIME))  //設(shè)置游標(biāo)有效期
                .setSize(pagesize)
                .setQuery(queryBuilder);
        log.info("ElasticsearchDAO query string:\nGET {}/{}/_search\n{} ", index, type, builder.toString());
        return builder;
    }


    private void addToResult(SearchHits hits,List result,Class tClass){
        for (SearchHit hit : hits) {
            String sourceAsString = hit.getSourceAsString();
            Object object = JsonUtil.parseObject(sourceAsString, tClass);
            result.add(object);
        }
    }


}

到此,關(guān)于“ElasticSearch如何實(shí)現(xiàn)查詢所有數(shù)據(jù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI