I'm developing a Webapplication with Spring Boot and Spring Data Jpa with Hibernate. Due to the need of a full-text search capability I integrated the Hibernate Search Engine, but the Entities are not indexed unless I do a manual indexing by
fullTextEntityManager.createIndexer().startAndWait();
If I do the manual indexing of all Entities then everything works fine, but if I use the save()-Method of the CrudRepository then the index is not created.
Code:
public interface JobRepositoryCustom {
public List<Job> searchJobs(SearchDto searchDto);
}
Code:
@Repository
public interface JobRepository extends CrudRepository<Job, Integer>, JobRepositoryCustom {
}
Code:
public class JobRepositoryImpl implements JobRepositoryCustom {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<Job> searchJobs(SearchDto searchDto) {
List<Job> jobs;
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);
/*
try {
fullTextEntityManager.createIndexer().startAndWait();
} catch (InterruptedException e) {
System.out.println("An error occurred trying to build the serach index: " + e.toString());
}*/
QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Job.class).get();
org.apache.lucene.search.Query luceneQuery = queryBuilder.bool().must(queryBuilder.keyword().onFields("title", "introduction").matching(searchDto.getSearchTerm()).createQuery()).createQuery();
org.hibernate.search.jpa.FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Job.class);
jobs = fullTextQuery.getResultList();
return jobs;
}
}
I have searched the whole Internet for a solution and I appreciate every assistance. Thanks