Skip to content

Commit

Permalink
Merge pull request #115 from dnd-side-project/refactor/search_region
Browse files Browse the repository at this point in the history
[refactor] 지역검색 쿼리 fulltext search 적용
  • Loading branch information
stophwan authored Sep 16, 2024
2 parents d9b4369 + b4adcf4 commit 8ac0cf4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docker/blooming-container/docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
MYSQL_DATABASE: blooming
MYSQL_ROOT_PASSWORD: 1234
TZ: "Asia/Seoul"
volumes:
- ./my.cnf:/etc/my.cnf
command:
- --character-set-server=utf8
- --collation-server=utf8_general_ci
Expand Down
2 changes: 2 additions & 0 deletions docker/blooming-container/my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mysqld]
ngram_token_size=1
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,41 @@ package dnd11th.blooming.api.service.region
import dnd11th.blooming.api.dto.region.RegionResponse
import dnd11th.blooming.domain.entity.region.Region
import dnd11th.blooming.domain.repository.region.RegionRepository
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class RegionService(
private val regionRepository: RegionRepository,
) {
companion object {
private const val DEFAULT_PAGE_NUMBER = 0
private const val DEFAULT_PAGE_SIZE = 30
private val jamoSet =
setOf(
'', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
)
}

fun findRegion(name: String): List<RegionResponse> {
val pageable: Pageable = PageRequest.of(DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE)
val regions: List<Region> = regionRepository.findByNameContaining(name, pageable)
val searchQuery = createSearchQuery(name)
val regions: List<Region> = regionRepository.findByNameContaining(searchQuery, DEFAULT_PAGE_SIZE)
return regions.stream().map { region -> RegionResponse.from(region) }.toList()
}

fun createSearchQuery(input: String): String {
return input
.removeLastJamoIfOneWord()
.split(" ")
.filter { it.isNotBlank() }
.joinToString(" ") { "+$it" }
}

fun String.removeLastJamoIfOneWord(): String {
if (this.length > 1 && this.last() in jamoSet) {
return this.dropLast(1)
}
return this
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package dnd11th.blooming.domain.repository.region

import dnd11th.blooming.domain.entity.region.Region
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface RegionRepository : JpaRepository<Region, Int> {
@Query(
value = """
SELECT *
FROM region
WHERE MATCH(name) AGAINST(:searchQuery IN BOOLEAN MODE)
LIMIT :limit
""",
nativeQuery = true,
)
fun findByNameContaining(
name: String,
pageable: Pageable,
@Param("searchQuery") searchQuery: String,
@Param("limit") limit: Int,
): List<Region>
}

0 comments on commit 8ac0cf4

Please sign in to comment.