diff --git a/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeyword.java b/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeyword.java index 373702666..17d7cd394 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeyword.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeyword.java @@ -57,7 +57,6 @@ public void updateWeight(double newWeight) { public void resetWeight() { this.weight = 1.0; - this.lastSearchedAt = LocalDateTime.now(); } public void incrementTotalSearch() { diff --git a/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeywordIpMap.java b/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeywordIpMap.java index b901ee098..77e214d53 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeywordIpMap.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/model/ArticleSearchKeywordIpMap.java @@ -12,6 +12,7 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; +import jakarta.persistence.UniqueConstraint; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -19,7 +20,8 @@ @Getter @Entity @Table(name = "article_search_keyword_ip_map", indexes = { - @Index(name = "idx_ip_address", columnList = "ipAddress") + @Index(name = "idx_ip_address", columnList = "ipAddress")}, uniqueConstraints = { + @UniqueConstraint(name = "ux_keyword_ip", columnNames = {"keyword_id", "ipAddress"}) }) @NoArgsConstructor(access = PROTECTED) public class ArticleSearchKeywordIpMap extends BaseEntity { diff --git a/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordIpMapRepository.java b/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordIpMapRepository.java index 173023dce..0d3ab84f5 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordIpMapRepository.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordIpMapRepository.java @@ -15,5 +15,5 @@ public interface ArticleSearchKeywordIpMapRepository extends Repository findByArticleSearchKeywordAndIpAddress(ArticleSearchKeyword keyword, String ipAddress); - List findByCreatedAtBetween(LocalDateTime fiveHoursThirtyMinutesAgo, LocalDateTime now); + List findByUpdatedAtBetween(LocalDateTime before, LocalDateTime now); } diff --git a/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordRepository.java b/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordRepository.java index 227623c90..a526d1b2e 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordRepository.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleSearchKeywordRepository.java @@ -31,5 +31,5 @@ public interface ArticleSearchKeywordRepository extends Repository findTopKeywordsByLatest(Pageable pageable); - List findByCreatedAtBetween(LocalDateTime startTime, LocalDateTime endTime); + List findByUpdatedAtBetween(LocalDateTime startTime, LocalDateTime endTime); } diff --git a/src/main/java/in/koreatech/koin/domain/community/article/service/ArticleService.java b/src/main/java/in/koreatech/koin/domain/community/article/service/ArticleService.java index 64ccc18c9..1996d03c6 100644 --- a/src/main/java/in/koreatech/koin/domain/community/article/service/ArticleService.java +++ b/src/main/java/in/koreatech/koin/domain/community/article/service/ArticleService.java @@ -211,8 +211,6 @@ private void updateKeywordWeightAndCount(ArticleSearchKeyword keyword, ArticleSe if (map.getSearchCount() <= 10) { keyword.updateWeight(keyword.getWeight() + additionalWeight); } - - keyword.updateWeight(keyword.getWeight() + additionalWeight); articleSearchKeywordRepository.save(keyword); } @@ -221,9 +219,9 @@ public void resetWeightsAndCounts() { LocalDateTime now = LocalDateTime.now(); LocalDateTime before = now.minusHours(6).minusMinutes(30); - List keywordsToUpdate = articleSearchKeywordRepository.findByCreatedAtBetween( + List keywordsToUpdate = articleSearchKeywordRepository.findByUpdatedAtBetween( before, now); - List ipMapsToUpdate = articleSearchKeywordIpMapRepository.findByCreatedAtBetween( + List ipMapsToUpdate = articleSearchKeywordIpMapRepository.findByUpdatedAtBetween( before, now); for (ArticleSearchKeyword keyword : keywordsToUpdate) { diff --git a/src/main/resources/db/migration/V60__alter_search_keywords_and_ip_map_unique_cascade.sql b/src/main/resources/db/migration/V60__alter_search_keywords_and_ip_map_unique_cascade.sql new file mode 100644 index 000000000..b2965a323 --- /dev/null +++ b/src/main/resources/db/migration/V60__alter_search_keywords_and_ip_map_unique_cascade.sql @@ -0,0 +1,9 @@ +ALTER TABLE `article_search_keywords` + MODIFY COLUMN `keyword` VARCHAR(255) NOT NULL UNIQUE; + +ALTER TABLE article_search_keyword_ip_map + ADD CONSTRAINT unique_keyword_ip UNIQUE (keyword_id, ip_address), + ADD CONSTRAINT fk_keyword_id + FOREIGN KEY (keyword_id) + REFERENCES article_search_keywords (id) + ON DELETE CASCADE;