ElasticSearch: 페이지 나누기 질의(jest 작업)
35388 단어 ElasticsearchSpringBoot 학습 요약
@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;
}
}
@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;
}
}
@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;
}
}
@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);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Embulk를 사용하여 ElasticCloud로 보내기Embulk에서 ElasticCloud에 보낼 수 있을까라고 생각비망록도 겸해 기술을 남깁니다 Embulk 설치 ElasticCloud (14 일 체험판) brew라면 아래 명령 입력 파일 만들기 파일 내용 seed...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.