Create a custom expression provider

An expression provider implements custom business logic for modifying the main search expression before it is sent to the search engine. Each expression provider can be separately registered to any search profile query section so that it can be reused for other search operations. That is, a search profile defines a list of search providers used for assembling the main expression for a search request.


Before beginning


Procedure

  1. Create a class that extends from...

      com.ibm.commerce.foundation.server.services.search.expression.solr.AbstractSolrSearchExpressionProvider

    ...and and implements the interface...

      com.ibm.commerce.foundation.server.services.search.expression.SearchExpressionProvider

  2. Implement the constructor method, which takes a component identifier as a string input parameter. Typically, component-specific logic is initialized here.

  3. Implement the invoke that takes the SelectionCriteria object as an input parameter. This SelectionCriteria object is a Java representation of the search expression, where each search criteria is stored as a control parameter object.

    Note: We must first call the following method to initialize its abstract class:

      super.invoke(selectionCriteria);

    Information 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 WebSphere 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 WebSphere 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.

SearchExpressionSample.

  1. Run xgift.sql against the 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');

    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');
      

  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.

  3. 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>
      >>> 

  4. Build your project and publish the changes to the server.

  5. Restart the Search server.

  6. Make a REST call to verify your post processor customization.

      http://{searchhost}:{search-http-port}/search/resources/store/1/productview/byId/10001

    Ensure that the response includes isGift and giftDesc in the return fields and that the values match the values in the XGIFT table.


What to do next

During run time, it is possible to capture the expression 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 Enable tracing and logging for WebSphere Commerce Search.

Enable tracing, run your scenario, and find Final Solr query in the trace.log file.