Skip to content

Commit

Permalink
[#527] fix: 검색 키워드 조합 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
heeeeeseok committed Dec 16, 2024
1 parent d50a058 commit d9e0c5b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -584,4 +584,29 @@ private List<String> getTopSearchVolumeList() {
Set<String> 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<String> combinationUserKeywords(List<String> keywords) {
if (keywords.size() == 1) {
return keywords;
}

List<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -54,4 +59,32 @@ public void setup() {
when(redisTemplate.opsForZSet()).thenReturn(zSetOperations);
}

@Test
@DisplayName("검색어 조합 테스트")
void combinationUserKeywordsTest() {
// given
List<String> keywords = List.of("jeju", "city", "restaurant");

// when
List<String> 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"
);
}

}

0 comments on commit d9e0c5b

Please sign in to comment.