Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public ElasticsearchAggregations getUsageDateAndDiffAndCount(User user) {

// userId 필터
Query userFilter = getUserFilter(userId);
Query curMonthFilter = getCurMonthFilter();

// 사용자 성별, 연령대의 평균 사용량 정보
int userAgeRange = ((currentYear - user.getBirthDate().getYear() + 1) / 10) * 10;
Expand All @@ -70,18 +71,29 @@ public ElasticsearchAggregations getUsageDateAndDiffAndCount(User user) {

// 카테고리 순위
Aggregation categoryAggregation = Aggregation.of(a -> a
.terms(t -> t
.field("category")
.order(List.of(NamedValue.of("_count", SortOrder.Desc)))
.filter(f -> f
.bool(b -> b.filter(List.of(userFilter, curMonthFilter)))
)
.aggregations("rank", subAgg -> subAgg
.terms(t -> t
.field("category")
.size(5)
.order(List.of(NamedValue.of("_count", SortOrder.Desc)))
)
)
);

// 제휴처 순위
Aggregation brandAggregation = Aggregation.of(a -> a
.terms(t -> t
.field("brandName")
.size(10)
.order(List.of(NamedValue.of("_count", SortOrder.Desc)))
.filter(f -> f
.bool(b -> b.filter(List.of(userFilter, curMonthFilter)))
)
.aggregations("rank", subAgg -> subAgg
.terms(t -> t
.field("brandName")
.size(5)
.order(List.of(NamedValue.of("_count", SortOrder.Desc)))
)
)
);

Expand Down Expand Up @@ -465,4 +477,12 @@ private Query getUserRankFilter(User user) {
))
)._toQuery();
}

private Query getCurMonthFilter() {
return DateRangeQuery.of(r -> r
.field("createdAt")
.gte("now/M")
.lte("now")
)._toRangeQuery()._toQuery();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.ureca.uble.entity.document.UsageHistoryDocument;
import com.ureca.uble.global.exception.GlobalException;
import lombok.RequiredArgsConstructor;

import org.slf4j.MDC;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchAggregations;
import org.springframework.data.elasticsearch.core.SearchHits;
Expand Down Expand Up @@ -214,14 +213,16 @@ public GetTimeRecommendationListRes getTimeRecommendation(Long userId) {

private List<CategoryRankRes> getCategoryRankList(ElasticsearchAggregations rankResult) {
return rankResult.aggregationsAsMap().get("category_rank").aggregation()
.getAggregate().sterms().buckets().array().stream()
.getAggregate().filter().aggregations()
.get("rank").sterms().buckets().array().stream()
.map(b -> CategoryRankRes.of(b.key().stringValue(), b.docCount()))
.toList();
}

private List<BrandRankRes> getBrandRankList(ElasticsearchAggregations rankResult) {
return rankResult.aggregationsAsMap().get("brand_rank").aggregation()
.getAggregate().sterms().buckets().array().stream()
.getAggregate().filter().aggregations()
.get("rank").sterms().buckets().array().stream()
.map(b -> BrandRankRes.of(b.key().stringValue(), b.docCount()))
.toList();
}
Expand Down
Loading