Skip to content

Commit

Permalink
fix: keyword 검색 쿼리 LIKE -> MATCH 쿼리로 수정 (#77)
Browse files Browse the repository at this point in the history
* fix: LIKE 쿼리 MATCH 쿼리로 수정

* refactor: CustomFunctionContributor -> MatchFunctionContributor
  • Loading branch information
annahxxl authored Mar 12, 2024
1 parent 1d543d6 commit 3089e3a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.kw.data.common.contributor

import org.hibernate.boot.model.FunctionContributions
import org.hibernate.boot.model.FunctionContributor
import org.hibernate.type.StandardBasicTypes.DOUBLE

class MatchFunctionContributor : FunctionContributor {

companion object {
private const val FUNCTION_NAME = "MATCH_AGAINST"
private const val FUNCTION_PATTERN = "MATCH (?1) AGAINST (?2 IN BOOLEAN MODE)"
}

override fun contributeFunctions(functionContributions: FunctionContributions) {
functionContributions.functionRegistry
.registerPattern(
FUNCTION_NAME,
FUNCTION_PATTERN,
functionContributions.typeConfiguration.basicTypeRegistry.resolve(DOUBLE)
)
}
}
21 changes: 21 additions & 0 deletions data/src/main/kotlin/com/kw/data/common/util/CustomFunction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.kw.data.common.util

import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.core.types.dsl.Expressions
import com.querydsl.core.types.dsl.StringPath

class CustomFunction {
companion object {
fun match(target: StringPath, keyword: String): BooleanExpression? {
if (keyword.isEmpty()) {
return null
}
return Expressions.numberTemplate(
java.lang.Double::class.java,
"FUNCTION('MATCH_AGAINST', {0}, {1})",
target,
"$keyword*"
).gt(0)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kw.data.domain.bundle.repository

import com.kw.data.common.dto.SearchSortingType
import com.kw.data.common.util.CustomFunction
import com.kw.data.domain.bundle.Bundle
import com.kw.data.domain.bundle.QBundle.Companion.bundle
import com.kw.data.domain.bundle.QBundleTag.Companion.bundleTag
Expand All @@ -17,7 +18,7 @@ class BundleCustomRepositoryImpl(private val queryFactory: JPAQueryFactory) : Bu
.from(bundle)
.where(
bundle.shareType.eq(Bundle.ShareType.PUBLIC),
condition.keyword?.let { bundle.name.contains(it) }
condition.keyword?.let { CustomFunction.match(bundle.name, it) }
)

if (condition.tagIds != null) {
Expand All @@ -35,7 +36,7 @@ class BundleCustomRepositoryImpl(private val queryFactory: JPAQueryFactory) : Bu
.leftJoin(bundle.bundleTags, bundleTag).fetchJoin()
.where(
bundle.shareType.eq(Bundle.ShareType.PUBLIC),
condition.keyword?.let { bundle.name.contains(it) }
condition.keyword?.let { CustomFunction.match(bundle.name, it) }
)
.orderBy(
if (condition.sortingType == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.kw.data.domain.question.repository

import com.kw.data.common.dto.SearchSortingType
import com.kw.data.common.util.CustomFunction
import com.kw.data.domain.question.QQuestion.Companion.question
import com.kw.data.domain.question.QQuestionTag.Companion.questionTag
import com.kw.data.domain.question.Question
import com.kw.data.domain.question.dto.QuestionSearchDto
import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.stereotype.Repository

Expand All @@ -20,7 +20,7 @@ class QuestionCustomRepositoryImpl(val jpaQueryFactory: JPAQueryFactory) : Quest
.selectFrom(question)
.leftJoin(question.questionTags, questionTag).fetchJoin()
.where(
keyword?.let { containsKeyword(keyword) }
keyword?.let { CustomFunction.match(question.content, keyword) }
)
.orderBy(
if (questionSearchDto.sortingType == null) {
Expand All @@ -44,10 +44,6 @@ class QuestionCustomRepositoryImpl(val jpaQueryFactory: JPAQueryFactory) : Quest
return query.fetch()
}

private fun containsKeyword(keyword: String): BooleanExpression? {
return question.content.contains(keyword)
}

override fun getPageNum(questionSearchDto: QuestionSearchDto): Long {
val keyword = questionSearchDto.keyword
val page = questionSearchDto.page
Expand All @@ -57,7 +53,7 @@ class QuestionCustomRepositoryImpl(val jpaQueryFactory: JPAQueryFactory) : Quest
.select(question.count())
.from(question)
.where(
keyword?.let { containsKeyword(keyword) }
keyword?.let { CustomFunction.match(question.content, keyword) }
)

if (questionSearchDto.tagIds != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.kw.data.common.contributor.MatchFunctionContributor

0 comments on commit 3089e3a

Please sign in to comment.