From 9847285021b69bc80b219fa7bec48d707b757881 Mon Sep 17 00:00:00 2001 From: hseoky desktop Date: Sun, 1 Dec 2024 13:14:22 +0900 Subject: [PATCH] =?UTF-8?q?[#521]=20fix:=20=EB=82=98=EB=82=98=EC=8A=A4?= =?UTF-8?q?=ED=94=BD=20=EA=B2=80=EC=83=89=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nanaland/domain/common/dto/SearchDto.java | 2 +- .../domain/nana/dto/NanaSearchDto.java | 4 ++ .../nana/repository/NanaRepositoryImpl.java | 45 ++++++++++++++++--- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/jeju/nanaland/domain/common/dto/SearchDto.java b/src/main/java/com/jeju/nanaland/domain/common/dto/SearchDto.java index 1fcc9e58..b7f51194 100644 --- a/src/main/java/com/jeju/nanaland/domain/common/dto/SearchDto.java +++ b/src/main/java/com/jeju/nanaland/domain/common/dto/SearchDto.java @@ -14,10 +14,10 @@ @AllArgsConstructor public class SearchDto { + protected Long matchedCount; private Long id; private String title; private ImageFileDto firstImage; - private Long matchedCount; private LocalDateTime createdAt; @QueryProjection diff --git a/src/main/java/com/jeju/nanaland/domain/nana/dto/NanaSearchDto.java b/src/main/java/com/jeju/nanaland/domain/nana/dto/NanaSearchDto.java index cebf74e6..1734868b 100644 --- a/src/main/java/com/jeju/nanaland/domain/nana/dto/NanaSearchDto.java +++ b/src/main/java/com/jeju/nanaland/domain/nana/dto/NanaSearchDto.java @@ -21,4 +21,8 @@ public NanaSearchDto(Long id, String title, String originUrl, String thumbnailUr LocalDateTime createdAt) { super(id, title, originUrl, thumbnailUrl, matchedCount, createdAt); } + + public void setMatchedCount(Long matchedCount) { + this.matchedCount = matchedCount; + } } diff --git a/src/main/java/com/jeju/nanaland/domain/nana/repository/NanaRepositoryImpl.java b/src/main/java/com/jeju/nanaland/domain/nana/repository/NanaRepositoryImpl.java index 16c18073..1f0c05dc 100644 --- a/src/main/java/com/jeju/nanaland/domain/nana/repository/NanaRepositoryImpl.java +++ b/src/main/java/com/jeju/nanaland/domain/nana/repository/NanaRepositoryImpl.java @@ -26,6 +26,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.ArrayList; import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -201,18 +202,34 @@ public Page findSearchDtoByKeywordsUnion(List keywords, L .on(nanaTitle.language.eq(language)) .fetch(); - // 해시태그 값을 matchedCount에 더해줌 + // key: nana_id, value: nanaContent 중 키워드와 가장 많이 일치한 수 + Map nanaMap = new HashMap<>(); + for (NanaSearchDto nanaSearchDto : resultDto) { + Long nanaId = nanaSearchDto.getId(); + Long matchedCount = keywordMatchMap.get(nanaId); + nanaMap.put(nanaId, Math.max(nanaMap.getOrDefault(nanaId, 0L), matchedCount)); + } + // nana_id 값과 최대 matchedCount 값을 가진 객체만 관리 + List groupedResultDto = new ArrayList<>(); for (NanaSearchDto nanaSearchDto : resultDto) { + Long nanaId = nanaSearchDto.getId(); + if (nanaSearchDto.getMatchedCount() == nanaMap.get(nanaId)) { + groupedResultDto.add(nanaSearchDto); + } + } + + // 해시태그 값을 matchedCount에 더해줌 + for (NanaSearchDto nanaSearchDto : groupedResultDto) { Long id = nanaSearchDto.getId(); nanaSearchDto.addMatchedCount(keywordMatchMap.getOrDefault(id, 0L)); } // matchedCount가 0이라면 검색결과에서 제거 - resultDto = resultDto.stream() + groupedResultDto = groupedResultDto.stream() .filter(nanaSearchDto -> nanaSearchDto.getMatchedCount() > 0) .toList(); // 매칭된 키워드 수 내림차순, 생성날짜 내림차순 정렬 - List resultList = new ArrayList<>(resultDto); + List resultList = new ArrayList<>(groupedResultDto); resultList.sort(Comparator .comparing(NanaSearchDto::getMatchedCount, Comparator.nullsLast(Comparator.reverseOrder())) @@ -277,18 +294,34 @@ public Page findSearchDtoByKeywordsIntersect(List keyword .on(nanaTitle.language.eq(language)) .fetch(); - // 해시태그 값을 matchedCount에 더해줌 + // key: nana_id, value: nanaContent 중 키워드와 가장 많이 일치한 수 + Map nanaMap = new HashMap<>(); + for (NanaSearchDto nanaSearchDto : resultDto) { + Long nanaId = nanaSearchDto.getId(); + Long matchedCount = keywordMatchMap.get(nanaId); + nanaMap.put(nanaId, Math.max(nanaMap.getOrDefault(nanaId, 0L), matchedCount)); + } + // nana_id 값과 최대 matchedCount 값을 가진 객체만 관리 + List groupedResultDto = new ArrayList<>(); for (NanaSearchDto nanaSearchDto : resultDto) { + Long nanaId = nanaSearchDto.getId(); + if (nanaSearchDto.getMatchedCount() == nanaMap.get(nanaId)) { + groupedResultDto.add(nanaSearchDto); + } + } + + // 해시태그 값을 matchedCount에 더해줌 + for (NanaSearchDto nanaSearchDto : groupedResultDto) { Long id = nanaSearchDto.getId(); nanaSearchDto.addMatchedCount(keywordMatchMap.getOrDefault(id, 0L)); } // matchedCount가 키워드 개수와 다르다면 검색결과에서 제거 - resultDto = resultDto.stream() + groupedResultDto = groupedResultDto.stream() .filter(nanaSearchDto -> nanaSearchDto.getMatchedCount() >= keywords.size()) .toList(); // 생성날짜 내림차순 정렬 - List resultList = new ArrayList<>(resultDto); + List resultList = new ArrayList<>(groupedResultDto); resultList.sort(Comparator .comparing(NanaSearchDto::getCreatedAt, Comparator.nullsLast(Comparator.reverseOrder())));