ElasticSearch is now default search engine in Liferay 7/DXP. ElasticSearch is an advanced search engine compared to Lucene which Liferay was using in prior versions. This allows us to implement different use cases easily, for example, full-text search, analytics storage, autocomplete, spell checker, geo-distance etc. Now, we can use all these features in Liferay without the need to configure external search engine. Here I’ll show you how you can implement fuzzy search(along with did you mean functionality) using ElasticSearch in Liferay 7/DXP.
Prerequisite:
Basic knowledge about Liferay searching and indexing. You can visit Understanding Search And Indexing user guide for detailed explanation.
Liferay provides Suggester API for implementing fuzzy search using ElasticSearch. TermSuggester is used to provide suggestions for misspelled words based on specific StringDistance algorithms. In this tutorial, I’ll be showing how you can display results for a misspelled query with the help of fuzzy search. Please consider following steps to implement fuzzy search/did you mean functionality in Liferay. Here we have provided “iphene” as input yet it will search for the correct word, iphone.
Step 1: Create indexer of custom entity
Create indexer of your custom entity on which you want to implement fuzzy search. For more information about how to index custom entity, please visit Liferay documentation. (Custom Entity Indexing in liferay 7)
Step 2: Create Termsuggester object to get suggestions for a search term
Initialize the termSuggester object by passing appropriate parameters. Here field param represents the table column from which you want to find suggestions and value will be the searched term for which you want suggestions.
You can set accuracy of termSuggester between 0 to 1 based on your needs. (Lower accuracy may impact on performance).
TermSuggester termSuggester = new TermSuggester("spellCheckRequest", field, value);
termSuggester.setAccuracy(0.75F);
Step 3: Get suggestions results using indexSearcher
Create a searchContext object and set filters in the searchContext object.
Create an indexSearcher object.
Finally, you can get suggesterResults by invoking suggest method of the indexSearcher object.
Using above SuggesterResults object you can get all related suggestions for your misspelled search terms.
Sometimes you may want to directly display alternative results for misspelled search term instead of providing “Did you mean” link, in such scenario you can use Liferay’s MatchQuery API. It provides a related document for given search term instead of providing suggested words.
We use cookies to deliver personalized content, analyze trends, administer the site, track user movements on the site, and collect demographic information about our user base as a whole. Accept all cookies for the best possible experience on our website or manage your preferences.
What For?