Where searchTerm has to have "*" in it to support partial matching.
My online tech scrapbook where I keep my notes in case I need to look them up later
Showing posts with label lucene. Show all posts
Showing posts with label lucene. Show all posts
Friday, June 1, 2012
Lucene Wildcard Search On Multiple Fields Using Query Builder
booleanJunction.must(qb.keyword().wildca rd().onField("title").andField("author").andField("description").matching(searchTerm).createQuery());
Where searchTerm has to have "*" in it to support partial matching.
Where searchTerm has to have "*" in it to support partial matching.
ClassCastException Gotcha With Hibernate Search
When using Hibernate Search, you'd think the following line will limit the results to only those matching the provided entity class:
QueryBuilder qb = fullTextSession.getSearchFactory().build QueryBuilder().forEntity(Cat.class).get();
Unfortunately, it doesn't. It won't just return Cats, if may even return Dogs if they are also indexed, which will in turn cause ClassCastExceptions.
To limit to only cats, you'd need to specify the entity class in this line:
FullTextQuery searchQuery = fullTextSession.createFullTextQuery(luce neQuery,Cat.class);
Unfortunately, it doesn't. It won't just return Cats, if may even return Dogs if they are also indexed, which will in turn cause ClassCastExceptions.
To limit to only cats, you'd need to specify the entity class in this line:
FullTextQuery searchQuery = fullTextSession.createFullTextQuery(luce
Disable Lucene With Hibernate Search
One issue with Hibernate search is that it is activated the moment the hibernate search JAR file is detected in the classpath. However, sometimes it's on the classpath for other reasons e.g. a hibernate data object that has hibernate search annotations is also on classpath.
To prevent hibernate search from being switched on, add the following line to the hibernate.cfg.xml file:
<property name="hibernate.search.autoregister_list eners">false</property>
Reference: http://docs.jboss.org/hibernate/search/3.2/r eference/en/html_single/#search-configur ation-event
To prevent hibernate search from being switched on, add the following line to the hibernate.cfg.xml file:
<property name="hibernate.search.autoregister_list
Reference: http://docs.jboss.org/hibernate/search/3.2/r
Using Lucene With Hibernate
Lucene is a powerful java-based full-text search engine. If you are using Hibernate, it is possible to use Lucene easily and transparently with Hibernate taking care of telling Lucene to update the indexes etc. Lucene searches can also be wrapped in Hibernate searches.
To use Lucene with Hibernate, download Hibernate Search: http://www.hibernate.org/subprojects/sea rch.html
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 _provider">filesystem</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:
Query
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
Subscribe to:
Posts (Atom)