/** * @param clazz */ private long reindexMassIndexer(final Class<?> clazz) { final Session session = sessionFactory.getCurrentSession(); final Criteria criteria = createCriteria(session, clazz, null, true); final Long number = (Long) criteria.uniqueResult(); // Get number of objects to re-index (select count(*) from). log.info("Starting (mass) re-indexing of " + number + " entries of type " + clazz.getName() + "..."); final FullTextSession fullTextSession = Search.getFullTextSession(session); try { fullTextSession.createIndexer(clazz)// .batchSizeToLoadObjects(25) // //.cacheMode(CacheMode.NORMAL) // .threadsToLoadObjects(5) // //.threadsForIndexWriter(1) // .threadsForSubsequentFetching(20) // .startAndWait(); } catch (final InterruptedException ex) { log.error("Exception encountered while reindexing: " + ex.getMessage(), ex); } final SearchFactory searchFactory = fullTextSession.getSearchFactory(); searchFactory.optimize(clazz); log.info("Re-indexing of " + number + " objects of type " + clazz.getName() + " done."); return number; }
searchFactory.optimize(clazz); log.info("Re-indexing of " + index + " objects of type " + clazz.getName() + " done."); return index;
@Test public void testFolderSize() throws Exception { File indexFolder = new File( indexBasePath ); long initialFolderSize = folderSize( indexFolder ); try ( Session session = openSession() ) { session.beginTransaction(); SearchFactory searchFactory = Search.getFullTextSession( session ).getSearchFactory(); searchFactory.optimize(); session.getTransaction().commit(); } long afterSessionClosed = folderSize( indexFolder ); closeSessionFactory(); long afterFactoryClosed = folderSize( indexFolder ); assertThat( afterSessionClosed ).isEqualTo( afterFactoryClosed ); assertThat( afterFactoryClosed ).isLessThan( initialFolderSize ); }
@Override public String process() throws Exception { boolean purgeAllOnStart = SerializationUtil.parseBooleanParameterOptional( PURGE_ALL_ON_START, serializedPurgeAllOnStart, Defaults.PURGE_ALL_ON_START ); boolean optimizeAfterPurge = SerializationUtil.parseBooleanParameterOptional( OPTIMIZE_AFTER_PURGE, serializedOptimizeAfterPurge, Defaults.OPTIMIZE_AFTER_PURGE ); if ( purgeAllOnStart ) { JobContextData jobData = (JobContextData) jobContext.getTransientUserData(); EntityManagerFactory emf = jobData.getEntityManagerFactory(); try ( Session session = PersistenceUtil.openSession( emf, tenantId ) ) { FullTextSession fts = Search.getFullTextSession( session ); jobData.getEntityTypes().forEach( clz -> fts.purgeAll( clz ) ); // This is necessary because the batchlet is not executed inside a transaction fts.flushToIndexes(); if ( optimizeAfterPurge ) { log.startOptimization(); fts.getSearchFactory().optimize(); } } } return null; } }
@Test public void testIndexReaderAccessOptimizedIndex() throws Exception { try ( Session session = openSession() ) { session.beginTransaction(); session.delete( session.load( SongWithLongTitle.class, 1L ) ); session.getTransaction().commit(); session.clear(); session.beginTransaction(); SearchFactory searchFactory = Search.getFullTextSession( session ).getSearchFactory(); try ( IndexReader indexReader = indexReader( searchFactory ) ) { assertThat( indexReader.hasDeletions() ) .as( "IndexReader should see the deletions before the optimization" ) .isTrue(); searchFactory.optimize(); session.getTransaction().commit(); } try ( IndexReader indexReader = indexReader( searchFactory ) ) { assertThat( indexReader.hasDeletions() ) .as( "IndexReader should see some deletions after optimization" ) .isFalse(); } } finally { closeSessionFactory(); } }
@Test public void testOptimize() throws Exception { FullTextSession s = Search.getFullTextSession( openSession() ); Transaction tx = s.beginTransaction(); int loop = 2000; for ( int i = 0; i < loop; i++ ) { Email email = new Email(); email.setId( (long) i + 1 ); email.setTitle( "JBoss World Berlin" ); email.setBody( "Meet the guys who wrote the software" ); s.persist( email ); } tx.commit(); s.close(); s = Search.getFullTextSession( openSession() ); tx = s.beginTransaction(); s.getSearchFactory().optimize( Email.class ); tx.commit(); s.close(); //check non indexed object get indexed by s.index s = Search.getFullTextSession( openSession() ); tx = s.beginTransaction(); QueryParser parser = new QueryParser( "id", TestConstants.stopAnalyzer ); int result = s.createFullTextQuery( parser.parse( "body:wrote" ) ).getResultSize(); assertEquals( 2000, result ); s.createQuery( "delete " + Email.class.getName() ).executeUpdate(); tx.commit(); s.close(); }
strategy.getOptimizationsPerformed() ); fullTextSession.getSearchFactory().optimize( Clock.class ); assertEquals( "Optimize should have been incremented",