Introduced in Feature Pack 2

Limiting auto suggestions and spell corrections to a specific catalog

Storefront searches display auto suggestions and checks for spelling for all products by default, across the entire master catalog and all sales catalogs. If you are using extended sites, and have only a subset of the products from the master catalog in a catalog, this behavior might not be ideal. For example, a book store might contain extended sites for technical manuals and children's books. The extended site that contains technical manuals does not want auto suggestions and spell corrections to show children’s books. Complete this task to change the default auto suggest and spell corrections behavior.

Feature Pack 7 or laterNote: REST-based search deployments hard-code the auto suggest fields. For more information about limiting keyword suggestions using REST, see Enabling and working with auto suggestions.

Procedure

  1. Limiting auto suggestions:
    1. Update WebSphere Commerce search:
      1. Open the solrhome/MC_masterCatalogId/locale/CatalogEntry/conf/schema.xml file for editing.
      2. Add the following new schema field:
            
        <!--
              Store related check field
             -->  
          <dynamicField name="localSpellCheck_*" type="wc_textSpell" indexed="true" stored="false" multiValued="true" />
        
      3. Save your changes and close the file.
      4. Open the solrhome/MC_masterCatalogId/locale/CatalogEntry/conf/wc-data-config.xml file for editing.
      5. Add the following code to the header:
        
                <script><![CDATA[
                        function copyToStoreSpellCheck(row)    {
                            var nameStr = row.get('NAME');
                            var shtDescStr = row.get('SHORTDESCRIPTION');
                            var keywordStr = row.get('KEYWORD');
                            var catalogIdStr = row.get('catalog_id');
        
                            for (i=0;i<catalogIdStr.size();i++){
                                var catalogId = catalogIdStr.get(i);
                                var copyTo = new java.util.ArrayList();
        
                                copyTo.add(nameStr);
                                copyTo.add(shtDescStr);
                                copyTo.add(keywordStr);
                                row.put('localSpellCheck_'+catalogId, copyTo);
                            }
                             
                           return row;
                        }
                        
                ]]></script>
        

        This transformer code copies the product name, short description and keyword into the related spell check field.

      6. Add the new transformer value, copyToStoreSpellCheck, to the end of all the existing transformer= elements in the file.
        For example:
        
        transformer="ClobTransformer, RegexTransformer, com.ibm.commerce.solr.handler.NameValuePairTransformer, script:copyToStoreSpellCheck"
        
      7. Save your changes and close the file.
      8. Preprocess and build the search index.
    2. Update WebSphere Commerce:
      1. Open the WCDE_installdir\workspace\Stores\WebContent\AuroraStorefrontAssetStore\include\styles\style1\CachedHeaderDisplay.jsp file for editing.
      2. In the <%@ include file="../../JSTLEnvironmentSetupExtForSearch.jspf" %> block, add the following parameter:
        
        <c:param name="catalogId" value="${param.catalogId}" />
        
      3. Save your changes and close the file.
      4. Open the WCDE_installdir\workspace\Stores\WebContent\AuroraStorefrontAssetStore\Widgets\Search\AutoSuggestSerialize.jsp file for editing.
      5. Find and change the spellCheck service invoker and add the catalogId as part of the request. That is, update the following string and query values in the file:
        
        String PARAM_CATALOGID = "catalogId";
        String TERMS_FIELD = "localSpellCheck_";
        String catalogId = request.getParameter(PARAM_CATALOGID);
        
        query.addTermsField(TERMS_FIELD+catalogID);
        
        List<Term> terms = termReq.process(server).getTermsResponse().getTerms(TERMS_FIELD+catalogId);
        
      6. Save your changes and close the file.
      7. Rebuild the Stores project to apply the changes to the test server. If deployed on your WebSphere Commerce server, restart the server to apply the changes.
  2. Limiting spell corrections:
    1. Update the solrconfig.xml file to contain the new spell checker:
      1. Open the solrhome/MC_masterCatalogId/locale_name/indextype/conf/solrconfig.xml file for editing.
      2. Locate the default <lst name="spellchecker"> element and add a new spell checker that specifies a different name and location than the default.
        For example, to create a spell checker called spellcheck_catalog10001:
        
        
            <lst name="spellchecker">
              <str name="name">spellcheck_catalog10001</str>
              <str name="field">localSpellCheck_10001</str>
              <str name="classname">solr.IndexBasedSpellChecker</str>
                  <str name="buildOnCommit">true</str>
              <str name="buildOnOptimize">true</str>
              <str name="spellcheckIndexDir">./spellchecker10001</str></str>
              <!-- uncomment this to require terms to occur in 1% of the documents in order to be included in the dictionary
                  <float name="thresholdTokenFrequency">.01</float>
              -->
            </lst>
      3. Save your changes and close the file.
      4. Preprocess and build the search index.
    2. Create a custom query preprocessor that implements the newly created spellcheck.dictionary=spellcheck_catalog10001.
      Use the following logic for reference:
      
      import org.apache.solr.client.solrj.SolrQuery;
      import com.ibm.commerce.foundation.server.services.search.query.solr.AbstractSolrSearchQueryPreprocessor;
      import com.ibm.commerce.foundation.server.services.dataaccess.SelectionCriteria;
      import com.ibm.commerce.foundation.server.services.search.SearchServiceConstants;
      import com.ibm.commerce.foundation.server.services.search.query.SearchQueryPreprocessor;
      
      public class RelevancyQuerySpellCheckPreprocessor extends AbstractSolrSearchQueryPreprocessor implements
              SearchQueryPreprocessor {
      
          public RelevancyQuerySpellCheckPreprocessor(String str) {
              super();
          }
      
          public void invoke(SelectionCriteria selectionCriteria,
                  Object... queryRequestObjects) throws RuntimeException {
              super.invoke(selectionCriteria, queryRequestObjects);
              boolean isSpellcheck = false;
              SolrQuery iQuery = (SolrQuery) queryRequestObjects[0];
              String[] spellChecks = iQuery.getParams("spellcheck");
              String searchTerm = getControlParameterValue(SearchServiceConstants.CTRL_PARAM_SEARCH_TERM);
              
              if(spellChecks!=null && spellChecks.length == 1){
                  isSpellcheck = Boolean.parseBoolean(spellChecks[0]);
              }
                  
              if(isSpellcheck && searchTerm != null && searchTerm.trim().length() > 0){
                  iSolrQuery.setParam("spellcheck.dictionary", searchTerm);
              }
                  
      
          }
      
      }
      

      For more information, see Creating a custom query preprocessor.