Skip to content
Open
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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ dependencies {
implementation group: 'com.mysql', name: 'mysql-connector-j', version: '8.3.0'

implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.31.0'

// ElasticSearch
implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
implementation 'jakarta.json:jakarta.json-api:2.1.1'
}

tasks.named('test') {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/playhive/batch/news/entity/NewsDocument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.playhive.batch.news.entity;

import jakarta.persistence.Id;
import lombok.*;
import org.springframework.data.elasticsearch.annotations.Document;

import java.time.LocalDateTime;

@Document(indexName = "news")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class NewsDocument {

@Id
private String id;

private String title;
private String content;
private String category;
private LocalDateTime postDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.playhive.batch.news.repository;

import com.playhive.batch.news.entity.NewsDocument;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface NewsSearchRepository extends ElasticsearchRepository<NewsDocument, String> {

}
43 changes: 43 additions & 0 deletions src/main/java/com/playhive/batch/news/service/NewsService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.playhive.batch.news.service;

import com.playhive.batch.news.entity.NewsDocument;
import com.playhive.batch.news.repository.NewsSearchRepository;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -10,17 +13,57 @@

import lombok.RequiredArgsConstructor;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class NewsService {

private final NewsRepository newsRepository;
private final NewsCountService newsCountService;
private final NewsSearchRepository newsSearchRepository;

@PostConstruct
public void setUpES() {
System.out.println("================ 초기화 시작 ===================");
List<News> allNews = newsRepository.findAll();
syncToElastic(allNews);
System.out.println("================ 초기화 완료 ===================");
}

public void syncToElastic(List<News> newsList) {
int batchSize = 1000;

for (int i = 0; i < newsList.size(); i += batchSize) {
int end = Math.min(i + batchSize, newsList.size());
List<NewsDocument> chunk = newsList.subList(i, end).stream()
.map(news -> NewsDocument.builder()
.id(news.getId().toString())
.title(news.getTitle())
.content(news.getContent())
.category(news.getCategory().name())
.postDate(news.getPostDate())
.build())
.toList();

newsSearchRepository.saveAll(chunk);
}
}

public void saveNews(NewsSaveRequest newsSaveRequest) {
News news = newsRepository.save(newsSaveRequest.toEntity());
newsCountService.saveNewsCount(news);

NewsDocument doc = NewsDocument.builder()
.id(String.valueOf(news.getId()))
.title(news.getTitle())
.content(news.getContent())
.category(news.getCategory().name())
.postDate(news.getPostDate())
.build();

newsSearchRepository.save(doc);
}

public String findRecentPostDate(NewsCategory category) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ spring:
hikari:
maximum-pool-size: 20


jpa:
show-sql: false
properties:
Expand All @@ -32,6 +31,9 @@ spring:
# none 기존테이블을 더 이상 건드리지 않음.
open-in-view: true

elasticsearch:
uris: http://{es-server-ip}:9200

logging:
level:
root: info # 전체 로그를 INFO, DEBUG, WARN 수준으로 설정
Expand Down