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.
.onFields(“patentName”, “patentNumber”,”inventor) .matching(keyword)
FullTextQuery hibernateQuery=fullTextSession.createFullTextQuery(luceneQuery, Patent.class);
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
.onFields(“patentName”, “patentNumber”,”inventor) .matching(key)
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.
Sorting Search Results
Sort sort=new Sort(new SortField(“patentName”, SortField.STRING, false));
Here false – Sort Ascending order, true Sort Descending order.
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.