Skip to content
New issue

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

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

Already on GitHub? # to your account

[refactor] 자동완성기능 like로 검색되지 않는 경우 full text search로 검색 #71

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
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ jacocoTestCoverageVerification {
"**/*Request*",
"**/*Response*",
"**/*Config*",
"mju/iphak/maru_egg/common/**",
"mju/iphak/maru_egg/common/dto",
"mju/iphak/maru_egg/common/entity",
"mju/iphak/maru_egg/common/exception",
"mju/iphak/maru_egg/common/meta",
"mju/iphak/maru_egg/common/regex",
"mju/iphak/maru_egg/common/config/**",
"mju/iphak/maru_egg/auth/jwt/**",
"mju/iphak/maru_egg/answer/application/**",
"mju/iphak/maru_egg/common/config/**",
"mju/iphak/maru_egg/auth/#/**",
"mju/iphak/maru_egg/question/domain/**",
"mju/iphak/maru_egg/**/dto/**",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ public List<QuestionListItemResponse> getQuestions(final QuestionType type, fina
public SliceQuestionResponse<SearchedQuestionsResponse> searchQuestionsOfCursorPaging(final String content,
final Integer cursorViewCount, final Long questionId, final Integer size) {
Pageable pageable = PageRequest.of(0, size);
return questionRepository.searchQuestionsOfCursorPagingByContent(
SliceQuestionResponse<SearchedQuestionsResponse> response;
response = questionRepository.searchQuestionsOfCursorPagingByContentWithLikeFunction(
content,
cursorViewCount, questionId, pageable);
if (response.data().isEmpty()) {
response = questionRepository.searchQuestionsOfCursorPagingByContentWithFullTextSearch(
content,
cursorViewCount, questionId, pageable);
}
return response;
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Optional<List<QuestionCore>> searchQuestionsByContentTokenAndTypeAndCategory(fin
final QuestionType type,
final QuestionCategory category);

SliceQuestionResponse<SearchedQuestionsResponse> searchQuestionsOfCursorPagingByContent(final String content,
SliceQuestionResponse<SearchedQuestionsResponse> searchQuestionsOfCursorPagingByContentWithFullTextSearch(
final String content,
final Integer cursorViewCount, final Long questionId, final Pageable pageable);

SliceQuestionResponse<SearchedQuestionsResponse> searchQuestionsOfCursorPagingByContentWithLikeFunction(
final String content,
final Integer cursorViewCount, final Long questionId, final Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Optional<List<QuestionCore>> searchQuestionsByContentTokenAndTypeAndCateg
}

@Override
public SliceQuestionResponse<SearchedQuestionsResponse> searchQuestionsOfCursorPagingByContent(
public SliceQuestionResponse<SearchedQuestionsResponse> searchQuestionsOfCursorPagingByContentWithFullTextSearch(
final String content,
final Integer cursorViewCount, final Long questionId, final Pageable pageable) {
QQuestion question = QQuestion.question;
Expand Down Expand Up @@ -102,6 +102,38 @@ public SliceQuestionResponse<SearchedQuestionsResponse> searchQuestionsOfCursorP
nextCursorViewCount, nextQuestionId);
}

@Override
public SliceQuestionResponse<SearchedQuestionsResponse> searchQuestionsOfCursorPagingByContentWithLikeFunction(
final String content,
final Integer cursorViewCount, final Long questionId, final Pageable pageable) {
QQuestion question = QQuestion.question;
int pageSize = pageable.getPageSize();

List<Question> questions = queryFactory
.selectFrom(question)
.where(question.content.contains(content))
.fetch();

boolean hasNext = questions.size() > pageSize;

if (hasNext) {
questions.remove(pageSize);
}

List<SearchedQuestionsResponse> questionResponses = mapToResponse(questions);

Integer nextCursorViewCount = null;
Long nextQuestionId = null;
if (!questions.isEmpty()) {
Question lastQuestion = questions.get(questions.size() - 1);
nextCursorViewCount = lastQuestion.getViewCount();
nextQuestionId = lastQuestion.getId();
}

return new SliceQuestionResponse<>(questionResponses, pageable.getPageNumber(), pageSize, hasNext,
nextCursorViewCount, nextQuestionId);
}

private NumberTemplate<Double> createBooleanTemplate(QQuestion question, String content) {
return Expressions.numberTemplate(Double.class, "function('match', {0}, {1})", question.content, content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ void setUp() {
SliceQuestionResponse<SearchedQuestionsResponse> expectedResponse = new SliceQuestionResponse<>(
List.of(searchedQuestionsResponse), 0, size, false, null, null);

when(questionRepository.searchQuestionsOfCursorPagingByContent(content, cursorViewCount, questionId, pageable))
when(questionRepository.searchQuestionsOfCursorPagingByContentWithFullTextSearch(content, cursorViewCount,
questionId, pageable))
.thenReturn(expectedResponse);

when(questionRepository.searchQuestionsOfCursorPagingByContentWithLikeFunction(content, cursorViewCount,
questionId, pageable))
.thenReturn(expectedResponse);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void setUp() throws Exception {
Pageable pageable = PageRequest.of(0, 3);

// when
SliceQuestionResponse<SearchedQuestionsResponse> result = questionRepositoryImpl.searchQuestionsOfCursorPagingByContent(
SliceQuestionResponse<SearchedQuestionsResponse> result = questionRepositoryImpl.searchQuestionsOfCursorPagingByContentWithFullTextSearch(
content, null, null, pageable);

// then
Expand Down
Loading