Skip to content

Commit

Permalink
검색시 발생하는 500 에러 해결 (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
HoeSeong123 authored Dec 13, 2024
1 parent 4dc5328 commit 106873f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static codezap.template.domain.QTemplate.template;

import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;
Expand All @@ -21,6 +22,7 @@ public class FullTextSearchSearchStrategy implements SearchStrategy {
private static final int FULL_TEXT_FIELD_SECOND_ORDER = 1;
private static final int FULL_TEXT_KEYWORD_ORDER = 2;
private static final int NO_MATCHED_SCORE = 0;
private static final Pattern INVALID_CHAR_PATTERN = Pattern.compile("[^a-zA-Z0-9가-힣]");

@Override
public BooleanExpression matchedKeyword(String trimmedKeyword) {
Expand All @@ -39,6 +41,8 @@ public BooleanExpression matchedKeyword(String trimmedKeyword) {
private String parseKeyword(String trimmedKeyword) {
String[] parsedKeywords = trimmedKeyword.split(" ");
return Arrays.stream(parsedKeywords)
.map(keyword -> INVALID_CHAR_PATTERN.matcher(keyword).replaceAll(""))
.filter(keyword -> !keyword.isEmpty())
.map(keyword -> "+" + keyword)
.collect(Collectors.joining(" "));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -126,11 +128,11 @@ void findAllFailureWithNullPageable() {
.hasMessage("Pageable을 필수로 작성해야 합니다.");
}

@Test
@ParameterizedTest
@DisplayName("검색 기능: 키워드로 템플릿 목록 조회 성공")
void findAllSuccessByKeyword() {
@ValueSource(strings = {"안녕", "+안녕", "안+녕", " 안녕", "안녕+", "안녕-", "-안녕"})
void findAllSuccessByKeyword(String keyword) {
Long memberId = null;
String keyword = "안녕";
Long categoryId = null;
List<Long> tagIds = null;
Visibility visibility = null;
Expand All @@ -139,7 +141,7 @@ void findAllSuccessByKeyword() {
FixedPage<Template> actual = sut.findAllBy(memberId, keyword, categoryId, tagIds, visibility, pageable);

assertThat(actual.contents()).containsExactlyInAnyOrder(templates.stream()
.filter(template -> template.getTitle().contains(keyword) || template.getDescription().contains(keyword))
.filter(template -> template.getTitle().contains("안녕") || template.getDescription().contains("안녕"))
.toArray(Template[]::new));
}

Expand All @@ -161,7 +163,7 @@ void findAllSuccessByCategoryId() {
}

@Test
@DisplayName("검색 기능: 공개 범위로 템플릿 목록 조회 성공")
@DisplayName("검색 기능: 복수 태그 ID로 템플릿 목록 조회 성공")
void findAllSuccessByTagIds() {
Long memberId = null;
String keyword = null;
Expand Down

0 comments on commit 106873f

Please sign in to comment.