To use Lucene with Hibernate, download Hibernate Search: http://www.hibernate.org/subprojects/sea
A copy of the Lucene libraries is also provided.
Setup
By default, Hibernate Search will be enabled once Hibernate detects that it is on the classpath.
Any additional configuration can be done in the hibernate.cfg.xml file. E.g.
<property name="hibernate.search.default.directory
<property name="hibernate.search.default.indexBase" >/var/lucene/indexes</property>
These tell Lucene to use the filesystem based index directory and the folder to store the indexes.
Searchable Fields
To tell Hibernate to auto-index classes & fields, mark classes with the annotation "@Indexed", and fields with "@Field", and the primary key as "@DocumentId".
All subsequent updates to the table will be indexed (Note: prior entries, if any, will have to be manually indexed).
Manual Indexing
A one-time manual indexing can be done by calling:
FullTextSession fts = Search.getFullTextSession(session);
fts.createIndexer().startAndWait();
Query
FullTextSession fts = Search.getFullTextSession(session);
QueryBuilder qb = fts.getSearchFactory().buildQueryBuilder( ).forEntity(Book.class).get();
org.apache.lucene.search.Query luceneSearchQuery = qb.keyword().onFields("author").matching(s earchParam).createQuery();
FullTextQuery hibernateQuery = fts.createFullTextQuery(luceneSearchQuer y);
List<Book> results = hibernateQuery.list(); // same paradigm as Hibernate querying
References
Hibernate Search Manual: http://docs.jboss.org/hibernate/stable/s earch/reference/en-US/html/index.html
References
Hibernate Search Manual: http://docs.jboss.org/hibernate/stable/s
No comments:
Post a Comment