Home » Featured, Headline, Software Development

Configuring autosuggest in Solr and SilverStripe CMS

25 June 2013 No Comment

This post is about configuring autosuggest on Solr search using SilverStripe CMS. Solr is a powerfull search tool and it has a Suggester component which is used for autosuggesting purposes.

Below are steps for configuring autosuggest.

1. On the page that extends SolrIndex, add the following functions

   public function getFieldDefinitions() {
	$xml = parent::getFieldDefinitions();

	$xml .= "\n\n\t\t<!-- Custom fields for auto suggest -->";
	$xml .= "\n\t\t<field name='spell' type='suggest' indexed='true' stored='false' multiValued='true' omitNorms='false' omitTermFreqAndPositions='false' />";
		
	return $xml;
    }

    public function getCopyFieldDefinitions() {
	$xml = parent::getCopyFieldDefinitions();

	// copy autosuggest fields to 'spell' field type
	$xml .= "\n\t\t<copyField source='SiteTree_Title' dest='spell' />";
	$xml .= "\n\t\t<copyField source='Page_CommonName' dest='spell' />"; 
		
	return $xml;
    }

Add the below xml to the solrconfig.xml at /mysite/conf/extras/.

  <!-- Auto-suggest component using Solr -->  
  <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.tst.TSTLookup</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="field">spell</str>
      <float name="threshold">0.005</float>
      <str name="buildOnCommit">true</str>
	  <bool name="exactMatchFirst">true</bool>
	  <str name="queryAnalyzerFieldType">suggest</str>
    </lst>
  </searchComponent>
  
  <requestHandler class="org.apache.solr.handler.component.SearchHandler" name="/suggest">
    <lst name="defaults">
      <str name="spellcheck">true</str>
      <str name="spellcheck.dictionary">suggest</str>
      <str name="spellcheck.onlyMorePopular">true</str>
      <str name="spellcheck.count">5</str>
     <str name="spellcheck.collate">false</str>
    </lst>
    <arr name="components">
      <str>suggest</str>
    </arr>
  </requestHandler>

Run a dev/tasks/Solr_Configure to build the new schema.xml and config file in the solrindex directory.
Check to see that the above xml is written to solrconfig.xml file. Also make sure that schema.xml in the solrindex directory contains the above fields defined in getFieldDefinitions() and getCopyFieldDefinitions() methods.

Now run a dev/tasks/Solr_Reindex to build new indexes.

Browse the url, http://{solrserver:port}/{solrindex}/suggest?q=drivi and view xml generated suggestions.

For more auto suggest tweaking, consult the following article.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image