28 Eylül 2021 Salı

HibernateSearch MassIndexer Arayüzü

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

Örnek
Şö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
@Configuration
public class HibernateSearchIndexBuild implements ApplicationListener<ApplicationReadyEvent>{

  @Autowired
  private EntityManager entityManager;

  @Override
  @Transactional
  public 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) {
      ...
    }
  }
}

Hiç yorum yok:

Yorum Gönder