Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

검색시 boolean mode로 변경 #926

Merged
merged 4 commits into from
Dec 3, 2024
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 @@ -8,7 +8,7 @@
public class FullTextFunctionContributor implements FunctionContributor {

private static final String FUNCTION_NAME = "fulltext_match";
private static final String MATCH_AGAINST_FUNCTION = "match(?1, ?2) against(?3)";
private static final String MATCH_AGAINST_FUNCTION = "match(?1, ?2) against(?3 in boolean mode)";

@Override
public void contributeFunctions(FunctionContributions functionContributions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@

import codezap.template.domain.Visibility;
import codezap.template.repository.strategy.FullTextSearchSearchStrategy;
import codezap.template.repository.strategy.LikeSearchStrategy;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class TemplateSearchExpressionProvider {

private final LikeSearchStrategy likeSearchStrategy;
private final FullTextSearchSearchStrategy fullTextSearchStrategy;

private static final int MINIMUM_KEYWORD_LENGTH = 3;

public BooleanExpression filterMember(Long memberId) {
return Optional.ofNullable(memberId)
.map(template.member.id::eq)
Expand Down Expand Up @@ -60,15 +56,7 @@ public BooleanExpression matchesKeyword(String keyword) {
return Optional.ofNullable(keyword)
.filter(k -> !k.isBlank())
.map(String::trim)
.map(this::createKeywordMatchExpression)
.map(fullTextSearchStrategy::matchedKeyword)
.orElse(null);
}

private BooleanExpression createKeywordMatchExpression(String trimmedKeyword) {
if (trimmedKeyword.length() < MINIMUM_KEYWORD_LENGTH) {
return likeSearchStrategy.matchedKeyword(trimmedKeyword);
}

return fullTextSearchStrategy.matchedKeyword(trimmedKeyword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static codezap.template.domain.QSourceCode.sourceCode;
import static codezap.template.domain.QTemplate.template;

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

import org.springframework.stereotype.Component;

import com.querydsl.core.types.dsl.BooleanExpression;
Expand All @@ -21,8 +24,9 @@ public class FullTextSearchSearchStrategy implements SearchStrategy {

@Override
public BooleanExpression matchedKeyword(String trimmedKeyword) {
NumberExpression<Double> titleScore = getMatchedAccuracy(template.title, template.description, trimmedKeyword);
NumberExpression<Double> sourceCodeScore = getMatchedAccuracy(sourceCode.filename, sourceCode.content, trimmedKeyword);
String parsedKeyword = parseKeyword(trimmedKeyword);
NumberExpression<Double> titleScore = getMatchedAccuracy(template.title, template.description, parsedKeyword);
NumberExpression<Double> sourceCodeScore = getMatchedAccuracy(sourceCode.filename, sourceCode.content, parsedKeyword);
return titleScore.gt(NO_MATCHED_SCORE).or(
template.id.in(JPAExpressions
.select(sourceCode.template.id)
Expand All @@ -32,6 +36,13 @@ public BooleanExpression matchedKeyword(String trimmedKeyword) {
);
}

private String parseKeyword(String trimmedKeyword) {
String[] parsedKeywords = trimmedKeyword.split(" ");
return Arrays.stream(parsedKeywords)
.map(keyword -> "+" + keyword)
.collect(Collectors.joining(" "));
}

private NumberExpression<Double> getMatchedAccuracy(Object... args) {
return Expressions.numberTemplate(Double.class,
MATCH_FUNCTION,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import codezap.global.rds.DataSourceConfig;
import codezap.template.repository.TemplateSearchExpressionProvider;
import codezap.template.repository.strategy.FullTextSearchSearchStrategy;
import codezap.template.repository.strategy.LikeSearchStrategy;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
Expand All @@ -29,7 +28,6 @@
DataSourceConfig.class,
QueryDSLConfig.class,
TemplateSearchExpressionProvider.class,
LikeSearchStrategy.class,
FullTextSearchSearchStrategy.class,
FixedPageCounter.class
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@
import codezap.template.domain.Template;
import codezap.template.domain.Visibility;
import codezap.template.repository.strategy.FullTextSearchSearchStrategy;
import codezap.template.repository.strategy.LikeSearchStrategy;

@DataJpaTest(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Repository.class), useDefaultFilters = false)
@Import({
JpaAuditingConfiguration.class,
DataSourceConfig.class,
QueryDSLConfig.class,
TemplateSearchExpressionProvider.class,
LikeSearchStrategy.class,
FullTextSearchSearchStrategy.class,
FixedPageCounter.class,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ void findAllSuccessByMemberIdAndVisibility() {
@DisplayName("검색 기능: 모든 검색 기준으로 템플릿 목록 조회 성공")
void findAllSuccessWithAllCriteria() {
Long memberId = member1.getId();
String keyword = "안녕하세요";
String keyword = "안녕";
Long categoryId = category1.getId();
List<Long> tagIds = List.of(tag1.getId(), tag2.getId());
Visibility visibility = Visibility.PUBLIC;
Expand Down
Loading