Skip to content

Commit

Permalink
Merge pull request #1220 from rahul6603/massindexer
Browse files Browse the repository at this point in the history
Add support for manual re-indexing using MassIndexer
  • Loading branch information
mozzy11 authored Aug 13, 2024
2 parents 8053b98 + a26f937 commit f9bd77e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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());
}
}
}
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();
}
}

0 comments on commit f9bd77e

Please sign in to comment.