Hi!
I added hibernate search to my existing project. When I start the application I have to detect whether there is already an index or not. If not I have to re-index all entities. This could be the case when I upgrade my existing application with the new hibernate search project where already some entities are persisted in the database. I could re-index all entities by using the manual indexing code like this:
Code:
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
Session session = getSession();
session.setFlushMode(FlushMode.MANUAL);
session.setCacheMode(CacheMode.IGNORE);
Transaction tx = session.beginTransaction();
ScrollableResults results = session.createCriteria(ProjectEntityImpl.class)
.setFetchSize(this.batchSize)
.scroll(ScrollMode.FORWARD_ONLY);
int index = 0;
while(results.next()) {
index++;
fullTextEntityManager.index(results.get(0));
if (index % this.batchSize == 0) {
fullTextEntityManager.flushToIndexes();
fullTextEntityManager.clear();
}
}
tx.commit();
fullTextEntityManager.getSearchFactory().optimize(ProjectEntityImpl.class);
I would do this when starting my application but only in the case my index is not existing or not up to date.
How can I detect that? By checking if there is a sub-directory for this entity in the lucene target directory? If so, can I determine if the index is empty or not?
Regards, jacquipre