protected void reindexClasses(FullTextEntityManager fullTextEntityManager, Set<Class<?>> entityClasses) throws InterruptedException { int batchSize = propertyService.get(HIBERNATE_SEARCH_REINDEX_BATCH_SIZE); int loadThreads = propertyService.get(HIBERNATE_SEARCH_REINDEX_LOAD_THREADS); if (LOGGER.isInfoEnabled()) { LOGGER.info("Targets for indexing job: {}", entityClasses); } for (Class<?> clazz : entityClasses) { ProgressMonitor progressMonitor = new ProgressMonitor(); Thread t = new Thread(progressMonitor); LOGGER.info(String.format("Reindexing %1$s.", clazz)); t.start(); MassIndexer indexer = fullTextEntityManager.createIndexer(clazz); indexer.batchSizeToLoadObjects(batchSize) .threadsToLoadObjects(loadThreads) .cacheMode(CacheMode.NORMAL) .progressMonitor(progressMonitor) .startAndWait(); progressMonitor.stop(); t.interrupt(); LOGGER.info(String.format("Reindexing %1$s done.", clazz)); } }
/** * Regenerates all the indexed class indexes * * @param async true if the reindexing will be done as a background thread * @param sess the hibernate session */ public static void reindexAll(boolean async, Session sess) { FullTextSession txtSession = Search.getFullTextSession(sess); MassIndexer massIndexer = txtSession.createIndexer(); massIndexer.purgeAllOnStart(true); try { if (!async) { massIndexer.startAndWait(); } else { massIndexer.start(); } } catch (InterruptedException e) { log.error("mass reindexing interrupted: " + e.getMessage()); } finally { txtSession.flushToIndexes(); } }
@Test public void testUpdateIndex(@Mocked(methods = "getFullTextEntityManager") final BookManager mockManager, @Mocked final FullTextEntityManager mockFullTextEntityManager, @Mocked final MassIndexer mockIndexer) throws Exception { new Expectations() { { mockManager.getFullTextEntityManager(); result = mockFullTextEntityManager; mockFullTextEntityManager.createIndexer(); result = mockIndexer; mockIndexer.startAndWait(); } }; bookManager.updateFullTextIndex(); }
/** * Regenerates the index for a given class * * @param clazz the class * @param sess the hibernate session */ public static void reindex(Class clazz, Session sess) { FullTextSession txtSession = Search.getFullTextSession(sess); MassIndexer massIndexer = txtSession.createIndexer(clazz); try { massIndexer.startAndWait(); } catch (InterruptedException e) { log.error("mass reindexing interrupted: " + e.getMessage()); } finally { txtSession.flushToIndexes(); } }
protected boolean reindex(Session session, Class<?> entityClass) { boolean result = false; try { LOGGER.info("reindex: [" + entityClass.getSimpleName() + "]"); FullTextSession fullTextSession = getFullTextSession(session); MassIndexer massIndexer = fullTextSession.createIndexer(entityClass); massIndexer.startAndWait(); result = true; } catch (Exception ex) { throw new CommonDaoException(ex); } return result; }
/** * Assert that the index is created on startup. * * @throws Exception Any unexpected exceptions. */ @Test @PrepareForTest({Search.class, SearchIndexContextListener.class}) public void testOnStartup() throws Exception { // Set up a fake session factory SessionFactory mockFactory = mock(SessionFactory.class); Session mockSession = mock(Session.class); when(mockFactory.openSession()).thenReturn(mockSession); // Set up a fake indexer MassIndexer mockIndexer = mock(MassIndexer.class); // Set up our fulltext session. FullTextSession mockFtSession = mock(FullTextSession.class); when(mockFtSession.createIndexer()) .thenReturn(mockIndexer); // This is the way to tell PowerMock to mock all static methods of a // given class mockStatic(Search.class); when(Search.getFullTextSession(mockSession)) .thenReturn(mockFtSession); SearchIndexContextListener listener = mock(SearchIndexContextListener.class); doReturn(mockFactory).when(listener).createSessionFactory(); doCallRealMethod().when(listener).contextInitialized(any()); // Run the test listener.contextInitialized(mock(ServletContextEvent.class)); verify(mockIndexer).startAndWait(); // Verify that the session was closed. verify(mockSession).close(); }
/** * Assert that an interrupted exception exits cleanly. * * @throws Exception An exception that might be thrown. */ @Test @PrepareForTest({Search.class, SearchIndexContextListener.class}) public void testInterruptedIndex() throws Exception { // Set up a fake session factory SessionFactory mockFactory = mock(SessionFactory.class); Session mockSession = mock(Session.class); when(mockFactory.openSession()).thenReturn(mockSession); // Set up a fake indexer MassIndexer mockIndexer = mock(MassIndexer.class); // Set up our fulltext session. FullTextSession mockFtSession = mock(FullTextSession.class); when(mockFtSession.createIndexer()) .thenReturn(mockIndexer); doThrow(new InterruptedException()) .when(mockIndexer) .startAndWait(); // This is the way to tell PowerMock to mock all static methods of a // given class mockStatic(Search.class); when(Search.getFullTextSession(mockSession)) .thenReturn(mockFtSession); SearchIndexContextListener listener = mock(SearchIndexContextListener.class); doReturn(mockFactory).when(listener).createSessionFactory(); doCallRealMethod().when(listener).contextInitialized(any()); // Run the test listener.contextInitialized(mock(ServletContextEvent.class)); // Verify that the session was closed. verify(mockSession).close(); verify(mockFactory).close(); }
public void indexDatabaseContent() { final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); final MassIndexerProgressMonitor indexMonitor = new SimpleIndexingProgressMonitor(10); try { final MassIndexer indexer = fullTextEntityManager.createIndexer(); indexer.threadsToLoadObjects(1); indexer.progressMonitor(indexMonitor); indexer.startAndWait(); } catch (InterruptedException e) { logger.error("Error indexing current database content", e); } logger.info("Succesfully indexed database content for full text search"); }
@Schedule(hour = "2") @Override public void reindexSearch() { active = true; final SubMonitor m = monitorFactory.newSubMonitor("Recreationg Searchindex"); m.start(); try { MassIndexer indexer = Search.getFullTextEntityManager(em).createIndexer(); indexer.progressMonitor(new SimpleIndexingProgressMonitor() { private final AtomicLong documentsDoneCounter = new AtomicLong(); private final AtomicLong totalCounter = new AtomicLong(); private final AtomicLong entitiesLoaded = new AtomicLong(); @Override public void entitiesLoaded(int size) { entitiesLoaded.set(size); } @Override public void addToTotalCount(long count) { super.addToTotalCount(count); totalCounter.addAndGet(count); } @Override public void documentsAdded(long increment) { super.documentsAdded(increment); long current = documentsDoneCounter.addAndGet(increment); if ( current % getStatusMessagePeriod() == 0 ) { m.message("Prozess " + current + "/" + totalCounter.get() + " | Entities loaded: " + entitiesLoaded.get()); } } }); // Values still not optimal, but the mysql db holds. indexer .batchSizeToLoadObjects(10000) .threadsToLoadObjects(3) .startAndWait(); } catch (InterruptedException ex) { LOG.error("Error on Reindex Search.", ex); } finally { active = false; } m.finish(); }