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.
Procedure
-
Limiting auto suggestions:
-
Update WebSphere Commerce search:
- Open the
solrhome/MC_masterCatalogId/locale/CatalogEntry/conf/schema.xml
file for editing.
- Add the following new schema
field:
<!--
Store related check field
-->
<dynamicField name="localSpellCheck_*" type="wc_textSpell" indexed="true" stored="false" multiValued="true" />
- Save your changes and close the file.
- Open the
solrhome/MC_masterCatalogId/locale/CatalogEntry/conf/wc-data-config.xml
file for editing.
- 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.
- 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"
- Save your changes and close the file.
- Preprocess and
build the search index.
-
Update WebSphere Commerce:
- Open the
WCDE_installdir\workspace\Stores\WebContent\AuroraStorefrontAssetStore\include\styles\style1\CachedHeaderDisplay.jsp
file for editing.
- In the
<%@ include file="../../JSTLEnvironmentSetupExtForSearch.jspf" %>
block, add the following
parameter:
<c:param name="catalogId" value="${param.catalogId}" />
- Save your changes and close the file.
- Open the
WCDE_installdir\workspace\Stores\WebContent\AuroraStorefrontAssetStore\Widgets\Search\AutoSuggestSerialize.jsp
file for editing.
- 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);
- Save your changes and close the file.
- 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.
-
Limiting spell corrections:
-
Update the solrconfig.xml file to contain the new spell checker:
- Open the
solrhome/MC_masterCatalogId/locale_name/indextype/conf/solrconfig.xml
file for editing.
- 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>
- Save your changes and close the file.
- Preprocess and
build the search index.
-
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.