1313import org .springframework .data .domain .Page ;
1414import org .springframework .data .domain .PageRequest ;
1515import org .springframework .data .domain .Pageable ;
16+ import org .springframework .data .elasticsearch .core .ElasticsearchOperations ;
17+ import org .springframework .data .elasticsearch .core .IndexOperations ;
1618import org .springframework .stereotype .Component ;
1719import org .springframework .transaction .annotation .Transactional ;
1820
@@ -25,8 +27,8 @@ public class LectureStartupIndexer {
2527
2628 private final LectureElasticRepository lectureElasticRepository ;
2729 private final LectureRepository lectureRepository ;
28-
2930 private final LectureDocumentConverter lectureDocumentConverter ;
31+ private final ElasticsearchOperations elasticsearchOperations ; // 추가
3032
3133 private final int CHUNK_SIZE = 100 ;
3234
@@ -42,6 +44,15 @@ public void onApplicationEvent(ApplicationReadyEvent event) {
4244 return ;
4345 }
4446
47+ // 인덱스 존재 여부 확인 및 생성
48+ IndexOperations indexOps = elasticsearchOperations .indexOps (LectureDocument .class );
49+ if (!indexOps .exists ()) {
50+ log .info ("ElasticSearch lectures 인덱스가 존재하지 않아 직접 생성합니다." );
51+ indexOps .create ();
52+ indexOps .putMapping (indexOps .createMapping (LectureDocument .class ));
53+ log .info ("ElasticSearch lectures 인덱스 및 매핑 생성 완료." );
54+ }
55+
4556 long existingCount = lectureElasticRepository .count ();
4657 if (existingCount > 0 ) {
4758 log .info ("ElasticSearch 인덱스가 이미 존재합니다 ({}개), 전체 재인덱싱을 진행합니다." , existingCount );
@@ -51,25 +62,28 @@ public void onApplicationEvent(ApplicationReadyEvent event) {
5162 }
5263
5364 log .info ("Starting ElasticSearch Indexing, Lecture Data" );
54- Pageable pageable = PageRequest .of (0 , CHUNK_SIZE );
55- Page <Lecture > page ;
65+ int pageNumber = 0 ;
5666 int totalProcessed = 0 ;
67+ List <Lecture > lectures ;
5768
5869 do {
59- page = lectureRepository .findAllPaged (pageable );
60- List <LectureDocument > documents = page .getContent ().stream ()
61- .map (lectureDocumentConverter ::convertToDocument )
62- .toList ();
70+ Pageable pageable = PageRequest .of (pageNumber , CHUNK_SIZE );
71+ lectures = lectureRepository .findAllPaged (pageable );
6372
64- lectureElasticRepository .saveAll (documents );
65- totalProcessed += documents .size ();
73+ if (!lectures .isEmpty ()) {
74+ List <LectureDocument > documents = lectures .stream ()
75+ .map (lectureDocumentConverter ::convertToDocument )
76+ .toList ();
6677
67- log .info ("[indexAllLectures] Indexed Lecture: {}, Total: {})" , documents .size (), totalProcessed );
68- pageable = page .nextPageable ();
69- } while (page .hasNext ());
78+ lectureElasticRepository .saveAll (documents );
79+ totalProcessed += documents .size ();
7080
71- log .info ("ElasticSearch 인덱싱 성공. Total Indexed : {}" , totalProcessed );
72- }
81+ log .info ("[indexAllLectures] Indexed Lecture: {}, Total : {}" , documents . size () , totalProcessed );
82+ }
7383
84+ pageNumber ++;
85+ } while (lectures .size () == CHUNK_SIZE );
7486
87+ log .info ("ElasticSearch 인덱싱 성공. Total Indexed: {}" , totalProcessed );
88+ }
7589}
0 commit comments