[opencms-dev] CmsSolrIndex and suggestions

Tobias Karrer kartobi at gmail.com
Wed Jun 25 12:49:37 CEST 2014


I for the moment solved this myself by copying CmsSolrIndex.java to my 
java classes an added the following method:
     public List<String> suggestions(String requestHandler, String term, 
String type) {
         List<String> resultList = new ArrayList<>();
         try {
             term = Encoding.encodeISO8859toUTF8(term);
         } catch (UnsupportedEncodingException e) {
             LOG.error("could not encode search query to UTF-8", e);
         }
         ModifiableSolrParams params = new ModifiableSolrParams();
         params.set("qt", requestHandler);
         params.set("q", term);
         params.set("spellcheck", "on");
         params.set("fq", "type:" + type);
         try {
             QueryResponse rsp = m_solr.query(params);
             SpellCheckResponse scresp = rsp.getSpellCheckResponse();
             if (null != scresp) {
                 List<SpellCheckResponse.Suggestion> suggestions = 
scresp.getSuggestions();
                 if (null != suggestions) {
                     for (SpellCheckResponse.Suggestion sug : suggestions) {
resultList.addAll(sug.getAlternatives());
                     }
                 }
             }
         } catch (Exception e) {
             LOG.error("Error retrieving suggestions for term " + term, e);
         }
         return resultList;
     }

The passed request handler needs to be defined in solrconfig.xml:
<searchComponent class="solr.SpellCheckComponent" name="suggest_de">
     <lst name="spellchecker">
       <str name="name">suggestDictionaryDe</str>
       <str 
name="classname">org.apache.solr.spelling.suggest.Suggester</str>
       <str 
name="lookupImpl">org.apache.solr.spelling.suggest.fst.FSTLookupFactory</str>
       <str name="field">suggest_de</str>
       <float name="threshold">0.</float>
       <str name="buildOnCommit">true</str>
     </lst>
   </searchComponent>
   <requestHandler name="/suggest_de" 
class="org.apache.solr.handler.component.SearchHandler">
     <lst name="defaults">
       <str name="echoParams">none</str>
       <str name="indent">false</str>
       <str name="spellcheck">true</str>
       <str name="spellcheck.dictionary">suggestDictionaryDe</str>
       <str name="spellcheck.onlyMorePopular">true</str>
       <str name="spellcheck.count">16</str>
       <str name="spellcheck.collate">false</str>
       </lst>
     <arr name="components">
       <str>suggest_de</str>
     </arr>
   </requestHandler>

and the configured field "<str name="field">suggest_de</str>" is mapped 
in schema.xml:
<copyField source="content_de" dest="suggest_de"/>

Best,
Tobias

Am 18.06.2014 08:16, schrieb Tobias Karrer:
> CmsSolrIndex and suggestions
> Hi all,
>
> does someone know how to return "suggestions" like it is described in
> http://wiki.apache.org/solr/Suggester on CmsSolrIndex?
>
> Best,
> Tobias
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev
>
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20140625/61922632/attachment.htm>


More information about the opencms-dev mailing list