Auto-Complete, suggestions for Magnolia CMS with JQuery/SolR Part 2/5 ( Solr Suggestion handler )

Welcome to part 2 of this Magnolia Tutorial on creating an auto complete functionality in Magnolia CMS using JQuery and SolR.

In this part we will concentrate more specifically on implementing the suggestion/auto complete handler in SolR. Let’s open SolR’s configuration file that you can find in solr/nameofyourcore/config/solrconfig.xml, open it and paste teh following xml element some where in the file.

 <searchComponent class="solr.SpellCheckComponent" name="suggest">
    <lst name="spellchecker">
      <str name="name">suggest</str>
      <str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
      <str name="lookupImpl">org.apache.solr.spelling.suggest.fst.FSTLookup</str>
      <!-- Alternatives to lookupImpl: 
           org.apache.solr.spelling.suggest.fst.FSTLookup   [finite state automaton]
           org.apache.solr.spelling.suggest.fst.WFSTLookupFactory [weighted finite state automaton]
           org.apache.solr.spelling.suggest.jaspell.JaspellLookup [default, jaspell-based]
           org.apache.solr.spelling.suggest.tst.TSTLookup   [ternary trees]
      -->
       <str name="breakWords">true</str>
      <str name="field">content</str>  <!-- the indexed field to derive suggestions from -->
     <str name="accuracy">0.7</str>
      <float name="threshold">0.005</float>
      <float name="thresholdTokenFrequency">.0001</float>
      <str name="buildOnCommit">true</str>
      <str name="buildOnOptimize">true</str>
<!--
      <str name="sourceLocation">american-english</str>
-->
    </lst>
     <lst name="spellchecker">
    <str name="name">wordbreak</str>
    <str name="classname">solr.WordBreakSolrSpellChecker</str>
    <str name="field">spell</str>
    <str name="combineWords">true</str>
    <str name="breakWords">true</str>
    <str name="buildOnCommit">false</str>
    <int name="maxChanges">3</int>
  </lst>
  <str name="queryAnalyzerFieldType">textSpell</str>
  </searchComponent>
  <requestHandler class="org.apache.solr.handler.component.SearchHandler" name="/suggest">
    <lst name="defaults">
      <str name="spellcheck">true</str>
      <str name="spellcheck.build">true</str>
      <str name="spellcheck.dictionary">suggest</str>
      <str name="spellcheck.dictionary">wordbreak</str>
      <str name="spellcheck.onlyMorePopular">true</str>
      <str name="spellcheck.count">10</str>
      <str name="spellcheck.collate">true</str>
      <str name="spellcheck.extendedResults">true</str>
    </lst>
    <arr name="components">
      <str>suggest</str>
    </arr>
  </requestHandler>

The searchComponent is as the name indicates our search component that will look for the suggestions, to indicate that we want to use content as field for finding susggestions we use teh following line <str name="field">content</str> this tells Solr to build suggestions from what is indexed in the content field. The requestHandler acts as a servlet inside Solr and will deal with the incoming requests on /solr/suggest, we tell it to use the previously constructed dictionnary suggest, provide only the five most relevant results and try to collate them.

After restarting Solr, let’s open a browser an test our newly created component:

Copy paste the following http://localhost:8983/solr/suggest?q=ma and replace “Ma” with whatever could be relevant for your site. As for our index, which is Magnolia’s corporate website, we get the following result:

WordpressSolRMa

We effectively get 5 suggestions starting with ma related to the indexed content and the collation proposes us magnolia, everything works, we can move on to the creation of a Servlet in magnolia which will call this handler.

That will be the topic of our next part.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: