/** * Create an initial Lucene index for the data already present in the * database. * This method is called when Spring's startup. */ @Override public void onApplicationEvent(final ApplicationReadyEvent event) { try { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.createIndexer().startAndWait(); } catch (InterruptedException e) { System.out.println( "An error occurred trying to build the serach index: " + e.toString()); } return; }
/** * @see ContextDAO#updateSearchIndex() */ @Override public void updateSearchIndex() { try { log.info("Updating the search index... It may take a few minutes."); Search.getFullTextSession(sessionFactory.getCurrentSession()).createIndexer().startAndWait(); GlobalProperty gp = Context.getAdministrationService().getGlobalPropertyObject( OpenmrsConstants.GP_SEARCH_INDEX_VERSION); if (gp == null) { gp = new GlobalProperty(OpenmrsConstants.GP_SEARCH_INDEX_VERSION); } gp.setPropertyValue(OpenmrsConstants.SEARCH_INDEX_VERSION.toString()); Context.getAdministrationService().saveGlobalProperty(gp); log.info("Finished updating the search index"); } catch (Exception e) { throw new RuntimeException("Failed to update the search index", e); } }
@Override public boolean rebuildLuceneIndex() { try { FullTextSession fullTextSession = Search.getFullTextSession(sessionFactory.getCurrentSession()); fullTextSession.createIndexer().startAndWait(); } catch (InterruptedException e) { return false; } return true; }
@Override protected void execute(FullTextSession fts) { try { fts.createIndexer( Book.class ).startAndWait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException( e ); } } }
/** * Rebuild the index using a MassIndexer * * @throws InterruptedException */ private void rebuildIndexes() throws InterruptedException { Session session = openSession(); try { FullTextSession fullTextSession = Search.getFullTextSession( session ); fullTextSession.createIndexer( Blog.class ).startAndWait(); } finally { session.close(); } }
@Test public void testBatchIndexing() throws Exception { SessionFactory sessionFactory = (SessionFactory) bundleContext.getService( serviceReference ); FullTextSession fullTextSession = Search.getFullTextSession( sessionFactory.openSession() ); AssertingMassIndexerProgressMonitor progressMonitor = new AssertingMassIndexerProgressMonitor( 0 ); fullTextSession.createIndexer( Muppet.class ).progressMonitor( progressMonitor ).startAndWait(); progressMonitor.assertExpectedProgressMade(); persistElmo( fullTextSession ); progressMonitor = new AssertingMassIndexerProgressMonitor( 1 ); fullTextSession.createIndexer( Muppet.class ).progressMonitor( progressMonitor ).startAndWait(); progressMonitor.assertExpectedProgressMade(); }
private void massIndexFooInstances(MassIndexerProgressMonitor monitor) throws InterruptedException { FullTextSession fullTextSession = Search.getFullTextSession( openSession() ); MassIndexer massIndexer = fullTextSession.createIndexer( Foo.class ); massIndexer.progressMonitor( monitor ); massIndexer.startAndWait(); fullTextSession.close(); }
@Test public void testIndexUpdatesViaMassIndexerProhibitedByInterceptor() throws Exception { indexTestData(); MassIndexer massIndexer = fullTextSession.createIndexer(); massIndexer.startAndWait(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( new MatchAllDocsQuery() ); assertEquals( "There should be no indexed entities, since interceptor also applied for mass indexer", 0, fullTextQuery.list().size() ); }
@Test public void testReindexedOnce() throws InterruptedException { Assert.assertEquals( 2, countBooksInIndex() ); Session session = openSession(); FullTextSession fullTextSession = Search.getFullTextSession( session ); MassIndexer massIndexer = fullTextSession.createIndexer(); massIndexer.startAndWait(); session.close(); Assert.assertEquals( 2, countBooksInIndex() ); }
private void rebuildIndexWithMassIndexer(Class<?> entityType, String tenantId) throws Exception { FullTextSession session = Search.getFullTextSession( openSessionWithTenantId( tenantId ) ); session.createIndexer( entityType ).purgeAllOnStart( true ).startAndWait(); session.close(); String indexName = getExtendedSearchIntegrator().getIndexBindings().get( entityType ) .getIndexManagerSelector().all().iterator().next().getIndexName(); assertThat( getNumberOfDocumentsInIndexByQuery( indexName, DocumentBuilderIndexedEntity.TENANT_ID_FIELDNAME, tenantId ) ).isGreaterThan( 0 ); }
@Test public void testFieldBridgeOnLazyFieldReindexTest() throws InterruptedException { prepareEntities(); verifyMatchExistsWithName( "name", "name" ); try ( Session session = openSession() ) { FullTextSession fullTextSession = Search.getFullTextSession( session ); MassIndexer massIndexer = fullTextSession.createIndexer( Root.class ); massIndexer.startAndWait(); } verifyMatchExistsWithName( "name", "name" ); }
@Test @RequiresDialect(comment = "MySQL definitely should accept Integer.MIN_VALUE", strictMatching = false, value = org.hibernate.dialect.MySQLDialect.class) public void testSetFetchSizeOnMySQL() throws InterruptedException { SearchIntegrator searchIntegrator = getExtendedSearchIntegrator(); MockErrorHandler mockErrorHandler = MassIndexerErrorReportingTest.getErrorHandler( searchIntegrator ); FullTextSession fullTextSession = MassIndexerErrorReportingTest.prepareSomeData( this ); fullTextSession.createIndexer( Book.class ).idFetchSize( Integer.MIN_VALUE ).startAndWait(); getSession().close(); String errorMessage = mockErrorHandler.getErrorMessage(); Assert.assertEquals( null, errorMessage ); }
@Test public void testLazyIndirectCollectionBridgeReindex() throws InterruptedException { prepareEntities(); verifyMatchExistsWithName( "name", "name" ); try ( Session session = openSession() ) { FullTextSession fullTextSession = Search.getFullTextSession( session ); MassIndexer massIndexer = fullTextSession.createIndexer( Root.class ); massIndexer.startAndWait(); } verifyMatchExistsWithName( "name", "name" ); }
@Test public void testAllRelevantProgressMonitoringOperationsCalled() throws InterruptedException { // let mass indexer re-index the data in the db (created in initializeData()) AssertingMassIndexerProgressMonitor monitor = new AssertingMassIndexerProgressMonitor( 10, 10 ); fullTextSession.createIndexer( LegacyCar.class ) .progressMonitor( monitor ) .startAndWait(); fullTextSession.createFullTextQuery( new MatchAllDocsQuery(), LegacyCar.class ) .getResultSize(); monitor.assertExpectedProgressMade(); }
@Test @RequiresDialect(comment = "H2 does not accept negative fetch sizes", strictMatching = true, value = org.hibernate.dialect.H2Dialect.class) public void testSetFetchSizeOnH2Fails() throws InterruptedException { SearchIntegrator searchIntegrator = getExtendedSearchIntegrator(); MockErrorHandler mockErrorHandler = MassIndexerErrorReportingTest.getErrorHandler( searchIntegrator ); FullTextSession fullTextSession = MassIndexerErrorReportingTest.prepareSomeData( this ); fullTextSession.createIndexer( Book.class ).idFetchSize( -1 ).startAndWait(); getSession().close(); String errorMessage = mockErrorHandler.getErrorMessage(); Assert.assertEquals( "HSEARCH000211: An exception occurred while the MassIndexer was fetching the primary identifiers list", errorMessage ); Throwable exception = mockErrorHandler.getLastException(); Assert.assertTrue( exception instanceof org.hibernate.exception.GenericJDBCException ); }
@Test public void testReindexingWithCompositeIds() throws InterruptedException { try ( FullTextSession fullTextSession = ftsBuilder.openFullTextSession() ) { storeTestData( fullTextSession ); } try ( FullTextSession fullTextSession = ftsBuilder.openFullTextSession() ) { fullTextSession.createIndexer().startAndWait(); } SearchFactory searchFactory = ftsBuilder.getSearchFactory(); SearchIntegrator searchIntegrator = searchFactory.unwrap( SearchIntegrator.class ); CountingErrorHandler errorHandler = (CountingErrorHandler) searchIntegrator.getErrorHandler(); assertEquals( 0, errorHandler.getTotalCount() ); }
@Test public void testMassIndexerWithProxyTest() throws InterruptedException { prepareEntities(); verifyMatchExistsWithName( "lazyEntity.name", TEST_NAME_CONTENT ); verifyMatchExistsWithName( "lazyEntity2.name", TEST_NAME_CONTENT ); FullTextSession fullTextSession = Search.getFullTextSession( openSession() ); MassIndexer massIndexer = fullTextSession.createIndexer( IndexedEmbeddedProxyRootEntity.class ); massIndexer.startAndWait(); fullTextSession.close(); verifyMatchExistsWithName( "lazyEntity.name", TEST_NAME_CONTENT ); verifyMatchExistsWithName( "lazyEntity2.name", TEST_NAME_CONTENT ); }
private void startAndWaitMassIndexing(Class<?> entityType) throws InterruptedException, IOException { FullTextSession session = Search.getFullTextSession( openSession() ); session.createIndexer( entityType ).purgeAllOnStart( true ).startAndWait(); final int numDocs; try ( IndexReader indexReader = session.getSearchFactory().getIndexReaderAccessor().open( entityType ) ) { numDocs = indexReader.numDocs(); } assertThat( numDocs ).isGreaterThan( 0 ); }
@Test public void testMassIndexer() throws Exception { // verify against index assertEquals( "At the beginning of the test there should be an indexed Bretzel", 1, countBretzelsViaIndex( em ) ); // clear index em.purgeAll( Bretzel.class ); em.flushToIndexes(); // verify Bretzel removed from index assertEquals( "The index should be empty after an purge and flush", 0, countBretzelsViaIndex( em ) ); // re-index em.createIndexer( Bretzel.class ).startAndWait(); assertEquals( "After re-indexing the bretzel should be indexed again", 1, countBretzelsViaIndex( em ) ); }
private void startAndWaitMassIndexing(Class<?>... entityTypes) throws InterruptedException { FullTextEntityManager fullTextEm = Search.getFullTextEntityManager( createEntityManager() ); fullTextEm.createIndexer( entityTypes ).purgeAllOnStart( true ).startAndWait(); int numDocs = fullTextEm.getSearchFactory().getIndexReaderAccessor().open( entityTypes ).numDocs(); fullTextEm.close(); assertThat( numDocs ).isGreaterThan( 0 ); }