ElasticSearch: 페이지 나누기 질의(jest 작업)

두 가지 방법: 깊이 페이지와 얕은 페이지
  • 페이지 분류
  • @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    public class Page<T> {
        private int pageSize = 10; // 
        private int totalCount; // 
        private int start; // 
        private int pageNo;// 
        private int totalPages; // 
        private List<T> pageList;// 
    
        public Page(int totalCount) {
            this.totalCount = totalCount;
        }
    
        /**
         *  
         */
        public int getCurrentPageNo() {
            return start / pageSize + 1;
        }
    
        /**
         *  
         *
         * @return
         */
        public boolean getHasNextPage() {
            return getCurrentPageNo() < totalPages;
        }
    
        /**
         *  1
         *
         * @return
         */
        public boolean getHasPavPage() {
            return getCurrentPageNo() > 1;
        }
    
        /**
         *  
         *
         * @return
         */
        public int getTotalPages() {
            totalPages = totalCount / pageSize;
    
            if (totalCount % pageSize != 0) {
                totalPages++;
            }
    
            return totalPages;
        }
    
        /**
         *  
         *
         * @param pageNo
         * @return
         */
        public int getStart(int pageNo) {
            if (pageNo < 1) {
                pageNo = 1;
            } else if (getTotalPages() > 0 && pageNo > getTotalPages()) {
                pageNo = getTotalPages();
            }
    
            start = (pageNo - 1) * pageSize;
            return start;
        }
    }
    
  • dao
  • @Slf4j
    @Repository
    public class RecordDetailsDao extends JestBaseDao {
    
    
        /**
         *  
         *
         * @return
         */
        public SearchResult listRecordDetails(@NotBlank String esJson,
                                              @NotBlank String serviceName) {
            if (StringUtils.contains(serviceName, "_")) {
                int index = StringUtils.indexOf(serviceName, "_");
                String indexName = StringUtils.substring(serviceName, 0, index);
                String indexAllName = indexName.concat("*");
                return this.queryResByJsonStr(esJson, indexAllName, JestConsts.Index.AUTHS.getIndexType());
            }
            return null;
        }
    }
    
  • service
  • @Slf4j
    @Service
    public class RecordDetailsServiceImpl implements RecordDetailsService {
    
        private final RecordDetailsDao recordDetailsDao;
    
        @Autowired
        public RecordDetailsServiceImpl(RecordDetailsDao recordDetailsDao) {
            this.recordDetailsDao = recordDetailsDao;
        }
    
    
        @Override
        public Page<AllCentre> listRecordDetails(RecordDetails recordDetails) {
    
            SearchResult searchResult = this.searchResult(recordDetails);
            if (Objects.equals(searchResult, null)) {
                return new Page<AllCentre>().setPageList(Collections.emptyList());
            }
            // es 
            List<AllCentre> allCentreList = searchResult.getSourceAsObjectList(AllCentre.class, true);
            // 
            Long valueCount = searchResult.getAggregations().getValueCountAggregation(JestConsts.AggName.SERVICE_NAME_COUNT).getValueCount();
            // 
            return new Page<AllCentre>(valueCount.intValue()).setPageList(allCentreList);
        }
    
        @Override
        public List<SubCode> listSubCode(RecordDetails recordDetails) {
            SearchSourceBuilder source = SearchSourceBuilder.searchSource();
            BoolQueryBuilder bool = this.getBoolQuery(recordDetails);
            TermsAggregationBuilder termsAggs = AggregationBuilders.terms(JestConsts.AggName.SUB_CODE_GROUP).field(JestConsts.Keywords.SUB_CODE).size(100);
            String esJson = source.size(0).query(bool).aggregation(termsAggs).toString();
            log.info("{}", esJson);
            SearchResult searchResult = recordDetailsDao.listRecordDetails(esJson, recordDetails.getTacheCode());
            if (Objects.equals(searchResult, null)) {
                return Collections.emptyList();
            }
            List<TermsAggregation.Entry> entryList = searchResult.getAggregations().getTermsAggregation(JestConsts.AggName.SUB_CODE_GROUP).getBuckets();
            return entryList.stream().map(entry -> SubCode.builder().statusCode(entry.getKeyAsString()).statusName(entry.getKeyAsString()).build()).collect(Collectors.toList());
        }
    
        /**
         *  jest SearchResult
         *
         * @param recordDetails
         * @return
         */
        @NotNull
        private SearchResult searchResult(RecordDetails recordDetails) {
            SearchSourceBuilder source = SearchSourceBuilder.searchSource();
            BoolQueryBuilder bool = this.getBoolQuery(recordDetails);
            if (StringUtils.isNotBlank(recordDetails.getSubCode())) {
                bool.filter(QueryBuilders.termQuery(JestConsts.Keywords.SUB_CODE, recordDetails.getSubCode()));
            }
            ValueCountAggregationBuilder count = AggregationBuilders
                    .count(JestConsts.AggName.SERVICE_NAME_COUNT)
                    .field(JestConsts.Keywords.SERVICE_NAME);
            String esJson = source.from(recordDetails.getStart()).size(recordDetails.getLength()).query(bool).aggregation(count).toString();
            log.info("{}", esJson);
            return recordDetailsDao.listRecordDetails(esJson, recordDetails.getTacheCode());
        }
    
        /**
         * @param recordDetails
         * @return
         */
        private BoolQueryBuilder getBoolQuery(RecordDetails recordDetails) {
            Long beginTime = LocalDateTimeUtils.getTimeStampByDateTime(recordDetails.getBeginTime());
            Long endTime = LocalDateTimeUtils.getTimeStampByDateTime(recordDetails.getEndTime());
            BoolQueryBuilder bool = QueryBuilders.boolQuery();
            bool.filter(QueryBuilders.rangeQuery(JestConsts.Keywords.LOG_TIME_LONG)
                    .gte(beginTime)
                    .lte(endTime));
            bool.filter(QueryBuilders.termQuery(JestConsts.Keywords.SERVICE_NAME, recordDetails.getTacheCode()));
            bool.filter(QueryBuilders.termQuery(JestConsts.Keywords.MSG_IS_JSON, 1));
            bool.mustNot(QueryBuilders.termQuery(JestConsts.Keywords.SUB_CODE, "0000"));
            return bool;
        }
    
    }
    
  • controller
  •     @PostMapping(value = "/recordData", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
        @ApiOperation(value = " ")
        public AbstractApiResult listRecordDetails(@Validated RecordDetails recordDetails) {
            Page<AllCentre> objectPage = recordDetailsService.listRecordDetails(recordDetails);
            return AbstractApiResult.success(objectPage);
        }
    

    좋은 웹페이지 즐겨찾기