Skip to content

Commit

Permalink
[#521] fix: 나나스픽 검색 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
heeeeeseok committed Dec 1, 2024
1 parent 5b0480f commit 9847285
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,18 +202,34 @@ public Page<NanaSearchDto> findSearchDtoByKeywordsUnion(List<String> keywords, L
.on(nanaTitle.language.eq(language))
.fetch();

// 해시태그 값을 matchedCount에 더해줌
// key: nana_id, value: nanaContent 중 키워드와 가장 많이 일치한 수
Map<Long, Long> 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<NanaSearchDto> 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<NanaSearchDto> resultList = new ArrayList<>(resultDto);
List<NanaSearchDto> resultList = new ArrayList<>(groupedResultDto);
resultList.sort(Comparator
.comparing(NanaSearchDto::getMatchedCount,
Comparator.nullsLast(Comparator.reverseOrder()))
Expand Down Expand Up @@ -277,18 +294,34 @@ public Page<NanaSearchDto> findSearchDtoByKeywordsIntersect(List<String> keyword
.on(nanaTitle.language.eq(language))
.fetch();

// 해시태그 값을 matchedCount에 더해줌
// key: nana_id, value: nanaContent 중 키워드와 가장 많이 일치한 수
Map<Long, Long> 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<NanaSearchDto> 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<NanaSearchDto> resultList = new ArrayList<>(resultDto);
List<NanaSearchDto> resultList = new ArrayList<>(groupedResultDto);
resultList.sort(Comparator
.comparing(NanaSearchDto::getCreatedAt,
Comparator.nullsLast(Comparator.reverseOrder())));
Expand Down

0 comments on commit 9847285

Please sign in to comment.