Giriş
Şu satırı dahil ederiz
import org.hibernate.search.MassIndexer;
massIndexer metodu - Class
Hangi sınıfların indeksleneceğini belirtmek için kullanılır
Şöyle yaparız
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.Component; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; @Transactional @Component public class Indexer { private EntityManager entityManager; private static final int THREAD_NUMBER = 4; public Indexer(EntityManager entityManager) { this.entityManager = entityManager; } public void indexPersistedData(String indexClassName) throws IndexException { try { SearchSession searchSession = Search.session(entityManager); Class<?> classToIndex = Class.forName(indexClassName); MassIndexer indexer = searchSession .massIndexer(classToIndex) .threadsToLoadObjects(THREAD_NUMBER); indexer.startAndWait(); } catch (Exception e) { ... } }Şöyle yaparız
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ApplicationRunner buildIndex(Indexer indexer) throws Exception { return (ApplicationArguments args) -> { indexer.indexPersistedData("com.mozen.springboothibernatesearch.model.Plant"); }; } }
startAndWait metodu
Burada massIndexer() metoduna sınıf ismi geçilmiyor.
Örnek
Şöyle yaparız
@Configurationpublic class HibernateSearchIndexBuild implements ApplicationListener<ApplicationReadyEvent>{@Autowiredprivate EntityManager entityManager;@Override@Transactionalpublic void onApplicationEvent(ApplicationReadyEvent event) {SearchSession searchSession = Search.session(entityManager);MassIndexer indexer = searchSession.massIndexer()
.idFetchSize(150)
.batchSizeToLoadObjects(25).threadsToLoadObjects(12);try {indexer.startAndWait();} catch (InterruptedException e) {...}}}