-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1220 from rahul6603/massindexer
Add support for manual re-indexing using MassIndexer
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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()); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/openelisglobal/hibernate/search/massindexer/MassIndexerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |