这篇文章主要介绍“ElasticSearch如何实现查询所有数据”,在日常操作中,相信很多人在ElasticSearch如何实现查询所有数据问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ElasticSearch如何实现查询所有数据”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
站在用户的角度思考问题,与客户深入沟通,找到九江网站设计与九江网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、成都做网站、企业官网、英文网站、手机端网站、网站推广、域名注册、网站空间、企业邮箱。业务覆盖九江地区。
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; }
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 游标有效期 */ 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) //设置游标 .setScroll(TimeValue.timeValueSeconds(ES_SCROLL_TIME)) //设置游标有效期 .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)) //设置游标有效期 .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); } } }
到此,关于“ElasticSearch如何实现查询所有数据”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
网页标题:ElasticSearch如何实现查询所有数据
文章地址:https://www.cdcxhl.com/article10/ggjpgo.html
成都网站建设公司_创新互联,为您提供Google、网站内链、网站改版、电子商务、建站公司、商城网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联