private List<Post> findPosts() { try { FullTextSession fullTextSession = getFullTextSession((Session) entityManager.getDelegate()); Builder builder = new Builder(); String[] fields = new String[] { "message.text", "topic.subject" }; MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer()); builder.add(parser.parse(POST_TEXT), MUST); builder.add(new TermQuery(new Term("topic.forum.id", "0")), MUST); builder.add(new TermQuery(new Term("topic.forum.category.id", "0")), MUST); builder.add(new WildcardQuery(new Term("poster.userId", "root")), MUST); addPostTimeQuery(builder); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), Post.class); fullTextQuery.setSort(getSort()); fullTextQuery.setFirstResult(0); fullTextQuery.setMaxResults(15); @SuppressWarnings("unchecked") List<Post> posts = fullTextQuery.list(); return posts; } catch (ParseException e) { logger.severe("error"); return null; } }
@Override @SuppressWarnings("unchecked") public List<PostPO> searchByTag(Paging paigng, String tag) { FullTextSession fullTextSession = Search.getFullTextSession(super.session()); SearchFactory sf = fullTextSession.getSearchFactory(); QueryBuilder qb = sf.buildQueryBuilder().forEntity(PostPO.class).get(); org.apache.lucene.search.Query luceneQuery = qb.phrase().onField("tags").sentence(tag).createQuery(); FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery); query.setFirstResult(paigng.getFirstResult()); query.setMaxResults(paigng.getMaxResults()); Sort sort = new Sort(new SortField("id", SortField.Type.LONG, true)); query.setSort(sort); paigng.setTotalCount(query.getResultSize()); return query.list(); }
@GET @Path("/{name}") @Produces(MediaType.TEXT_HTML) public Response sayHtmlHello(@PathParam("name") String name) { String output = "<html> " + "<title>" + "Hello " + name + "</title>" + "<body><h1>" + "Hello " + name + "<br>Database Index will now be rebuilt..." + "<br>Please make sure the system is put into maintenance or else or the query will return nothing." + "</body></h1>" + "</html> "; Session session = DBUtil.getFactory().openSession(); FullTextSession fullTextSession = Search.getFullTextSession(session); try { System.out.println("@@ database re-indexing now begin..."); fullTextSession.createIndexer().startAndWait(); } catch (Exception exc) { exc.printStackTrace(); } return Response.status(200).entity(output).build(); }
@RequestMapping(method = RequestMethod.GET, value = FONDS + SLASH + "all" + SLASH) public ResponseEntity<FondsHateoas> findAllFonds( final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response, @RequestParam(name = "filter", required = false) String filter) { Session session = entityManager.unwrap(Session.class); FullTextSession fullTextSession = Search.getFullTextSession(session); QueryDescriptor query = ElasticsearchQueries.fromQueryString("title:test fonds"); List<Fonds> result = fullTextSession.createFullTextQuery(query, Fonds.class).list(); FondsHateoas fondsHateoas = new FondsHateoas((ArrayList<INikitaEntity>) (ArrayList) result); fondsHateoasHandler.addLinks(fondsHateoas, request, new Authorisation()); return ResponseEntity.status(HttpStatus.OK) .allow(CommonUtils.WebUtils.getMethodsForRequestOrThrow(request.getServletPath())) .body(fondsHateoas); }
private void handleListIndexing( Collection<? extends DomainObject<?>> list) { Session session = getSession(); if (list == null || session == null) { return; } FullTextSession fts = Search.getFullTextSession(session); Transaction tx = fts.beginTransaction(); for (DomainObject<?> obj : list) { if (obj != null) { fts.index(obj); } } tx.commit(); }
private void emptyProductIndex() throws Exception { runTX(new Callable<Void>() { @Override public Void call() throws Exception { Session session = dm.getSession(); if (session != null) { FullTextSession fullTextSession = Search .getFullTextSession(session); fullTextSession.purgeAll(Product.class); } return null; } }); }
private void emptySubscriptionIndex() throws Exception { runTX(new Callable<Void>() { @Override public Void call() throws Exception { Session session = dm.getSession(); if (session != null) { FullTextSession fullTextSession = Search .getFullTextSession(session); fullTextSession.purgeAll(Subscription.class); } return null; } }); }
/** * 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(); } }
@SuppressWarnings("unchecked") public <T> List<T> executeSearchQuery( RestSearchKey key, Class<T> typeClass, FullTextSession fullTextEntityManager, QueryBuilder queryBuilder, Map<String, Query> crtsMap ) { FullTextQuery fullTextQuery = createFullTextQuery(typeClass, fullTextEntityManager, queryBuilder, crtsMap); if(key.hasOrders()){ hibernateSearchBuilder.defineOrder(key, fullTextQuery); } hibernateSearchBuilder.definePaging(key, fullTextQuery); return fullTextQuery.list(); }
private <T> FullTextQuery createFullTextQuery(Class<T> typeClass, FullTextSession fullTextSession, QueryBuilder queryBuilder, Map<String, Query> crtsMap) { BooleanJunction<?> booleanJunction = hibernateSearchBuilder.createFinalBooleanJunction( queryBuilder, crtsMap); FullTextQuery fullTextQuery = null; if(booleanJunction == null){ fullTextQuery = fullTextSession.createFullTextQuery(queryBuilder.all().createQuery(), typeClass); } else { fullTextQuery = fullTextSession.createFullTextQuery( booleanJunction.createQuery(), typeClass); } return fullTextQuery; }
/** * Test provide and dispose. */ @Test public void testProvideDispose() { SessionFactory sessionFactory = locator.getService(SessionFactory.class); Session hibernateSession = sessionFactory.openSession(); FullTextSession ftSession = Search.getFullTextSession(hibernateSession); FulltextSearchFactoryFactory factory = new FulltextSearchFactoryFactory(ftSession); // Make sure that we can create a search factory. SearchFactory searchFactory = factory.provide(); Assert.assertNotNull(searchFactory); // Make sure we can dispose of the factory (does nothing, sadly). factory.dispose(searchFactory); if (hibernateSession.isOpen()) { hibernateSession.close(); } }
@Test public void searchTest() throws Exception { FullTextQuery ftq = createMock(FullTextQuery.class); FullTextSession fts = createMock(FullTextSession.class); SearchFactory factory = createMock(SearchFactory.class); expect(fts.getSearchFactory()).andReturn(factory); expect(factory.getAnalyzer(Item.class)).andReturn(new StandardAnalyzer(Version.LUCENE_36)); expect(fts.createFullTextQuery(isA(Query.class), eq(Item.class))).andReturn(ftq); expect(ftq.setProjection("title")).andReturn(ftq); List<Object[]> results = new ArrayList<Object[]>(); results.add(new Object[] { "The Incredibles" }); expect(ftq.list()).andReturn(results); replay(factory); replay(ftq); replay(fts); }
/** * * @param clazz */ private long reindexMassIndexer(final Class< ? > clazz) { final Session session = getSession(); 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; }
/** * Performs a search in Lucene and puts the resulting product object ids in * a corresponding map. * * @param query * the Lucene query * @param fts * the Hibernate Search FullTextSession * @param map * the map for the search results * @throws HibernateException */ private void searchViaLucene(org.apache.lucene.search.Query query, FullTextSession fts, LinkedHashMap<Long, VOService> map) throws HibernateException { FullTextQuery ftQuery = fts.createFullTextQuery(query, Product.class); ftQuery.setProjection("key"); List<?> result = ftQuery.list(); if (result != null) { for (Object item : result) { map.put((Long) ((Object[]) item)[0], null); } } }
private void handleListIndexing(Collection<? extends DomainObject<?>> list, Session session) { if (list == null || session == null) { return; } FullTextSession fts = Search.getFullTextSession(session); for (DomainObject<?> obj : list) { if (obj != null) { fts.index(obj); } } }
private void handleObjectIndexing(Object parameter, Session session) { if (parameter == null || session == null) { return; } FullTextSession fts = Search.getFullTextSession(session); fts.index(parameter); }
protected void applyQueryImpl(Query query) { EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(emf); if (em == null) { entityManager = emf.createEntityManager(); em = entityManager; } FullTextSession fullTextSession = Search.getFullTextSession(em.unwrap(Session.class)); fullTextQuery = fullTextSession.createFullTextQuery(query, entityClass); }
@Override @SuppressWarnings("unchecked") public List<Post> search(Paging paging, String q) throws Exception { FullTextSession fullTextSession = Search.getFullTextSession(super.session()); SearchFactory sf = fullTextSession.getSearchFactory(); QueryBuilder qb = sf.buildQueryBuilder().forEntity(PostPO.class).get(); org.apache.lucene.search.Query luceneQuery = qb.keyword().onFields("title","summary","tags").matching(q).createQuery(); FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery); query.setFirstResult(paging.getFirstResult()); query.setMaxResults(paging.getMaxResults()); StandardAnalyzer standardAnalyzer = new StandardAnalyzer(); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span style='color:red;'>", "</span>"); QueryScorer queryScorer = new QueryScorer(luceneQuery); Highlighter highlighter = new Highlighter(formatter, queryScorer); List<PostPO> list = query.list(); List<Post> rets = new ArrayList<>(list.size()); for (PostPO po : list) { Post m = BeanMapUtils.copy(po, 0); // 处理高亮 String title = highlighter.getBestFragment(standardAnalyzer, "title", m.getTitle()); String summary = highlighter.getBestFragment(standardAnalyzer, "summary", m.getSummary()); if (StringUtils.isNotEmpty(title)) { m.setTitle(title); } if (StringUtils.isNotEmpty(summary)) { m.setSummary(summary); } rets.add(m); } paging.setTotalCount(query.getResultSize()); return rets; }
/** * Do real indexes optimization. */ public static void optimizeIndexes() throws Exception { FullTextSession ftSession = null; Session session = null; if (optimizeIndexesRunning) { log.warn("*** Optimize indexes already running ***"); } else { optimizeIndexesRunning = true; log.debug("*** Begin optimize indexes ***"); try { session = HibernateUtil.getSessionFactory().openSession(); ftSession = Search.getFullTextSession(session); // Optimize indexes SearchFactory searchFactory = ftSession.getSearchFactory(); searchFactory.optimize(); } catch (Exception e) { throw e; } finally { optimizeIndexesRunning = false; HibernateUtil.close(ftSession); HibernateUtil.close(session); } log.debug("*** End optimize indexes ***"); } }
/** * Security is evaluated by Lucene, so query result are already pruned. This means that every node * should have its security (user and role) info stored in Lucene. This provides very quick search * but security modifications need to be recursively applied to reach every document node in the * repository. This may take several hours (or days) is big repositories. */ @SuppressWarnings("unchecked") private NodeResultSet runQueryLucene(FullTextSession ftSession, Query query, int offset, int limit) throws IOException, InvalidTokenOffsetsException, HibernateException { log.debug("runQueryLucene({}, {}, {}, {})", new Object[]{ftSession, query, offset, limit}); List<NodeQueryResult> results = new ArrayList<NodeQueryResult>(); NodeResultSet result = new NodeResultSet(); FullTextQuery ftq = ftSession.createFullTextQuery(query, NodeDocument.class, NodeFolder.class, NodeMail.class); ftq.setProjection(FullTextQuery.SCORE, FullTextQuery.THIS); ftq.enableFullTextFilter("readAccess"); QueryScorer scorer = new QueryScorer(query, NodeDocument.TEXT_FIELD); // Set limits ftq.setFirstResult(offset); ftq.setMaxResults(limit); // Highlight using a CSS style SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span class='highlight'>", "</span>"); Highlighter highlighter = new Highlighter(formatter, scorer); highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer, MAX_FRAGMENT_LEN)); for (Iterator<Object[]> it = ftq.iterate(); it.hasNext(); ) { Object[] qRes = it.next(); Float score = (Float) qRes[0]; NodeBase nBase = (NodeBase) qRes[1]; // Add result addResult(ftSession, results, highlighter, score, nBase); } result.setTotal(ftq.getResultSize()); result.setResults(results); log.debug("runQueryLucene: {}", result); return result; }
/** * Get Lucene index reader. */ @SuppressWarnings("rawtypes") private IndexReader getReader(FullTextSession session, Class entity) { SearchFactory searchFactory = session.getSearchFactory(); DirectoryProvider provider = searchFactory.getDirectoryProviders(entity)[0]; ReaderProvider readerProvider = searchFactory.getReaderProvider(); return readerProvider.openReader(provider); }
/** * Check if this uuid represents a mail node. * * Used in SearchDAO, and should exposed in other method should make Security Check */ public boolean isMail(FullTextSession ftSession, String uuid) throws HibernateException { log.debug("isMail({}, {})", ftSession, uuid); boolean ret = ftSession.get(NodeMail.class, uuid) instanceof NodeMail; log.debug("isMail: {}", ret); return ret; }
@SuppressWarnings("unchecked") public User findUserByPhoneNumber(String phoneNumber) { User user = null; if (!StringUtil.isEmpty(phoneNumber)) { Session session = DBUtil.getFactory().openSession(); FullTextSession fullTextSession = Search.getFullTextSession(session); try { // not necessary if database index is presence already //fullTextSession.createIndexer().startAndWait(); StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35); BooleanQuery finalQuery = new BooleanQuery(); QueryParser queryParser = new QueryParser(Version.LUCENE_35, UserConstants.DOMAIN_NAME_PHONE, analyzer); Query query = queryParser.parse(phoneNumber); finalQuery.add(query, Occur.MUST); org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(finalQuery); List<User> result = hibQuery.list(); if (result != null && result.size() > 0) { user = result.get(0); } else { LoggerUtil.info(this.getClass().getName(), "No user with phone number = " + phoneNumber + " can be found."); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally { fullTextSession.disconnect(); session.disconnect(); } } return user; }
private void handleObjectIndexing(Object parameter) { Session session = getSession(); if (parameter == null || session == null) { return; } FullTextSession fts = Search.getFullTextSession(session); Transaction tx = fts.beginTransaction(); fts.index(parameter); tx.commit(); }
@Override public void rebuildSearchIndexes() { // For some reason this generates an exception, something about unable // to synchronize transactions // FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em); // try // { // fullTextEntityManager.createIndexer().startAndWait(); // } // catch (InterruptedException e) { throw new RuntimeException(e); } // This alternative code (from the Hibernate Search docs) seems to work // although it generates a warning message when complete. Not going to // worry about it, this code doesn't run often. final int BATCH_SIZE = 128; org.hibernate.Session session = ((EntityManagerImpl)this.em.getDelegate()).getSession(); FullTextSession fullTextSession = Search.getFullTextSession(session); fullTextSession.setFlushMode(FlushMode.MANUAL); fullTextSession.setCacheMode(CacheMode.IGNORE); Transaction transaction = fullTextSession.beginTransaction(); //Scrollable results will avoid loading too many objects in memory ScrollableResults results = fullTextSession.createCriteria(Mail.class) .setFetchSize(BATCH_SIZE) .scroll(ScrollMode.FORWARD_ONLY); int index = 0; while (results.next()) { index++; fullTextSession.index(results.get(0)); //index each element if (index % BATCH_SIZE == 0) { fullTextSession.flushToIndexes(); //apply changes to indexes fullTextSession.clear(); //free memory since the queue is processed } } transaction.commit(); }
/** * {@inheritDoc} */ public List<T> search(String searchTerm) throws SearchException { Session sess = getSession(); FullTextSession txtSession = Search.getFullTextSession(sess); org.apache.lucene.search.Query qry; try { qry = HibernateSearchTools.generateQuery(searchTerm, this.persistentClass, sess, defaultAnalyzer); } catch (ParseException ex) { throw new SearchException(ex); } org.hibernate.search.FullTextQuery hibQuery = txtSession.createFullTextQuery(qry, this.persistentClass); return hibQuery.list(); }
/** * 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(); } }
@SuppressWarnings("unchecked") @DataProvider public List<SearchResult> search(String searchText) throws Exception { FullTextSession fullTextSession = Search.getFullTextSession(exampleDao .getSession()); if (!indexRebuilded) { indexRebuilded = true; fullTextSession.createIndexer().startAndWait(); } MultiFieldQueryParser parser = new MultiFieldQueryParser( Version.LUCENE_31, new String[] { "label", "tags", "url", "summary" }, new StandardAnalyzer(Version.LUCENE_31)); org.apache.lucene.search.Query luceneQuery = parser.parse(searchText); FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery, Example.class); query.setFirstResult(0); query.setMaxResults(100); List<SearchResult> searchResults = new ArrayList<SearchResult>(); for (Example example : (List<Example>) query.list()) { SearchResult searchResult = new SearchResult(); PropertyUtils.copyProperties(searchResult, example); searchResults.add(searchResult); } return searchResults; }
protected static boolean reindexEntityClass(Class<?> c) { String entityName = c.getName().substring( c.getPackage().getName().length() + 1); log("---Reindexing: " + entityName + "---"); long time = System.currentTimeMillis(); org.hibernate.search.FullTextSession ftSession = getFullTextSession(); try { ftSession .createIndexer(c) .progressMonitor( new org.webdsl.search.IndexProgressMonitor(2000, entityName)).batchSizeToLoadObjects(15) .threadsToLoadObjects(1).threadsForSubsequentFetching(2) .threadsForIndexWriter(1).purgeAllOnStart(true) .startAndWait(); } catch (Exception ex) { org.webdsl.logging.Logger.error( "Error during reindexing of entity: " + entityName, ex); return false; } finally { if (ftSession != null) { ftSession.close(); ftSession = null; } } time = System.currentTimeMillis() - time; log("---Done in " + time + "ms.---"); return true; }
public static void tryDropIndex() { if ("create-drop".equals(utils.BuildProperties.getDbMode())) { log("Db-mode is set to create-drop -> Clearing search indexes"); FullTextSession fts = getFullTextSession(); fts.purgeAll(Object.class); fts.getSearchFactory().optimize(); fts.flushToIndexes(); log("Clearing search indexes successful"); } }
protected FullTextSession getFullTextSession() { if( fullTextSession == null ) { fullTextSession = org.hibernate.search.Search.getFullTextSession( HibernateUtil.getCurrentSession() ); updateFullTextQuery = true; } return fullTextSession; }
public static <T> FacetingRequest toFacetingRequest(String field, String rangeAsString, Class<?> entityClass, Class<T> type, FullTextSession fts){ List<FacetRange<T>> facetRangeList = new ArrayList<FacetRange<T>>(); DocumentBuilderIndexedEntity<?> documentBuilder = ContextHelper.getSearchFactory(fts).getDocumentBuilderIndexedEntity(entityClass); Matcher matcher = p.matcher( rangeAsString ); FacetRange<T> range; T min, max; Class<?> targetClass; boolean includeMin, includeMax; while(matcher.find()){ includeMin = matcher.group(1).equals("["); min = ( matcher.group( 2 ).isEmpty() ) ? null : (T) stringToTypedObject( matcher.group( 2 ).trim(), type ); max = ( matcher.group( 3 ).isEmpty() ) ? null : (T) stringToTypedObject( matcher.group( 3 ).trim(), type ); includeMax = matcher.group(4).equals("]"); targetClass = (min != null) ? min.getClass() : max.getClass(); range = new FacetRange<T>( targetClass, min, max, includeMin, includeMax, field, documentBuilder ); facetRangeList.add(range); } FacetingRequestImpl rfr = new RangeFacetRequest<T>( facetName(field), field, facetRangeList, documentBuilder ); rfr.setSort( FacetSortOrder.RANGE_DEFINITION_ODER ); rfr.setIncludeZeroCounts( false ); rfr.setMaxNumberOfFacets( facetRangeList.size() ); return rfr; }
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; }
protected <T> boolean reindex(Session session, T entity) { boolean result = false; try { FullTextSession fullTextSession = getFullTextSession(session); fullTextSession.index(entity); result = true; } catch (Exception ex) { throw new CommonDaoException(ex); } return result; }
/** * Rebuild the search index during context initialization. * * @param servletContextEvent The context event (not really used). */ @Override public void contextInitialized( final ServletContextEvent servletContextEvent) { logger.info("Rebuilding Search Index..."); // Build the session factory. SessionFactory factory = createSessionFactory(); // Build the hibernate session. Session session = factory.openSession(); // Create the fulltext session. FullTextSession fullTextSession = Search.getFullTextSession(session); try { fullTextSession .createIndexer() .startAndWait(); } catch (InterruptedException e) { logger.warn("Search reindex interrupted. Good luck!"); logger.trace("Error:", e); } finally { // Close everything and release the lock file. session.close(); factory.close(); } }
/** * Dispose of the fulltext session if it hasn't already been closed. * * @param session The fulltext session to dispose of. */ @Override public void dispose(final FullTextSession session) { if (session != null && session.isOpen()) { logger.trace("Disposing of hibernate fulltext session."); session.close(); } }