Skip to content

Commit

Permalink
Merge pull request #2 from pop-pin/ClassifiedAsGu
Browse files Browse the repository at this point in the history
Classified as gu
  • Loading branch information
codrin2 authored Dec 11, 2023
2 parents 7ed2968 + eb5657f commit 73d3226
Showing 1 changed file with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.springframework.data.mongodb.core.query.Criteria;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
Expand All @@ -37,27 +39,87 @@ public Page<Location> searchLocation(String name, Pageable pageable) {
}

public List<AverageRatingDto> calculateAverageRatingByGu() {
Map<String, String> guNameMapping = Map.ofEntries(
Map.entry("강남구", "Gangnam-gu"), Map.entry("강동구", "Gangdong-gu"),
Map.entry("강북구", "Gangbuk-gu"), Map.entry("강서구", "Gangseo-gu"),
Map.entry("관악구", "Gwanak-gu"), Map.entry("광진구", "Gwangjin-gu"),
Map.entry("구로구", "Guro-gu"), Map.entry("금천구", "Geumcheon-gu"),
Map.entry("노원구", "Nowon-gu"), Map.entry("도봉구", "Dobong-gu"),
Map.entry("동대문구", "Dongdaemun-gu"), Map.entry("동작구", "Dongjak-gu"),
Map.entry("마포구", "Mapo-gu"), Map.entry("서대문구", "Seodaemun-gu"),
Map.entry("서초구", "Seocho-gu"), Map.entry("성동구", "Seongdong-gu"),
Map.entry("성북구", "Seongbuk-gu"), Map.entry("송파구", "Songpa-gu"),
Map.entry("양천구", "Yangcheon-gu"), Map.entry("영등포구", "Yeongdeungpo-gu"),
Map.entry("용산구", "Yongsan-gu"), Map.entry("은평구", "Eunpyeong-gu"),
Map.entry("종로구", "Jongno-gu"), Map.entry("중구", "Jung-gu"),
Map.entry("중랑구", "Jungnang-gu")
);

String regexPattern = String.join("|", guNameMapping.keySet()) + "|" + String.join("|", guNameMapping.values());

AggregationExpression regexExpression = context -> new Document("$regexFind",
new Document("input", "$vicinity")
.append("regex", "([가-힣]+구|[A-Za-z]+-gu)")
.append("regex", "(" + regexPattern + ")$")
);

ProjectionOperation projectionOperation = Aggregation.project()
.and(regexExpression).as("guMatch")
.andInclude("rating");

ProjectionOperation mappingProjection = Aggregation.project()
.and(new AggregationExpression() {
@Override
public Document toDocument(AggregationOperationContext context) {
Document switchExpr = new Document("$switch", new Document("branches", Arrays.asList(
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Gangnam-gu"))).append("then", "강남구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Gangdong-gu"))).append("then", "강동구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Gangbuk-gu"))).append("then", "강북구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Gangseo-gu"))).append("then", "강서구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Gwanak-gu"))).append("then", "관악구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Gwangjin-gu"))).append("then", "광진구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Guro-gu"))).append("then", "구로구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Geumcheon-gu"))).append("then", "금천구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Nowon-gu"))).append("then", "노원구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Dobong-gu"))).append("then", "도봉구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Dongdaemun-gu"))).append("then", "동대문구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Dongjak-gu"))).append("then", "동작구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Mapo-gu"))).append("then", "마포구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Seodaemun-gu"))).append("then", "서대문구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Seocho-gu"))).append("then", "서초구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Seongdong-gu"))).append("then", "성동구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Seongbuk-gu"))).append("then", "성북구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Songpa-gu"))).append("then", "송파구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Yangcheon-gu"))).append("then", "양천구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Yeongdeungpo-gu"))).append("then", "영등포구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Yongsan-gu"))).append("then", "용산구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Eunpyeong-gu"))).append("then", "은평구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Jongno-gu"))).append("then", "종로구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Jung-gu"))).append("then", "중구"),
new Document("case", new Document("$eq", Arrays.asList("$guMatch.match", "Jungnang-gu"))).append("then", "중랑구")
)).append("default", "Unknown"));
return new Document("$cond", Arrays.asList(
new Document("$ne", Arrays.asList("$guMatch.match", null)),
switchExpr,
"Unknown"
));
}
}).as("gu")
.andInclude("rating");

Aggregation aggregation = Aggregation.newAggregation(
projectionOperation,
Aggregation.project().and("guMatch.match").as("gu").andInclude("rating"),
Aggregation.match(Criteria.where("gu").exists(true)),
Aggregation.group("gu").avg("rating").as("averageRating"),
mappingProjection,
Aggregation.match(Criteria.where("gu").ne("Unknown")),
Aggregation.group("gu")
.avg("rating").as("averageRating"),
Aggregation.project("averageRating").and("gu").previousOperation()
);

AggregationResults<AverageRatingDto> results = mongoTemplate.aggregate(aggregation, Location.class, AverageRatingDto.class);
return results.getMappedResults();
}


// @Transactional
// public void updateLocation(String id, Location updatedLocation) {
// Optional<Location> optionalLocation = locationRepository.findById(id);
Expand Down

0 comments on commit 73d3226

Please sign in to comment.