Java Code Examples for org.hibernate.search.FullTextSession#clear()

The following examples show how to use org.hibernate.search.FullTextSession#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SystemService.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Async
@Transactional(propagation = Propagation.SUPPORTS)
public void reIndex() throws Exception {
	logger.info("Re-Index started");

	FullTextSession fullTextSession = Search.getFullTextSession((entityManager.unwrap(Session.class)));

	fullTextSession.setFlushMode(FlushMode.MANUAL);
	fullTextSession.setCacheMode(CacheMode.IGNORE);

	for (Class persistentClass : fullTextSession.getSearchFactory().getIndexedTypes()) {
		Transaction transaction = fullTextSession.beginTransaction();

		// Scrollable results will avoid loading too many objects in memory
		ScrollableResults results = fullTextSession.createCriteria(persistentClass)
				.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();
	}
	logger.info("Re-Index finished");
}
 
Example 2
Source File: IndexHelper.java    From document-management-system with GNU General Public License v2.0 4 votes vote down vote up
protected int doRebuildIndex() throws Exception {
	FullTextSession fullTextSession = (FullTextSession) entityManager.getDelegate();
	fullTextSession.setFlushMode(org.hibernate.FlushMode.MANUAL);
	fullTextSession.setCacheMode(org.hibernate.CacheMode.IGNORE);
	fullTextSession.purgeAll(NodeDocumentVersion.class);
	fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class);

	String query = "select ndv from NodeDocumentVersion ndv";
	ScrollableResults cursor = fullTextSession.createQuery(query).scroll();
	cursor.last();
	int count = cursor.getRowNumber() + 1;
	log.warn("Re-building Wine index for " + count + " objects.");

	if (count > 0) {
		int batchSize = 300;
		cursor.first(); // Reset to first result row
		int i = 0;

		while (true) {
			fullTextSession.index(cursor.get(0));

			if (++i % batchSize == 0) {
				fullTextSession.flushToIndexes();
				fullTextSession.clear(); // Clear persistence context for each batch
				log.info("Flushed index update " + i + " from Thread "
						+ Thread.currentThread().getName());
			}

			if (cursor.isLast()) {
				break;
			}

			cursor.next();
		}
	}

	cursor.close();
	fullTextSession.flushToIndexes();
	fullTextSession.clear(); // Clear persistence context for each batch
	fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class);

	return count;
}
 
Example 3
Source File: SearchServlet.java    From maven-framework-project with MIT License 4 votes vote down vote up
/**
 * This method contains the primary search functionality for this servlet, and is automatically invoked once for every HTTP
 * POST to the mapped URL. 
 */
@SuppressWarnings("unchecked")
@Override	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	Logger logger = LoggerFactory.getLogger(SearchServlet.class);
	
	// Get the user's search keyword(s) from CGI variables
	String searchString = request.getParameter("searchString");
	logger.info("Received searchString [" + searchString + "]");

	// Start a Hibernate session.
	Session session = StartupDataLoader.openSession();
	
	// Create a Hibernate Search wrapper around the vanilla Hibernate session
	FullTextSession fullTextSession = Search.getFullTextSession(session);

	// Begin a transaction.  This may not be strictly necessary, but is a good practice in general.
	fullTextSession.beginTransaction();

	// Create a Hibernate Search QueryBuilder for the appropriate Lucene index (i.e. the index for "App" in this case)
	QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( App.class ).get();
	
	// Use the QueryBuilder to construct a Lucene keyword query... matching the user's search keywords against the "name" 
	// and "description" fields of App, as well as "name" field of associated Device entities, and the "comments" field of
	// embedded CustomerReview objects.
	org.apache.lucene.search.Query luceneQuery = queryBuilder
		.keyword()
		.onFields("name", "description", "supportedDevices.name", "customerReviews.comments")
		.matching(searchString)
		.createQuery();
	org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, App.class);
	
	List<App> apps = hibernateQuery.list();
	logger.info("Found " + apps.size() + " apps");

	// Detach the results from the Hibernate session (to prevent unwanted interaction between the view layer 
	// and Hibernate when associated devices or embedded customer reviews are referenced)
	fullTextSession.clear();

	// Put the search results on the HTTP reqeust object
	request.setAttribute("apps", apps);

	// Close and clean up the Hibernate session
	fullTextSession.getTransaction().commit();
	session.close();
	
	// Forward the request object (including the search results) to the JSP/JSTL view for rendering
	getServletContext().getRequestDispatcher("/WEB-INF/pages/search.jsp").forward(request, response);
}
 
Example 4
Source File: SearchServlet.java    From maven-framework-project with MIT License 4 votes vote down vote up
/**
 * This method contains the primary search functionality for this servlet, and is automatically invoked once for every HTTP
 * POST to the mapped URL. 
 */
@SuppressWarnings("unchecked")
@Override	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	Logger logger = LoggerFactory.getLogger(SearchServlet.class);
	
	// Get the user's search keyword(s) from CGI variables
	String searchString = request.getParameter("searchString");
	logger.info("Received searchString [" + searchString + "]");

	// Start a Hibernate session.
	Session session = StartupDataLoader.openSession();
	
	// Create a Hibernate Search wrapper around the vanilla Hibernate session
	FullTextSession fullTextSession = Search.getFullTextSession(session);

	// Begin a transaction.  This may not be strictly necessary, but is a good practice in general.
	fullTextSession.beginTransaction();

	// Create a Hibernate Search QueryBuilder for the appropriate Lucene index (i.e. the index for "App" in this case)
	QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( App.class ).get();
	
	// Use the QueryBuilder to construct a Lucene keyword query... matching the user's search keywords against the "name" 
	// and "description" fields of App, as well as "name" field of associated Device entities, and the "comments" field of
	// embedded CustomerReview objects.
	org.apache.lucene.search.Query luceneQuery = queryBuilder
		.keyword()
		.onFields("name", "description", "supportedDevices.name", "customerReviews.comments")
		.matching(searchString)
		.createQuery();
	org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, App.class);
	
	List<App> apps = hibernateQuery.list();
	logger.info("Found " + apps.size() + " apps");

	// Detach the results from the Hibernate session (to prevent unwanted interaction between the view layer 
	// and Hibernate when associated devices or embedded customer reviews are referenced)
	fullTextSession.clear();

	// Put the search results on the HTTP reqeust object
	request.setAttribute("apps", apps);

	// Close and clean up the Hibernate session
	fullTextSession.getTransaction().commit();
	session.close();
	
	// Forward the request object (including the search results) to the JSP/JSTL view for rendering
	getServletContext().getRequestDispatcher("/WEB-INF/pages/search.jsp").forward(request, response);
}
 
Example 5
Source File: SearchServlet.java    From maven-framework-project with MIT License 4 votes vote down vote up
/**
 * This method contains the primary search functionality for this servlet, and is automatically invoked once for every HTTP
 * POST to the mapped URL. 
 */
@SuppressWarnings("unchecked")
@Override	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	Logger logger = LoggerFactory.getLogger(SearchServlet.class);
	
	// Get the user's search keyword(s) from CGI variables
	String searchString = request.getParameter("searchString");
	logger.info("Received searchString [" + searchString + "]");

	// Start a Hibernate session.
	Session session = StartupDataLoader.openSession();
	
	// Create a Hibernate Search wrapper around the vanilla Hibernate session
	FullTextSession fullTextSession = Search.getFullTextSession(session);

	// Begin a transaction.  This may not be strictly necessary, but is a good practice in general.
	fullTextSession.beginTransaction();

	// Create a Hibernate Search QueryBuilder for the appropriate Lucene index (i.e. the index for "App" in this case)
	QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( App.class ).get();
	
	// Use the QueryBuilder to construct a Lucene keyword query... matching the user's search keywords against the "name" 
	// and "description" fields of App, as well as "name" field of associated Device entities, and the "comments" field of
	// embedded CustomerReview objects.
	org.apache.lucene.search.Query luceneQuery = queryBuilder
		.keyword()
		.onFields("name", "description", "supportedDevices.name", "customerReviews.comments")
		.matching(searchString)
		.createQuery();
	org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, App.class);
	
	List<App> apps = hibernateQuery.list();
	logger.info("Found " + apps.size() + " apps");

	// Detach the results from the Hibernate session (to prevent unwanted interaction between the view layer 
	// and Hibernate when associated devices or embedded customer reviews are referenced)
	fullTextSession.clear();

	// Put the search results on the HTTP reqeust object
	request.setAttribute("apps", apps);

	// Close and clean up the Hibernate session
	fullTextSession.getTransaction().commit();
	session.close();
	
	// Forward the request object (including the search results) to the JSP/JSTL view for rendering
	getServletContext().getRequestDispatcher("/WEB-INF/pages/search.jsp").forward(request, response);
}