HibernateSearch学习1

Preparing and executing a query consists
of four simple steps:
• Creating a FullTextSession
• Creating a Lucene query
• Wrapping the Lucene query using a org.hibernate.Query
• Executing the search by calling for example list() or scroll()
To access the querying facilities, you have to use an FullTextSession. This Search specific
session wraps a regular org.hibernate.Session to provide query and indexing capabilities.

Creating a FullTextSession
Session session = sessionFactory.openSession();
...
FullTextSession fullTextSession = Search.getFullTextSession(session);

Creating a Lucene query
org.apache.lucene.queryParser.QueryParser parser =
new QueryParser("title", new StopAnalyzer() );
org.apache.lucene.search.Query luceneQuery = parser.parse( "summary:Festina Or
brand:Seiko" );
org.hibernate.Query fullTextQuery =
fullTextSession.createFullTextQuery( luceneQuery );
List result = fullTextQuery.list(); //return a list of managed objects

Defining pagination for a search query
org.hibernate.Query fullTextQuery =
fullTextSession.createFullTextQuery( luceneQuery, Customer.class );
fullTextQuery.setFirstResult(15); //start from the 15th element
fullTextQuery.setMaxResults(10); //return 10 elements

Specifying a Lucene Sort in order to sort the results
org.hibernate.search.FullTextQuery query = s.createFullTextQuery( query,
Book.class );
org.apache.lucene.search.Sort sort = new Sort(new SortField("title"));
query.setSort(sort);
List results = query.list();

你可能感兴趣的:(java,apache,Hibernate,Lucene,Access)