HibernateSearch学习2
Using projection instead of returning the full domain object
org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery,Book.class );query.setProjection( "id", "summary", "body", "mainAuthor.name" );List results = query.list();Object[] firstResult = (Object[]) results.get(0);Integer id = firstResult[0];String summary = firstResult[1];String body = firstResult[2];String authorName = firstResult[3];
org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery,Book.class );query.setProjection( "title", "mainAuthor.name" );query.setResultTransformer(new StaticAliasToBeanResultTransformer( BookView.class, "title", "author" ));List<BookView> results = (List<BookView>) query.list();for(BookView view : results) {log.info( "Book: " + view.getTitle() + ", " + view.getAuthor() );}
FullTextSession fullTextSession = Search.getFullTextSession(session);Transaction tx = fullTextSession.beginTransaction();Object customer = fullTextSession.load( Customer.class, 8 );fullTextSession.index(customer);tx.commit(); //index only updated at commit time
FullTextSession fullTextSession = Search.getFullTextSession(session);Transaction tx = fullTextSession.beginTransaction();for (Customer customer : customers) {fullTextSession.purge( Customer.class, customer.getId() );}tx.commit(); //index is updated at commit time
FullTextSession fullTextSession = Search.getFullTextSession(session);Transaction tx = fullTextSession.beginTransaction();fullTextSession.purgeAll( Customer.class );//optionally optimize the index//fullTextSession.getSearchFactory().optimize( Customer.class );tx.commit(); //index changes are applied at commit time