I'm running an application that uses hibernate search for looking up people in the system. I run it on JBoss AS 7.1.1 and the application is based on Seam 3, Weld and JSF 2.1. It's working smoothly but after some higher load it turns out that pages that use the FullTextEntityManager
are being loaded very slowly. And in some moments the whole application is slowing down.
So it got to my mind that maybe I'm using Hibernate search incorrectly. I'm using a startup singleton to index the database:
@Singleton
@Startup
public class Initializer implements Serializable {
private static final long serialVersionUID = 1L;
@PersistenceContext
private EntityManager entityManager;
@PostConstruct
public void startup() throws Exception {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
}
}
And then I use the FullTextEntityManager
in the method that is used in an autocomplete component of PrimeFaces:
@Session Scoped
public class ... {
private QueryBuilder queryBuilder;
@Inject
private EntityManager entityManager;
@PostConstruct
public void initFulltext() {
fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);
queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get();
}
@Override
@SuppressWarnings("unchecked")
public List<Person> autocomplete(Object event) throws Exception {
if (event.toString() != null && !event.toString().trim().isEmpty()) {
org.apache.lucene.search.Query query = queryBuilder.keyword()
.onFields("username", "firstname", "lastname").matching(event.toString())
.createQuery();
FullTextQuery persistenceQuery = fullTextEntityManager.createFullTextQuery(query, Person.class);
persistenceQuery.setMaxResults(MAX_RESULTS_ON_PAGE);
return persistenceQuery.getResultList();
}
return null;
}
}
Is this a correct usage of Hibernate Search in a Java EE application? Isn't it possible that after a while hibernate search is trying to synchronize changes of entities and the Lucene index? If so, is it possible that it causes a huge impact on performance?