RE: Query Suggestion Not Woking in Liferay 7

Aastha Saxena, modified 6 Years ago. New Member Posts: 2 Join Date: 11/13/18 Recent Posts

Hi,

 

My goal is to get suggested keywords when user is typing in the search input field. Like if user types "New" , words like "Newspaper","News","Newton" etc should be displayed in the drop down which will have some search results.

 

The approach we are using here for both keyword and query suggestions, and what Liferay offers out of the box, is query indexing. You make a succesful search and if the result count is above a defined threshold value the query gets indexed to the querySuggestions type in Elasticsearch using index Keyword    .

Then when user types some characters we are getting those saved suggestion from our previous successful search  using query Suggestor for a given keyword.

 

Below are the steps I followed.

Step 1:

Once my search is successful and number of docs in Hits more than threshold  (size >1) then Indexed my keyword "news" with below code 

 

_indexWriterHelper.indexKeyword(companyId,"news",0,SuggestionConstants.TYPE_QUERY_SUGGESTION, locale);

 

Step 2:

on checking Elastic engine server I found my keyword is indexed as below:

url:http://localhost:9200/liferay-20115/_search?pretty&q=news

 

"hits" : [ {

"_index" : "liferay-20115",

"_type" : "querySuggestion",

"_id" : "20115_spellCheckWord_6RitQgdCR1qG3k8CzKTjdw==",

"_score" : 1.9425526,

"_source" : {

"uid" : "20115_spellCheckWord_6RitQgdCR1qG3k8CzKTjdw==",

"companyId" : "20115",

"groupId" : "0",

"keywordSearch_en_US" : "news",

"priority" : "0.0",

"spellCheckWord" : "true"

}

 

Step 3:

Then tried to find the indexed keyword with below code:

field = "keywordSearch_en_US";

keyword = "new";

TermSuggester termSuggester = new TermSuggester("termSuggester", field, keyword);

/** Method 1 using QuerySuggester of com.liferay.portal.kernel.search.suggest.QuerySuggester */

SuggesterResults suggesters1 = _querySuggester.suggest(searchContext, termSuggester);

 

Collection<SuggesterResult> suggesterResults = suggesters1.getSuggesterResults();

if (suggesterResults != null) {

for (SuggesterResult suggesterResult : suggesterResults) {

for (Entry entry : suggesterResult.getEntries()) {

for (Option option : entry.getOptions()) {

if (!suggestions.contains(option.getText())) {

suggestions.add(option.getText());

} } } } }

 

 

 /** Method 2 using indexSearcher directly of com.liferay.portal.kernel.search.IndexSearcher*/

SearchEngine searchEngine = SearchEngineHelperUtil.getSearchEngine(searchContext.getSearchEngineId());
 IndexSearcher indexSearcher = searchEngine.getIndexSearcher();
 

SuggesterResults suggesters2 = indexSearcher.suggest(searchContext, termSuggester);

 

suggesterResults = suggesters2.getSuggesterResults();

if (suggesterResults != null) {

for (SuggesterResult suggesterResult : suggesterResults) {

for (Entry entry : suggesterResult.getEntries()) {

for (Option option : entry.getOptions()) {

if (!suggestions.contains(option.getText())) {

suggestions.add(option.getText());

} } } } }

 

But in both the methods(method1 and Method2)  entry.getOptions() is coming empty list.

I am not sure what should be the value of field while initializing termSuggester.

 

Thanks for the response in advance.

thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

Hi Aastha

 

First off, since we have multiple versions of Liferay 7 now, can you please clarify if you are using 7.0 or 7.1? and after that, are you using the DXP version? or are you using the CE?

 

My next question for you would be to clarify what you are trying to acheive. Are you basically trying to do a "type ahead" sort of a solution where as the user is typing in the search field you are providing a drop down of sorts with options that they can choose from? or are you providing the "suggestions" on the search results page?

Aastha Saxena, modified 6 Years ago. New Member Posts: 2 Join Date: 11/13/18 Recent Posts

Hi Andrew,

 

I am using Liferay 7.0 CE version.

And I am trying to implement  "type ahead " sort of solution where as the user is typing in the search field you are providing a drop down of sorts with options that they can choose from.

 

Regards,

Aastha

thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

Hi Aastha,

 

I have done something similar simply by creating a MVCResourceCommand that is associated with the Search portlet and in that command performing search operations. A JSP fragment for the JSP that renders the Search Input field can be used to override the JSP and attach a listener (I used AUI) for the onChange event of the field. As the users types, once the keystroke threshold is met, I invoke an AUI A.IO.Request action to call my MVCResourceCommand, do the search, return the result and render it in a drop down of sorts. 

 

All in, it only took me a couple of hours to wire it all together and test it. Sounds to me like you are looking to do the same sort of a thing. It's not suggestions based on what other people have searched for mind you -- so maybe I'm wrong. Either way though, I beleive the right Query Config will allow Elastic to return to you suggestions, so you could probably still use the same sort of a solution and simply pluck out the suggestions from the response and render those to the user.