Java tutorial
/* * Copyright 2008-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package almira.sample.dao; import java.util.List; import javax.persistence.Query; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.util.Version; import org.hibernate.search.MassIndexer; import org.hibernate.search.jpa.FullTextEntityManager; import org.hibernate.search.jpa.Search; import org.slf4j.Logger; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import almira.sample.domain.Catapult; import almira.sample.util.Log; /** * If you add custom functions, also create the corresponding interface. * * Here it is important to map against the 'Catapult' entity because Hibernate * cannot map the interface. * * @author Daniel TISCHER */ @Repository("catapultDaoHibernateSearch") public final class CatapultDaoHibernateSearch extends AbstractDaoHibernate<Catapult, Long> implements DaoHibernateSearch<Catapult, Long> { private static final Version LUCENE_VERSION = Version.LUCENE_33; // Injected by LogPostProcessor @Log private Logger log; // http://www.skill-guru.com/blog/2010/07/19/integrting-hibernate-search-with-a-spring-and-jpa-application/ private FullTextEntityManager getFullTextEntityManager() { return Search.getFullTextEntityManager(getEntityManager()); } @Override public void purgeIndex() { getFullTextEntityManager().purgeAll(Catapult.class); getFullTextEntityManager().getSearchFactory().optimize(Catapult.class); log.info("Purged Lucene index."); } @Override // The MassIndexer was designed for speed and is unaware of transactions. // http://docs.jboss.org/hibernate/search/3.2/reference/en/html/manual-index-changes.html @Transactional(propagation = Propagation.NOT_SUPPORTED) public void rebuildIndex() throws InterruptedException { log.info("Index rebuild started..."); MassIndexer indexer = getFullTextEntityManager().createIndexer(); indexer.purgeAllOnStart(true); indexer.optimizeOnFinish(true); indexer.startAndWait(); } @Override @SuppressWarnings("unchecked") public List<Catapult> searchByName(final String searchQuery, final int startIndex, final int fetchSize) throws ParseException { Query query = createQuery(searchQuery); query.setFirstResult(startIndex).setMaxResults(fetchSize); return query.getResultList(); } @Override public void flushToIndexes() { getFullTextEntityManager().flushToIndexes(); } @Override public long countByName(String searchQuery) throws ParseException { if (null == searchQuery) { throw new IllegalArgumentException("searchQuery can't be null"); } // TODO: not very efficient way of counting results... // Have a look at [Bauer07] page 677 (TIP) return createQuery(searchQuery).getResultList().size(); } private Query createQuery(final String searchQuery) throws ParseException { QueryParser parser = new QueryParser(LUCENE_VERSION, "name", new StandardAnalyzer(LUCENE_VERSION)); return getFullTextEntityManager().createFullTextQuery(parser.parse(searchQuery), Catapult.class); } }