Creating a custom expression provider
Before you begin
- Put search customized expression provider preprocessor and postprocessor Java code in the workspace_dir\search-logic-ext directory.
- Define the profile that uses your customized expression provider, pre-processor, or post-processor in the workspace_dir\search-config-ext\src\runtime\config\com.ibm.commerce.search\wc-search.xml configuration file.
Procedure
-
Create a class that extends from
com.ibm.commerce.search.internal.expression.provider.AbstractSearchExpressionProvider
and implements thecom.ibm.commerce.search.expression.provider.SearchExpressionProvider
interface. -
Implement the invoke that takes the
SearchCriteria
object as an input parameter. ThisSearchCriteria
object is a Java representation of the search expression, where each search criterion is stored as a control parameter object.Note: You must first call the following method to initialize its abstract class:super.invoke(SearchCriteria);
Information that is stored in these objects is processed later by the
SolrSearchExpressionProcessor
and converted into a native Solr expression for sending to the Solr server.For a list of supported control parameters, see HCL Commerce Search interface.
The following list shows the Helper methods:- Retrieving
String term = getControlParameterValue(SearchServiceConstants.CTRL_PARAM_SEARCH_TERM);
- Storing
-
addControlParameterValue( SearchServiceConstants.CTRL_PARAM_SEARCH_INTERNAL_OPTIONAL_QUERY, searchExpression);
For more information about the default expression providers, see HCL Commerce Search configuration file (wc-search.xml).
Example
In this example, you customize the Solr query sort process. In a customized post processor, you add data for Search REST responses from a database table. This data is returned in queries in addition along with the Solr query response.
Download the sample file for the .XML and .SQL files use in this example.
- Run xgift.sql against your database. For
Db2, use the following
command.
CREATE TABLE XGIFT ( CATENTRY_ID BIGINT NOT NULL, PART_NUMBER VARCHAR(256), ISGIFT SMALLINT, GIFTDESC VARCHAR(256), PRIMARY KEY (CATENTRY_ID) ); INSERT INTO XGIFT VALUES(10001, 'AuroraWMDRS-1', 1, 'This is a test gift desc for AuroraWMDRS-1'); INSERT INTO XGIFT VALUES(10002, 'AuroraWMDRS-2', 1, 'This is a test gift desc for AuroraWMDRS-2');
Note: Change the values in bold to your own cantentryID and partnumber.For Oracle databases, use the following command.CREATE TABLE XGIFT ( CATENTRY_ID NUMBER NOT NULL, PART_NUMBER VARCHAR(256), ISGIFT NUMBER(5), GIFTDESC VARCHAR(256), PRIMARY KEY (CATENTRY_ID) ); INSERT INTO XGIFT VALUES(10001, 'AuroraWMDRS-1', 1, 'This is a test gift desc for AuroraWMDRS-1'); INSERT INTO XGIFT VALUES(10002, 'AuroraWMDRS-2', 1, 'This is a test gift desc for AuroraWMDRS-2');
- Put the objects CustomizedGiftPostprocessor.java and CustomizedExpressionProvider.java under workspace_dir\search-logic-ext. CustomizedGiftPostprocessor.java extracts data from the new table and composes a Solr query response to format the Search REST response. CustomizedExpressionProvider.java rewrites the Solr query to sort by price in ascending order.
- Add the following code into your wc-search.xml under
workspace_dir\search-config-ext\src\runtime\config\com.ibm.commerce.search.
<<< <_config:profile name="IBM_findProductsByCategory" indexName="CatalogEntry"> <_config:query> <_config:provider classname="com.mycompany.search.internal.expression.provider.CustomizedExpressionProvider"/> <_config:postprocessor classname="com.mycompany.search.internal.expression.postprocessor.CustomizedGiftPostprocessor" /> </_config:query> </_config:profile> <_config:profile name="IBM_findProductByIds_Details" indexName="CatalogEntry"> <_config:query> <_config:postprocessor classname="com.mycompany.search.internal.expression.postprocessor.CustomizedGiftPostprocessor" /> </_config:query> </_config:profile> >>>
- Build your project and publish the changes to the server.
- Restart the Search server.
- Make a REST call to verify your post processor
customization.
Ensure that the response includeshttp://{searchhost}:{search-http-port}/search/resources/store/1/productview/byId/10001
isGift
andgiftDesc
in the return fields and that the values match the values in theXGIFT
table.
What to do next
During run time, it is possible to capture the expression that is stored in this
SelectionCriteria
object, and the final Solr expression that is sent to the Solr
server.
For instructions on how to enable tracing, see Enabling tracing and logging for HCL Commerce Search.
Enable tracing, run your scenario, and find Final Solr query
in the
trace.log file.