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

검색어 자동완성시 구, 동 포함한 검색어 인식 구현 #98

Merged
merged 4 commits into from
May 17, 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 @@ -7,7 +7,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -18,8 +17,8 @@ public class AutoCompleteService {
private final AutoCompleteRepository autoCompleteRepository;

public AutoCompleteResponseDto getAutoComplete(String query) {
List<String> items = query.isEmpty() ? new ArrayList<>() :
autoCompleteRepository.findAutoComplete(query)
List<String> items =
autoCompleteRepository.getAutoComplete(query)
.stream()
.map(this::formatAddress)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import java.util.List;

public interface AutoCompleteRepositoryCustom {
List<AddressDto> findAutoComplete(String query);
List<AddressDto> getAutoComplete(String query);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package contest.collectingbox.module.autocomplete.domain;

import static contest.collectingbox.module.location.domain.QDongInfo.dongInfo;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import contest.collectingbox.module.autocomplete.dto.AddressDto;
import contest.collectingbox.module.autocomplete.dto.QAddressDto;
import contest.collectingbox.module.location.domain.QDongInfo;
import jakarta.persistence.EntityManager;
import java.util.List;

Expand All @@ -16,14 +18,24 @@ public AutoCompleteRepositoryImpl(EntityManager em) {
}

@Override
public List<AddressDto> findAutoComplete(String query) {
QDongInfo dongInfo = QDongInfo.dongInfo;
public List<AddressDto> getAutoComplete(String query) {
return queryFactory
.selectDistinct(new QAddressDto(dongInfo.sigunguNm, dongInfo.dongNm))
.from(dongInfo)
.where(dongInfo.sigunguNm.contains(query).or(dongInfo.dongNm.contains(query)))
.where(containsQuery(query))
.orderBy(dongInfo.sigunguNm.asc(), dongInfo.dongNm.asc())
.limit(5)
.fetch();
}

private BooleanExpression containsQuery(String query) {
String[] split = query.split(" ");
if (split.length == 1) {
return dongInfo.sigunguNm.contains(query)
.or(dongInfo.dongNm.contains(query));
}
return dongInfo.sigunguNm.contains(split[0])
.and(dongInfo.dongNm.contains(split[split.length-1]));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import contest.collectingbox.module.autocomplete.dto.AutoCompleteResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.ArrayList;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -23,7 +24,10 @@ public class AutoCompleteController {
@Operation(summary = "검색어 자동 완성", description = "실시간 입력값에 따라 DB에 저장된 키워드를 가져옵니다.")
@GetMapping
public ApiResponse<AutoCompleteResponseDto> getAutoComplete(@RequestParam String query) {
AutoCompleteResponseDto response = autoCompleteService.getAutoComplete(query);
AutoCompleteResponseDto response = new AutoCompleteResponseDto(new ArrayList<>());
if (!query.isBlank()) {
response = autoCompleteService.getAutoComplete(query.trim());
}
return ApiResponse.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void getAutoCompletes_Success_WithKeyword() {
// given
String keyword = "상";
// mock
given(autoCompleteRepository.findAutoComplete(keyword)).willReturn(
given(autoCompleteRepository.getAutoComplete(keyword)).willReturn(
List.of(new AddressDto("강동구", "상일동"), new AddressDto("노원구", "상계동")));

// when
Expand Down
Loading