We used cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. What For?

« Back to Blogs

Liferay 7/DXP Fuzzy Search Using ElasticSearch

Liferay DXP ElasticSearch


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.


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);
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.

SearchContext searchContext = SearchContextFactory.getInstance(httpRequest);
String[] classNames = new String[]{DealBuilder.class.getName()};

SearchEngine searchEngine = SearchEngineHelperUtil.getSearchEngine(searchContext.getSearchEngineId());
IndexSearcher indexSearcher = searchEngine.getIndexSearcher();
SuggesterResults suggesterResults = indexSearcher.suggest(searchContext, termSuggester);


Using above SuggesterResults object you can get all related suggestions for your misspelled search terms.

implement fuzzy search using ElasticSearch in Liferay DXP


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.

contact-us Request a callback WhatsApp