elasticsearch 다중 필드 그룹 집합 페이지 조회 기능 구현
7695 단어 Elasticsearch
2. 시간월별 그룹 포함
3. 페이지 나누기 조회 지원
아래 코드를 붙여서 실현하면 참고만 할 수 있습니다. 좋은 방안이 있으면 메시지를 남겨 주십시오.
@Override
public Pager findCusMap(CustMapConditionVO vo, Pager pager) {
// getBoolQueryBuilder ,
BoolQueryBuilder b = getBoolQueryBuilder(vo);
if (pager.getTotal() == null || pager.getTotal() <= 0) {
DateHistogramAggregationBuilder dateHistogramInterval = AggregationBuilders.dateHistogram("lastEndDateInterval").field("lastEndDate").dateHistogramInterval(new DateHistogramInterval("1M"));
TermsAggregationBuilder provinceTermsBuilder = AggregationBuilders.terms("provinceName").field("provinceName")
.subAggregation(AggregationBuilders.terms("cityName").field("cityName").subAggregation(AggregationBuilders.terms("companyName").field("companyName")));
CardinalityAggregationBuilder builder12 = AggregationBuilders.cardinality("totalCount").field("companyName");
dateHistogramInterval.subAggregation(builder12);
SearchRequestBuilder builder1 = client.prepareSearch(CarInfoCenterES.INDEX).setTypes(CarInfoCenterES.TYPE).setQuery(b)
.addAggregation(dateHistogramInterval)
.addAggregation(provinceTermsBuilder);
Aggregations aggregations = builder1.get().getAggregations();
Histogram histogram = aggregations.get("lastEndDateInterval");
long sum = 0;
for (Histogram.Bucket entry : histogram.getBuckets()) {
Aggregations userNumAggregations = entry.getAggregations();
Cardinality cardinality = userNumAggregations.get("totalCount");
sum += cardinality.getValue();
/* Terms provinceTerms = entry.getAggregations().get("provinceName");
for (Terms.Bucket provinceBucket : provinceTerms.getBuckets()) {
sum +=provinceBucket.getDocCount();
}
Terms cityTerms = entry.getAggregations().get("cityName");
for (Terms.Bucket cityBucket : cityTerms.getBuckets()) {
sum +=cityBucket.getDocCount();
}
Terms comTerms = entry.getAggregations().get("cityName");
for (Terms.Bucket comBucket : comTerms.getBuckets()) {
sum +=comBucket.getDocCount();
}*/
}
pager.setTotal((int) sum);
}
DateHistogramAggregationBuilder aggregationBuilder = AggregationBuilders.dateHistogram("lastEndDateInterval").field("lastEndDate").dateHistogramInterval(new DateHistogramInterval("1M"));
TermsAggregationBuilder userNumTerms = AggregationBuilders.terms("provinceName").field("provinceName");
userNumTerms.subAggregation(AggregationBuilders.terms("cityName").field("cityName")
.subAggregation(AggregationBuilders.terms("companyName").field("companyName")));
int startIndex = (pager.getPageNum() - 1) * pager.getPageSize();
int endIndex = pager.getPageSize() * pager.getPageNum();
if (pager.getPageSize() != null || pager.getPageSize() > 0) {
userNumTerms.size(endIndex);
}
aggregationBuilder.subAggregation(userNumTerms);
SearchRequestBuilder builder = client.prepareSearch(CarInfoCenterES.INDEX).setTypes(CarInfoCenterES.TYPE).setQuery(b)
.addAggregation(aggregationBuilder);
builder.addSort("provinceName", SortOrder.ASC);
builder.addSort("cityName", SortOrder.ASC);
SearchResponse response = builder.get();
Histogram lastEndDateInterval = response.getAggregations().get("lastEndDateInterval");
List dataList = new ArrayList<>(0);
int i = 0;
for (Histogram.Bucket entry : lastEndDateInterval.getBuckets()) {
CustMapVO custMapVO = new CustMapVO();
String startTime = entry.getKeyAsString();
custMapVO.setYearMonth(startTime.substring(0, 7));
Aggregations userNumAggregations = entry.getAggregations();
Terms terms = userNumAggregations.get("provinceName");
for (Terms.Bucket proEntry : terms.getBuckets()) {
String provinceName = proEntry.getKeyAsString();
if(StringUtils.isBlank(provinceName)){
System.out.println("*************");
continue;
}
custMapVO.setProviceName(provinceName);
Aggregations cityAggregation = proEntry.getAggregations();
Terms cityTerms = cityAggregation.get("cityName");
for (Terms.Bucket cityEntry : cityTerms.getBuckets()) {
String cityName = cityEntry.getKeyAsString();
custMapVO.setCityName(cityName);
Aggregations comAggregation = cityEntry.getAggregations();
Terms comTerms = comAggregation.get("companyName");
for (Terms.Bucket comEntry : comTerms.getBuckets()) {
if (i++ < startIndex) {
continue;
}
if (i > endIndex) {
break;
}
String comName = comEntry.getKeyAsString();
long num = comEntry.getDocCount();
custMapVO.setComName(comName);
custMapVO.setAmount(new BigDecimal(num));
CustMapVO custMapVO1 = new CustMapVO();
BeanUtils.copyProperties(custMapVO,custMapVO1);
dataList.add(custMapVO1);
}
}
}
}
pager.setData(dataList);
return pager;
}
결합 질의 기준 코드는 다음과 같습니다.
private BoolQueryBuilder getBoolQueryBuilder(CustMapConditionVO vo) {
BoolQueryBuilder b = QueryBuilders.boolQuery();
// id
if (StringUtils.isNotBlank(vo.getOrderId())) {
b.must(QueryBuilders.matchQuery("orderId", vo.getOrderId()));
}
//
if (StringUtils.isNotBlank(vo.getLicenseNo())) {
b.must(QueryBuilders.matchQuery("licenseNo", vo.getLicenseNo()));
}
//
if (StringUtils.isNotBlank(vo.getFrameNo())) {
b.must(QueryBuilders.matchQuery("frameNo", vo.getFrameNo()));
}
//
if (StringUtils.isNotBlank(vo.getChannelCode())) {
b.must(QueryBuilders.matchQuery("channelCode", vo.getChannelCode()));
}
//
if (StringUtils.isNotBlank(vo.getInsureComCode())) {
b.must(QueryBuilders.matchQuery("insureComCode", vo.getInsureComCode()));
}
//
if (StringUtils.isNotBlank(vo.getStatus())) {
b.must(QueryBuilders.matchQuery("status", vo.getStatus()));
}
//
if (StringUtils.isNotBlank(vo.getReceiptMessage())) {
String[] cStartEndTimeArray = vo.getReceiptMessage().split("_");
b.must(QueryBuilders.rangeQuery("sendTime").gte(cStartEndTimeArray[0].trim()).lte(cStartEndTimeArray[1].trim()));
}
return b;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.