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 d9e0c5b commit a88c89d
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,34 +420,6 @@ public PopularPostPreviewDto findPostPreviewDtoByLanguageAndId(Language language
.fetchOne();
}

private List<Long> getIdListContainAllHashtags(String keywords, Language language) {
return queryFactory
.select(experience.id)
.from(experience)
.leftJoin(hashtag)
.on(hashtag.post.id.eq(experience.id)
.and(hashtag.category.eq(Category.EXPERIENCE))
.and(hashtag.language.eq(language)))
.where(hashtag.keyword.content.toLowerCase().trim().in(keywords))
.groupBy(experience.id)
.having(experience.id.count().eq(splitKeyword(keywords).stream().count()))
.fetch();
}

private List<Long> getIdListContainAllHashtags(List<String> keywords, Language language) {
return queryFactory
.select(experience.id)
.from(experience)
.leftJoin(hashtag)
.on(hashtag.post.id.eq(experience.id)
.and(hashtag.category.eq(Category.EXPERIENCE))
.and(hashtag.language.eq(language)))
.where(hashtag.keyword.content.toLowerCase().trim().in(keywords))
.groupBy(experience.id)
.having(experience.id.count().eq(keywords.stream().count()))
.fetch();
}

private List<String> splitKeyword(String keyword) {
String[] tokens = keyword.split("\\s+");
List<String> tokenList = new ArrayList<>();
Expand Down Expand Up @@ -476,15 +448,42 @@ private BooleanExpression keywordCondition(List<ExperienceTypeKeyword> keywordFi
}
}

/**
* 공백 제거, 소문자화, '-'와 '_' 제거
*
* @param stringExpression 조건절 컬럼
* @return 정규화된 컬럼
*/
private StringExpression normalizeStringExpression(StringExpression stringExpression) {
return Expressions.stringTemplate(
"replace(replace({0}, '-', ''), '_', '')",
stringExpression.toLowerCase().trim());
}

/**
* 제목, 주소태그, 내용과 일치하는 키워드 개수 카운팅
*
* @param keywords 키워드
* @return 키워드를 포함하는 조건 개수
*/
private Expression<Long> countMatchingWithKeyword(List<String> keywords) {
return Expressions.asNumber(0L)
.add(countMatchingConditionWithKeyword(experienceTrans.title.toLowerCase().trim(), keywords,
0))
.add(countMatchingConditionWithKeyword(experienceTrans.addressTag.toLowerCase().trim(),
.add(countMatchingConditionWithKeyword(normalizeStringExpression(experienceTrans.title),
keywords, 0))
.add(
countMatchingConditionWithKeyword(normalizeStringExpression(experienceTrans.addressTag),
keywords, 0))
.add(countMatchingConditionWithKeyword(experienceTrans.content, keywords, 0));
}

/**
* 조건이 키워드를 포함하는지 검사
*
* @param condition 테이블 컬럼
* @param keywords 유저 키워드 리스트
* @param idx 키워드 인덱스
* @return
*/
private Expression<Integer> countMatchingConditionWithKeyword(StringExpression condition,
List<String> keywords, int idx) {
if (idx == keywords.size()) {
Expand All @@ -497,13 +496,4 @@ private Expression<Integer> countMatchingConditionWithKeyword(StringExpression c
.otherwise(0)
.add(countMatchingConditionWithKeyword(condition, keywords, idx + 1));
}

private BooleanExpression containsAllKeywords(StringExpression condition, List<String> keywords) {
BooleanExpression expression = null;
for (String keyword : keywords) {
BooleanExpression containsKeyword = condition.contains(keyword);
expression = (expression == null) ? containsKeyword : expression.and(containsKeyword);
}
return expression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,18 @@ private BooleanExpression addressTagCondition(Language language, List<AddressTag
}
}

/**
* 공백 제거, 소문자화, '-'와 '_' 제거
*
* @param stringExpression 조건절 컬럼
* @return 정규화된 컬럼
*/
private StringExpression normalizeStringExpression(StringExpression stringExpression) {
return Expressions.stringTemplate(
"replace(replace({0}, '-', ''), '_', '')",
stringExpression.toLowerCase().trim());
}

/**
* 제목, 주소태그, 내용과 일치하는 키워드 개수 카운팅
*
Expand All @@ -542,9 +554,9 @@ private BooleanExpression addressTagCondition(Language language, List<AddressTag
*/
private Expression<Long> countMatchingWithKeyword(List<String> keywords) {
return Expressions.asNumber(0L)
.add(countMatchingConditionWithKeyword(festivalTrans.title.toLowerCase().trim(), keywords,
0))
.add(countMatchingConditionWithKeyword(festivalTrans.addressTag.toLowerCase().trim(),
.add(countMatchingConditionWithKeyword(normalizeStringExpression(festivalTrans.title),
keywords, 0))
.add(countMatchingConditionWithKeyword(normalizeStringExpression(festivalTrans.addressTag),
keywords, 0))
.add(countMatchingConditionWithKeyword(festivalTrans.content, keywords, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ private BooleanExpression addressTagCondition(Language language, List<AddressTag
}
}

/**
* 공백 제거, 소문자화, '-'와 '_' 제거
*
* @param stringExpression 조건절 컬럼
* @return 정규화된 컬럼
*/
private StringExpression normalizeStringExpression(StringExpression stringExpression) {
return Expressions.stringTemplate(
"replace(replace({0}, '-', ''), '_', '')",
stringExpression.toLowerCase().trim());
}

/**
* 제목, 주소태그, 내용과 일치하는 키워드 개수 카운팅
*
Expand All @@ -395,9 +407,9 @@ private BooleanExpression addressTagCondition(Language language, List<AddressTag
*/
private Expression<Long> countMatchingWithKeyword(List<String> keywords) {
return Expressions.asNumber(0L)
.add(countMatchingConditionWithKeyword(marketTrans.title.toLowerCase().trim(), keywords,
0))
.add(countMatchingConditionWithKeyword(marketTrans.addressTag.toLowerCase().trim(),
.add(countMatchingConditionWithKeyword(normalizeStringExpression(marketTrans.title),
keywords, 0))
.add(countMatchingConditionWithKeyword(normalizeStringExpression(marketTrans.addressTag),
keywords, 0))
.add(countMatchingConditionWithKeyword(marketTrans.content, keywords, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ private List<String> splitKeyword(String keyword) {
return tokenList;
}

/**
* 공백 제거, 소문자화, '-'와 '_' 제거
*
* @param stringExpression 조건절 컬럼
* @return 정규화된 컬럼
*/
private StringExpression normalizeStringExpression(StringExpression stringExpression) {
return Expressions.stringTemplate(
"replace(replace({0}, '-', ''), '_', '')",
stringExpression.toLowerCase().trim());
}

/**
* 제목, 주소태그, 내용과 일치하는 키워드 개수 카운팅
*
Expand All @@ -393,8 +405,8 @@ private List<String> splitKeyword(String keyword) {
*/
private Expression<Long> getMaxMatchingCountWithKeyword(List<String> keywords) {
return Expressions.asNumber(0L)
.add(countMatchingConditionWithKeyword(nanaTitle.heading.toLowerCase().trim(), keywords,
0))
.add(countMatchingConditionWithKeyword(normalizeStringExpression(nanaTitle.heading),
keywords, 0))
.add(countMatchingConditionWithKeyword(nanaContent.content, keywords, 0))
.max();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,18 @@ private List<String> splitKeyword(String keyword) {
return tokenList;
}

/**
* 공백 제거, 소문자화, '-'와 '_' 제거
*
* @param stringExpression 조건절 컬럼
* @return 정규화된 컬럼
*/
private StringExpression normalizeStringExpression(StringExpression stringExpression) {
return Expressions.stringTemplate(
"replace(replace({0}, '-', ''), '_', '')",
stringExpression.toLowerCase().trim());
}

/**
* 제목, 주소태그, 내용과 일치하는 키워드 개수 카운팅
*
Expand All @@ -447,9 +459,9 @@ private List<String> splitKeyword(String keyword) {
*/
private Expression<Long> countMatchingWithKeyword(List<String> keywords) {
return Expressions.asNumber(0L)
.add(countMatchingConditionWithKeyword(natureTrans.title.toLowerCase().trim(), keywords,
0))
.add(countMatchingConditionWithKeyword(natureTrans.addressTag.toLowerCase().trim(),
.add(countMatchingConditionWithKeyword(normalizeStringExpression(natureTrans.title),
keywords, 0))
.add(countMatchingConditionWithKeyword(normalizeStringExpression(natureTrans.addressTag),
keywords, 0))
.add(countMatchingConditionWithKeyword(natureTrans.content, keywords, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,18 @@ public List<Long> findAllIds() {
.fetch();
}

/**
* 공백 제거, 소문자화, '-'와 '_' 제거
*
* @param stringExpression 조건절 컬럼
* @return 정규화된 컬럼
*/
private StringExpression normalizeStringExpression(StringExpression stringExpression) {
return Expressions.stringTemplate(
"replace(replace({0}, '-', ''), '_', '')",
stringExpression.toLowerCase().trim());
}

/**
* 제목, 주소태그, 내용과 일치하는 키워드 개수 카운팅
*
Expand All @@ -502,10 +514,11 @@ public List<Long> findAllIds() {
*/
private Expression<Long> countMatchingWithKeyword(List<String> keywords) {
return Expressions.asNumber(0L)
.add(countMatchingConditionWithKeyword(restaurantTrans.title.toLowerCase().trim(), keywords,
0))
.add(countMatchingConditionWithKeyword(restaurantTrans.addressTag.toLowerCase().trim(),
.add(countMatchingConditionWithKeyword(normalizeStringExpression(restaurantTrans.title),
keywords, 0))
.add(
countMatchingConditionWithKeyword(normalizeStringExpression(restaurantTrans.addressTag),
keywords, 0))
.add(countMatchingConditionWithKeyword(restaurantTrans.content, keywords, 0));
}

Expand All @@ -515,7 +528,7 @@ private Expression<Long> countMatchingWithKeyword(List<String> keywords) {
* @param condition 테이블 컬럼
* @param keywords 유저 키워드 리스트
* @param idx 키워드 인덱스
* @return
* @return 매칭된 수
*/
private Expression<Integer> countMatchingConditionWithKeyword(StringExpression condition,
List<String> keywords, int idx) {
Expand Down
Loading

0 comments on commit a88c89d

Please sign in to comment.