Example usages of Hibernate Search for GSoC (Google Summer of Code)
These examples are created under Eclipse IDE with JDK 1.8.
This is an example to demonstrate the how to build a web application integrating
Hibernate Search feature and JPA 2.1. If you want to run the demo, you should
use MySQL database and import data from script src/main/resources/zoo.sql
.
There're 3 types of Lucene query in the demo, they're
- keyword query
- Fuzzy search
- Wildcard search
You can switch them by commenting / uncommenting lines 101 - 103 in class
io.github.mincongh.servlet.AnimalSearchServlet
.
The most basic form of search. As the name suggests, this query type searches for one or more particular words.
private Query keywordQuery(String searchString) {
return queryBuilder
.keyword()
.onFields("name", "type")
.matching(searchString)
.createQuery();
}
All entities containing the target keyword(s) will be reached.
With a fuzzy search, keywords match against fields even when they are off by one or more characters. Check wikipedia to know more about the Edit Distance.
private Query fuzzyQuery(String searchString) {
return queryBuilder
.keyword()
.fuzzy()
// .withThreshold(0.7f) // deprecated
// // use withEditDistanceUpTo(int)
.withEditDistanceUpTo(2) // default 2 (can be 0, 1, 2)
.onFields("name", "type")
.matching(searchString)
.createQuery();
}
Lucene supports single and multiple character wildcard searches within single terms (not within phrase queries)
- To perform a single character wildcard search, user the
?
symbol - To perform a multiple character wildcard search, user the
*
symbol
private Query wildcardQuery(String searchString) {
return queryBuilder
.keyword()
.wildcard()
.onFields("name", "type")
.matching(searchString)
.createQuery();
}