<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Query Suggestion Not Woking in Liferay 7</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=111934145" />
  <subtitle>Query Suggestion Not Woking in Liferay 7</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=111934145</id>
  <updated>2026-04-04T04:02:23Z</updated>
  <dc:date>2026-04-04T04:02:23Z</dc:date>
  <entry>
    <title>RE: Query Suggestion Not Woking in Liferay 7</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111960487" />
    <author>
      <name>Andrew Jardine</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111960487</id>
    <updated>2019-01-08T15:13:39Z</updated>
    <published>2019-01-08T15:13:39Z</published>
    <summary type="html">&lt;p&gt;Hi Aastha,&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;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. &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;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. &lt;/p&gt;</summary>
    <dc:creator>Andrew Jardine</dc:creator>
    <dc:date>2019-01-08T15:13:39Z</dc:date>
  </entry>
  <entry>
    <title>RE: Query Suggestion Not Woking in Liferay 7</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111954685" />
    <author>
      <name>Aastha Saxena</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111954685</id>
    <updated>2019-01-08T07:06:16Z</updated>
    <published>2019-01-08T07:06:16Z</published>
    <summary type="html">&lt;p&gt;Hi Andrew,&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;I am using Liferay 7.0 CE version.&lt;/p&gt;
&lt;p&gt;And I am trying to implement  &amp;quot;type ahead &amp;quot; 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.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Aastha&lt;/p&gt;</summary>
    <dc:creator>Aastha Saxena</dc:creator>
    <dc:date>2019-01-08T07:06:16Z</dc:date>
  </entry>
  <entry>
    <title>RE: Query Suggestion Not Woking in Liferay 7</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111953280" />
    <author>
      <name>Andrew Jardine</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111953280</id>
    <updated>2019-01-07T21:59:29Z</updated>
    <published>2019-01-07T21:59:29Z</published>
    <summary type="html">&lt;p&gt;Hi Aastha&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;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?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;My next question for you would be to clarify what you are trying to
  acheive. Are you basically trying to do a &amp;quot;type ahead&amp;quot; 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 &amp;quot;suggestions&amp;quot; on the search results page?&lt;/p&gt;</summary>
    <dc:creator>Andrew Jardine</dc:creator>
    <dc:date>2019-01-07T21:59:29Z</dc:date>
  </entry>
  <entry>
    <title>Query Suggestion Not Woking in Liferay 7</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111934144" />
    <author>
      <name>Aastha Saxena</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111934144</id>
    <updated>2019-01-07T05:30:47Z</updated>
    <published>2019-01-07T05:30:47Z</published>
    <summary type="html">&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;My goal is to get suggested keywords when user is typing in the
  search input field. Like if user types &amp;quot;New&amp;quot; , words like
  &amp;quot;Newspaper&amp;quot;,&amp;quot;News&amp;quot;,&amp;quot;Newton&amp;quot; etc should
  be displayed in the drop down which will have some search results.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;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    .&lt;/p&gt;
&lt;p&gt;Then when user types some characters we are getting those saved
  suggestion from our previous successful search  using query Suggestor
  for a given keyword.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Below are the steps I followed.&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;Step 1:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Once my search is successful and number of docs in Hits more than
  threshold  (size &amp;gt;1) then Indexed my keyword &amp;quot;news&amp;quot; with
  below code &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;

&lt;p&gt;_indexWriterHelper.indexKeyword(companyId,&amp;quot;news&amp;quot;,0,SuggestionConstants.TYPE_QUERY_SUGGESTION, locale);&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;Step 2:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;on checking Elastic engine server I found my keyword is indexed as below:&lt;/p&gt;
&lt;p&gt;
  &lt;a&gt;url:http://localhost:9200/liferay-20115/_search?pretty&amp;amp;q=news&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&amp;quot;hits&amp;quot; : [ {&lt;/p&gt;
&lt;p&gt;&amp;quot;_index&amp;quot; : &amp;quot;liferay-20115&amp;quot;,&lt;/p&gt;
&lt;p&gt;&amp;quot;_type&amp;quot; : &amp;quot;&lt;strong&gt;querySuggestion&lt;/strong&gt;&amp;quot;,&lt;/p&gt;
&lt;p&gt;&amp;quot;_id&amp;quot; : &amp;quot;20115_spellCheckWord_6RitQgdCR1qG3k8CzKTjdw==&amp;quot;,&lt;/p&gt;
&lt;p&gt;&amp;quot;_score&amp;quot; : 1.9425526,&lt;/p&gt;
&lt;p&gt;&amp;quot;_source&amp;quot; : {&lt;/p&gt;
&lt;p&gt;&amp;quot;uid&amp;quot; : &amp;quot;20115_spellCheckWord_6RitQgdCR1qG3k8CzKTjdw==&amp;quot;,&lt;/p&gt;
&lt;p&gt;&amp;quot;companyId&amp;quot; : &amp;quot;20115&amp;quot;,&lt;/p&gt;
&lt;p&gt;&amp;quot;groupId&amp;quot; : &amp;quot;0&amp;quot;,&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;&amp;quot;keywordSearch_en_US&amp;quot; : &amp;quot;news&amp;quot;,&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&amp;quot;priority&amp;quot; : &amp;quot;0.0&amp;quot;,&lt;/p&gt;
&lt;p&gt;&amp;quot;spellCheckWord&amp;quot; : &amp;quot;true&amp;quot;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;Step 3:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Then tried to find the indexed keyword with below code:&lt;/p&gt;
&lt;p&gt;field = &amp;quot;keywordSearch_en_US&amp;quot;;&lt;/p&gt;
&lt;p&gt;keyword = &amp;quot;new&amp;quot;;&lt;/p&gt;
&lt;p&gt;TermSuggester termSuggester = new
  TermSuggester(&amp;quot;termSuggester&amp;quot;, field, keyword);&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;/** Method 1 using QuerySuggester of
    com.liferay.portal.kernel.search.suggest.QuerySuggester */&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;SuggesterResults suggesters1 = _querySuggester.suggest(searchContext, termSuggester);&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Collection&amp;lt;SuggesterResult&amp;gt; suggesterResults = suggesters1.getSuggesterResults();&lt;/p&gt;
&lt;p&gt;if (suggesterResults != null) {&lt;/p&gt;
&lt;p&gt;for (SuggesterResult suggesterResult : suggesterResults) {&lt;/p&gt;
&lt;p&gt;for (Entry entry : suggesterResult.getEntries()) {&lt;/p&gt;
&lt;p&gt;for (Option option : entry.getOptions()) {&lt;/p&gt;
&lt;p&gt;if (!suggestions.contains(option.getText())) {&lt;/p&gt;
&lt;p&gt;suggestions.add(option.getText());&lt;/p&gt;
&lt;p&gt;} } } } }&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt; /** Method 2 using indexSearcher directly of com.liferay.portal.kernel.search.IndexSearcher*/&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;SearchEngine searchEngine =
  SearchEngineHelperUtil.getSearchEngine(searchContext.getSearchEngineId());&lt;br /&gt;
   IndexSearcher indexSearcher = searchEngine.getIndexSearcher();&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;SuggesterResults suggesters2 = indexSearcher.suggest(searchContext, termSuggester);&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;suggesterResults = suggesters2.getSuggesterResults();&lt;/p&gt;
&lt;p&gt;if (suggesterResults != null) {&lt;/p&gt;
&lt;p&gt;for (SuggesterResult suggesterResult : suggesterResults) {&lt;/p&gt;
&lt;p&gt;for (Entry entry : suggesterResult.getEntries()) {&lt;/p&gt;
&lt;p&gt;for (Option option : entry.getOptions()) {&lt;/p&gt;
&lt;p&gt;if (!suggestions.contains(option.getText())) {&lt;/p&gt;
&lt;p&gt;suggestions.add(option.getText());&lt;/p&gt;
&lt;p&gt;} } } } }&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;But in both the methods(method1 and Method2)  entry.getOptions() is
  coming empty list.&lt;/p&gt;
&lt;p&gt;I am not sure what should be the value of &lt;strong&gt;field
  &lt;/strong&gt;while initializing &lt;strong&gt;termSuggester&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Thanks for the response in advance.&lt;/p&gt;</summary>
    <dc:creator>Aastha Saxena</dc:creator>
    <dc:date>2019-01-07T05:30:47Z</dc:date>
  </entry>
</feed>
