/** * 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; }
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); try { fullTextEntityManager.createIndexer().startAndWait(); } catch (InterruptedException e) { // Exception handling }
EntityManager em = entityManagerFactory.createEntityManager(); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em); fullTextEntityManager.createIndexer().startAndWait();
FullTextEntityManager ftem=Search.getFullTextEntityManager(entityManager); ftem.createIndexer().startAndWait();
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager); try { fullTextEntityManager.createIndexer(standardEntity.class).batchSizeToLoadObjects( 1000 ) .cacheMode( CacheMode.IGNORE ).threadsToLoadObjects( 20 ).threadsForSubsequentFetching( 50 ) .startAndWait(); } catch (InterruptedException e) { e.printStackTrace(); }
/** * Regenerates the index for a given class * * @param clazz the class * @param entityManager the entity manager */ public static void reindex(Class clazz, EntityManager entityManager) { FullTextEntityManager txtentityManager = Search.getFullTextEntityManager(entityManager); MassIndexer massIndexer = txtentityManager.createIndexer(clazz); try { massIndexer.startAndWait(); } catch (InterruptedException e) { log.error("mass reindexing interrupted: " + e.getMessage()); } finally { txtentityManager.flushToIndexes(); } }
@Async public void reindexAll(int batchSize) { LOGGER.info("Start creating search index."); if(batchSize <= 0) { batchSize = DEFAULT_BATCH_INDEX_SIZE; } try { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.createIndexer(INDEX_DOMAIN_CLASSES.toArray(new Class[0])) .typesToIndexInParallel(INDEX_DOMAIN_CLASSES.size()) .batchSizeToLoadObjects(batchSize) .threadsToLoadObjects(5) .idFetchSize(150) .startAndWait(); LOGGER.info("Successfully created search index."); } catch (InterruptedException e) { LOGGER.warn("An error occurred trying to build the search index: {}", e.toString()); } } }
fullTextEntityManager.purgeAll(reindexMap.get(key)); fullTextEntityManager.flushToIndexes(); fullTextEntityManager.createIndexer(reindexMap.get(key)) .batchSizeToLoadObjects(100).cacheMode(CacheMode.NORMAL) .threadsToLoadObjects(4).startAndWait();
/** * Regenerates all the indexed class indexes * * @param async true if the reindexing will be done as a background thread * @param entityManager the entity manager */ public static void reindexAll(boolean async, EntityManager entityManager) { FullTextEntityManager txtentityManager = Search.getFullTextEntityManager(entityManager); MassIndexer massIndexer = txtentityManager.createIndexer(); massIndexer.purgeAllOnStart(true); try { if (!async) { massIndexer.startAndWait(); } else { massIndexer.start(); } } catch (InterruptedException e) { log.error("mass reindexing interrupted: " + e.getMessage()); } finally { txtentityManager.flushToIndexes(); } } }
private void indexClass(Class<?> classToBeIndexed) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); try { getFullTextEntityManager(entityManager) // .createIndexer(classToBeIndexed) // .batchSizeToLoadObjects(batchSizeToLoadObjects) // .threadsToLoadObjects(threadsToLoadObjects) // .threadsForSubsequentFetching(threadsForSubsequentFetching) // .startAndWait(); } catch (InterruptedException e) { log.warn("Interrupted while indexing " + classToBeIndexed.getSimpleName(), e); Thread.currentThread().interrupt(); } finally { stopWatch.stop(); log.info("Indexed {} in {}", classToBeIndexed.getSimpleName(), stopWatch.toString()); } } }
@POST @Path("/reindex") public Response reindex(@QueryParam("limit") Long limit) { FullTextEntityManager fullTextEm = Search.getFullTextEntityManager( em ); MassIndexer indexer = fullTextEm.createIndexer( Page.class, User.class ) .purgeAllOnStart( true ) .typesToIndexInParallel( 2 ) .batchSizeToLoadObjects( 25 ) .idFetchSize( 150 ) .threadsToLoadObjects( 10 ) .cacheMode( CacheMode.IGNORE ); // Cache is likely to do more harm than good in our case (very few relations) if ( limit != null ) { indexer.limitIndexedObjectsTo( limit ); } indexer.start(); return Response.accepted().build(); }
@TransactionAttribute(TransactionAttributeType.NEVER) public void indexConcerts() { try { Search.getFullTextEntityManager( entityManager ) .createIndexer() .batchSizeToLoadObjects( 1 ) .threadsToLoadObjects( 1 ) .transactionTimeout( 10 ) .cacheMode( CacheMode.IGNORE ) .startAndWait(); } catch (InterruptedException e) { throw new RuntimeException( e ); } } }
public boolean rebuildIndex() throws InterruptedException { FullTextEntityManager fullTextEntityManager = Search .getFullTextEntityManager( entityManager ); try { fullTextEntityManager .createIndexer() .batchSizeToLoadObjects( 30 ) .threadsToLoadObjects( 4 ) .cacheMode( CacheMode.NORMAL ) .startAndWait(); } catch (Exception e) { return false; } return true; }
@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 ); }