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

[BLOOM-114] 지역검색 쿼리 fulltext search 적용 #115

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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,25 @@ 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

@Service
class RegionService(
private val regionRepository: RegionRepository,
) {
companion object {
private const val DEFAULT_PAGE_NUMBER = 0
private const val DEFAULT_PAGE_SIZE = 30
}

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.split(" ")
.filter { it.isNotBlank() }
.joinToString(" ") { "+$it" }
}
}
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>
}
Loading