Full Text Search using Apache Lucene (Part-III)
In this post, I shall discuss on how to perform search on Indexed data. This is in continuation to my earlier posts Full Text Search using Apache Lucene (Part-I) and Part-II
Searching Indexed Data:
Searching is a process of looking for words in the index and finding documents that contain those words. Hibernate Search provides API methods to perform different types of search on a given keyword
Below code snippets search colums “patentName, patentNumber, inventor” for a matching keyword on Patent table.
queryBuilder
.keyword()
.onFields(“patentName”, “patentNumber”,”inventor) .matching(keyword)
.createQuery();
FullTextQuery hibernateQuery=fullTextSession.createFullTextQuery(luceneQuery, Patent.class);
Fuzziness
Seldom we need search applications to handle typos, sound ex conditions while retrieving search results. It can be achieved in lucene by creating a Fuzzy query. We can make above query to handle typos, sound ex by modifying the query as below.
luceneQuery = queryBuilder
.keyword()
.fuzzy()
.withThreshold(0.8f)
.onFields(“patentName”, “patentNumber”,”inventor) .matching(key)
.createQuery();
withThreshold( ) is used to specify the amount of fuzziness.
i. Displaying Search Results:
IndexSearcher returns an array of references to ranked search results, such as documents that match a given query. Customized paging can be built on top of this. A custom Web application or desktop application can be used to display search results.
FullTextQuery hibernateQuery=fullTextSession.createFullTextQuery(luceneQuery,
Patent.class);
hibernateQuery.setFirstResult(0);
hibernateQuery.setMaxResults(50);
Sorting Search Results
Sort sort=new Sort(new SortField(“patentName”, SortField.STRING, false));
hibernateQuery.setSort(sort);
Here false – Sort Ascending order, true Sort Descending order.
Conclusion
Lucene, a very popular open source search library from Apache, provides powerful indexing and searching capabilities for applications. It provides a simple and easy-to-use API that requires minimal understanding of the internals of indexing and searching. In this article, you learned about Lucene architecture and its core APIs.
Lucene has powered various search applications being used by many well-known Web sites and organizations. It has been ported to many other programming languages. Lucene has a large and active technical user community. If you’re looking for an easy-to-use, scalable, and high performing open-source search library, Apache Lucene is a great choice.
Please follow and like us: