hi,
I looked at the documentation on ways to perform optimized batch indexing. However, the example uses a scrollable result set and Critera API which do not seem to have equivalents in JPA. According to my understanding, in JPA we have to use pagination to avoid the OutOfMemoryError. So I wrote the following code. Can someone confirm any issues with this approach or suggest a better approach?
Thanks
Seema
Code:
EntityManager em = entityManagerFactory.createEntityManager();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
em.getTransaction().begin();
fullTextEntityManager.purgeAll();
int maxRecords = 1000;
int startPosition = 0;
while(true) {
Query selectQuery = em.createQuery("select book from Book as book");
selectQuery.setMaxResults(maxRecords);
selectQuery.setFirstResult(startPosition);
List books = selectQuery.getResultList();
if (books.isEmpty()){
break;
}
for (Book book : books) {
fullTextEntityManager.index(book);
}
fullTextEntityManager.flushToIndexes();
em.clear();
startPosition = startPosition + books.size();
}
fullTextEntityManager.getSearchFactory().optimize(Book.class);
em.getTransaction().commit();
em.close();