/** * 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; }
/** * Build a full text capable EntityManager * The underlying EM implementation has to be Hibernate EntityManager * The created instance depends on the passed Session: closing either of them will * close both instances. They both share the same persistence context. * * @param em the entityManager instance to use * @return a FullTextEntityManager, wrapping the passed EntityManager * @throws IllegalArgumentException if passed null */ public static FullTextEntityManager getFullTextEntityManager(EntityManager em) { if ( em == null ) { throw log.getNullEntityManagerPassedToFullEntityManagerCreationException(); } else if ( em instanceof FullTextEntityManager ) { return (FullTextEntityManager) em; } else { return org.hibernate.search.Search.getFullTextSession( getSession( em ) ); } }
getFullTextEntityManager(entityManager);
@Override public boolean isDisabled() { Boolean retVal = ourDisabled; if (retVal == null) { retVal = new TransactionTemplate(myTxManager).execute(t -> { try { FullTextEntityManager em = org.hibernate.search.jpa.Search.getFullTextEntityManager(myEntityManager); em.getSearchFactory().buildQueryBuilder().forEntity(ResourceTable.class).get(); return Boolean.FALSE; } catch (Exception e) { ourLog.trace("FullText test failed", e); ourLog.debug("Hibernate Search (Lucene) appears to be disabled on this server, fulltext will be disabled"); return Boolean.TRUE; } }); ourDisabled = retVal; } assert retVal != null; return retVal; }
FullTextEntityManager em = org.hibernate.search.jpa.Search.getFullTextEntityManager(myEntityManager);
if (cs != null) { TermCodeSystemVersion csv = cs.getCurrentVersion(); FullTextEntityManager em = org.hibernate.search.jpa.Search.getFullTextEntityManager(myEntityManager); QueryBuilder qb = em.getSearchFactory().buildQueryBuilder().forEntity(TermConcept.class).get(); BooleanJunction<?> bool = qb.bool();
private List<Long> doSearch(String theResourceName, SearchParameterMap theParams, Long theReferencingPid) { FullTextEntityManager em = org.hibernate.search.jpa.Search.getFullTextEntityManager(myEntityManager);
/** * Flush search indexes, to be done after a reindex() or reindexAll() operation */ @Autowired public void flushSearchIndexes(ApplicationContext applicationContext) { EntityManagerFactory entityManagerFactory = (EntityManagerFactory) applicationContext.getBean("entityManagerFactory"); FullTextEntityManager fullTextEntityMgr = Search.getFullTextEntityManager(entityManagerFactory.createEntityManager()); fullTextEntityMgr.flushToIndexes(); } }
@Override public void compute() throws Exception { if (fullTextEntityManager == null) { fullTextEntityManager = Search.getFullTextEntityManager(manager); } computeLuceneIndexes(indexedObjects); // fullTextEntityManager.close(); }
@Override public void reset() throws Exception { if (fullTextEntityManager == null) { fullTextEntityManager = Search.getFullTextEntityManager(manager); } clearLuceneIndexes(); // fullTextEntityManager.close(); }
@Produces private FullTextEntityManager getFullTextEntityManager() { return Search.getFullTextEntityManager( em ); } }
@Produces private FullTextEntityManager getFullTextEntityManager() { return Search.getFullTextEntityManager( em ); } }
/** * 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()); } } }
@Override public boolean isDisabled() { Boolean retVal = ourDisabled; if (retVal == null) { retVal = new TransactionTemplate(myTxManager).execute(t -> { try { FullTextEntityManager em = org.hibernate.search.jpa.Search.getFullTextEntityManager(myEntityManager); em.getSearchFactory().buildQueryBuilder().forEntity(ResourceTable.class).get(); return Boolean.FALSE; } catch (Exception e) { ourLog.trace("FullText test failed", e); ourLog.debug("Hibernate Search (Lucene) appears to be disabled on this server, fulltext will be disabled"); return Boolean.TRUE; } }); ourDisabled = retVal; } assert retVal != null; return retVal; }
/** * Given an EntityManagerFactory, we'll open an EntityManager, allow some * code block to use it as a FullTextEntityManager, and finally close * the EntityManager. * @param emf The EntityManagerFactory which we'll use to create a new EntityManager * @param consumer A block of code which uses the FullTextEntityManager */ public static void withinEntityManager(EntityManagerFactory emf, Consumer<FullTextEntityManager> consumer) { EntityManager entityManager = emf.createEntityManager(); try { FullTextEntityManager fem = Search.getFullTextEntityManager( entityManager ); consumer.accept( fem ); } catch (Throwable mainError) { try { entityManager.close(); } catch (Throwable errorOnClose) { mainError.addSuppressed( errorOnClose ); } throw mainError; } entityManager.close(); }
public void rollbackedRegister() throws Exception { em.persist( newMember ); resetNewMember(); em.flush(); // force to flush the backend to send the JMS messages into the queue Search.getFullTextEntityManager( em ).flushToIndexes(); throw new RuntimeException( "Shit happens" ); }
public List<T> search(String searchTerm) throws SearchException { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); org.apache.lucene.search.Query qry; try { qry = HibernateSearchJpaTools.generateQuery(searchTerm, this.persistentClass, entityManager, defaultAnalyzer); } catch (ParseException ex) { throw new SearchException(ex); } org.hibernate.search.jpa.FullTextQuery hibQuery = fullTextEntityManager.createFullTextQuery(qry, this.persistentClass); // filter search results by owner.id value: // hibQuery.enableFullTextFilter("owned").setParameter("ownerId", owner.getId().toString()); return hibQuery.getResultList(); }
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()); } } }
/** * 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(); } } }