From d9e0c5b96c40efa1709b12dced0c58eae0279c96 Mon Sep 17 00:00:00 2001 From: seokhee Date: Mon, 16 Dec 2024 12:58:32 +0900 Subject: [PATCH] =?UTF-8?q?[#527]=20fix:=20=EA=B2=80=EC=83=89=20=ED=82=A4?= =?UTF-8?q?=EC=9B=8C=EB=93=9C=20=EC=A1=B0=ED=95=A9=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/search/service/SearchService.java | 25 ++++++++++++++ .../search/service/SearchServiceTest.java | 33 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/main/java/com/jeju/nanaland/domain/search/service/SearchService.java b/src/main/java/com/jeju/nanaland/domain/search/service/SearchService.java index 799b5153..befe6132 100644 --- a/src/main/java/com/jeju/nanaland/domain/search/service/SearchService.java +++ b/src/main/java/com/jeju/nanaland/domain/search/service/SearchService.java @@ -584,4 +584,29 @@ private List getTopSearchVolumeList() { Set topSearchVolumes = zSetOperations.reverseRange(SEARCH_VOLUME_KEY, 0, 3); return topSearchVolumes != null ? new ArrayList<>(topSearchVolumes) : new ArrayList<>(); } + + /** + * 검색으로 들어온 키워드 조합 예를 들어 [jeju city restaurant]가 인자로 들어오면 [jeju, city, restaurant, jejucity, + * jejucityrestaurant, cityrestaurant]를 반환 + * + * @param keywords 공백으로 구분된 사용자의 검색어 + * @return 조합된 사용자의 검색어 + */ + private List combinationUserKeywords(List keywords) { + if (keywords.size() == 1) { + return keywords; + } + + List combinedKeywords = new ArrayList<>(keywords); + for (int i = 0; i < keywords.size() - 1; i++) { + StringBuilder combinedKeyword = new StringBuilder(); + combinedKeyword.append(keywords.get(i)); + for (int j = i + 1; j < keywords.size(); j++) { + combinedKeyword.append(keywords.get(j)); + combinedKeywords.add(combinedKeyword.toString()); + } + } + + return combinedKeywords; + } } diff --git a/src/test/java/com/jeju/nanaland/domain/search/service/SearchServiceTest.java b/src/test/java/com/jeju/nanaland/domain/search/service/SearchServiceTest.java index 14c9a248..2a436112 100644 --- a/src/test/java/com/jeju/nanaland/domain/search/service/SearchServiceTest.java +++ b/src/test/java/com/jeju/nanaland/domain/search/service/SearchServiceTest.java @@ -1,5 +1,6 @@ package com.jeju.nanaland.domain.search.service; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; import com.jeju.nanaland.domain.experience.repository.ExperienceRepository; @@ -10,7 +11,11 @@ import com.jeju.nanaland.domain.nature.repository.NatureRepository; import com.jeju.nanaland.domain.restaurant.repository.RestaurantRepository; import com.jeju.nanaland.global.config.RedisConfig; +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @@ -54,4 +59,32 @@ public void setup() { when(redisTemplate.opsForZSet()).thenReturn(zSetOperations); } + @Test + @DisplayName("검색어 조합 테스트") + void combinationUserKeywordsTest() { + // given + List keywords = List.of("jeju", "city", "restaurant"); + + // when + List combinedKeywords = new ArrayList<>(keywords); + for (int i = 0; i < keywords.size() - 1; i++) { + StringBuilder combinedKeyword = new StringBuilder(); + combinedKeyword.append(keywords.get(i)); + for (int j = i + 1; j < keywords.size(); j++) { + combinedKeyword.append(keywords.get(j)); + combinedKeywords.add(combinedKeyword.toString()); + } + } + + // then + assertThat(combinedKeywords).containsExactly( + "jeju", + "city", + "restaurant", + "jejucity", + "jejucityrestaurant", + "cityrestaurant" + ); + } + } \ No newline at end of file