diff --git a/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java new file mode 100644 index 0000000000..34f4757c05 --- /dev/null +++ b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java @@ -0,0 +1,27 @@ +package org.openelisglobal.hibernate.search.massindexer; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/rest") +public class MassIndexerRestController { + + @Autowired + MassIndexerService massIndexerService; + + @GetMapping("/reindex") + public ResponseEntity reindex() { + try { + massIndexerService.reindex(); + return ResponseEntity.ok("Reindexing completed successfully."); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Error occurred during reindexing: " + e.getMessage()); + } + } +} diff --git a/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java new file mode 100644 index 0000000000..6148139cfa --- /dev/null +++ b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java @@ -0,0 +1,31 @@ +package org.openelisglobal.hibernate.search.massindexer; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.transaction.Transactional; +import org.hibernate.search.mapper.orm.Search; +import org.hibernate.search.mapper.orm.massindexing.MassIndexer; +import org.hibernate.search.mapper.orm.session.SearchSession; +import org.springframework.stereotype.Service; + +@Service +public class MassIndexerService { + @PersistenceContext + EntityManager entityManager; + + // parameters to allow tuning the MassIndexer for optimal performance + + private int idFetchSize = 100; + + private int batchSizeToLoadObjects = 10; + + private int threadsToLoadObjects = 6; + + @Transactional + public void reindex() throws Exception { + SearchSession searchSession = Search.session(entityManager); + MassIndexer indexer = searchSession.massIndexer(); + indexer.idFetchSize(idFetchSize).batchSizeToLoadObjects(batchSizeToLoadObjects) + .threadsToLoadObjects(threadsToLoadObjects).startAndWait(); + } +}