From 50fa10867b0e81ef121124e617bd7a2be47ed598 Mon Sep 17 00:00:00 2001 From: Rahul Pamnani Date: Wed, 7 Aug 2024 23:27:31 +0530 Subject: [PATCH 1/3] Add support for MassIndexer --- .../MassIndexerRestController.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java 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..88a99056bc --- /dev/null +++ b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java @@ -0,0 +1,30 @@ +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.session.SearchSession; +import org.openelisglobal.common.exception.LIMSRuntimeException; +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 { + + @PersistenceContext + EntityManager entityManager; + + @GetMapping("/reindex") + @Transactional + public void reindex() { + SearchSession searchSession = Search.session(entityManager); + try { + searchSession.massIndexer().startAndWait(); + } catch (InterruptedException e) { + throw new LIMSRuntimeException("Error reindexing entities", e); + } + } +} From 8fc6303c537f398dccc4bd5611c27fa87ea337fd Mon Sep 17 00:00:00 2001 From: Rahul Pamnani Date: Mon, 12 Aug 2024 22:35:51 +0530 Subject: [PATCH 2/3] Add MassIndexerService and modify the controller --- .../MassIndexerRestController.java | 25 +++++++-------- .../massindexer/MassIndexerService.java | 32 +++++++++++++++++++ 2 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java diff --git a/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java index 88a99056bc..34f4757c05 100644 --- a/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java +++ b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java @@ -1,11 +1,8 @@ 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.session.SearchSession; -import org.openelisglobal.common.exception.LIMSRuntimeException; +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; @@ -14,17 +11,17 @@ @RequestMapping("/rest") public class MassIndexerRestController { - @PersistenceContext - EntityManager entityManager; + @Autowired + MassIndexerService massIndexerService; @GetMapping("/reindex") - @Transactional - public void reindex() { - SearchSession searchSession = Search.session(entityManager); + public ResponseEntity reindex() { try { - searchSession.massIndexer().startAndWait(); - } catch (InterruptedException e) { - throw new LIMSRuntimeException("Error reindexing entities", e); + 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..4e49b98672 --- /dev/null +++ b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java @@ -0,0 +1,32 @@ +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(); + } +} From b4acd8ea8bfbdec92d2f27d0329f1c327188e2a9 Mon Sep 17 00:00:00 2001 From: Rahul Pamnani Date: Mon, 12 Aug 2024 22:41:16 +0530 Subject: [PATCH 3/3] Fix formatting --- .../hibernate/search/massindexer/MassIndexerService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java index 4e49b98672..6148139cfa 100644 --- a/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java +++ b/src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java @@ -3,7 +3,6 @@ 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;