Class RelevancyRulesSolrQueryContributor

java.lang.Object
com.broadleafcommerce.search.provider.solr.AbstractSolrQueryContributor
com.broadleafcommerce.search.provider.solr.RelevancyRulesSolrQueryContributor
All Implemented Interfaces:
SolrQueryContributor, org.springframework.core.Ordered

public class RelevancyRulesSolrQueryContributor extends AbstractSolrQueryContributor
This contributor is responsible for applying the query parser boost parameter to the solr query. These are used to boost the results based on the relevancy rules for the provided context. Requires the eDisMax Query Parser.

This contributor is also used in type ahead requests.

Since:
Search Service 2.2.0, Release Train 2.2.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final Double
     
    protected static final Double
     

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
    RelevancyRulesSolrQueryContributor(com.broadleafcommerce.search.provider.solr.SolrFieldService solrFieldService, com.broadleafcommerce.search.core.service.relevancyrule.RelevancyRuleUtils<com.broadleafcommerce.search.api.domain.RelevancyRule> relevancyRuleUtils, com.broadleafcommerce.rulesengine.expression.service.RuleEvaluationService ruleEvaluationService, com.broadleafcommerce.rulesengine.expression.context.ExpressionContext expressionContext)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    applyMatchingQueryBoostToQueryFields(org.apache.solr.client.solrj.SolrQuery solrQuery, com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField)
    Builds the boosted query field for the RelevancyRule of type DefaultRelevancyRuleTypes.MATCHING_VALUE.
    protected void
    attachRuleBoosts(org.apache.solr.client.solrj.SolrQuery solrQuery, org.springframework.data.domain.Sort sorts, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, List<com.broadleafcommerce.search.api.domain.RelevancyRule> relevancyRules, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, String locale)
    Configure Solr result sorting with given page sort information
    protected void
    buildBoostForField(org.apache.solr.client.solrj.SolrQuery solrQuery, com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField, List<String> boostFunctions, String locale)
     
    protected Map<String,String>
    buildFilterParamsMap(List<com.broadleafcommerce.search.api.domain.Filter> filters)
    Converts the filters in a set of filters to a key/value map to be used in relevancy rule evaluation.
    protected String
    buildMatchingValueBoostFunction(com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField)
    Builds the boost function to apply for the RelevancyRule of type DefaultRelevancyRuleTypes.MATCHING_VALUE.
    protected String
    buildOrdinalBoostFunction(com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField)
    Builds the boost function to apply for the RelevancyRule of type DefaultRelevancyRuleTypes.ORDINAL.
    protected String
    buildRelativeBoostFunction(com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField)
    Builds the boost function to apply for the RelevancyRule of type DefaultRelevancyRuleTypes.RELATIVE.
    protected void
    contributeInternal(org.apache.solr.client.solrj.SolrQuery solrQuery, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, org.springframework.data.domain.Pageable page, com.broadleafcommerce.search.core.service.SearchRequestProperties properties, String locale, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
    Customize the provided solrQuery with some additional behavior.
    boolean
     
    protected Map<String,String>
     
    protected String
    determineMatchingValueBoostedFunction(com.broadleafcommerce.search.api.domain.RelevancyRule rule, String termFreqFunction)
     
    protected com.broadleafcommerce.rulesengine.expression.context.ExpressionContext
     
    protected com.broadleafcommerce.search.core.service.relevancyrule.RelevancyRuleUtils<com.broadleafcommerce.search.api.domain.RelevancyRule>
     
    protected com.broadleafcommerce.search.api.domain.FieldVariant
    getRelevancyRuleVariant(com.broadleafcommerce.search.api.domain.RelevancyRule rule, com.broadleafcommerce.search.api.domain.FieldDefinition ruleField)
    Retrieve the FieldVariant to be used for the relevancy rule.
    protected com.broadleafcommerce.rulesengine.expression.service.RuleEvaluationService
     
    protected String
    getRuleField(com.broadleafcommerce.search.api.domain.RelevancyRule rule, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, String locale)
    Returns the Solr field name for the relevancy rule.
    protected com.broadleafcommerce.search.provider.solr.SolrFieldService
     
    protected boolean
    shouldApplyRule(com.broadleafcommerce.search.api.domain.RelevancyRule rule, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, String locale)
     
    protected boolean
    shouldApplyRuleBasedOnCategoryContext(com.broadleafcommerce.search.api.domain.RelevancyRule rule, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, String locale)
     
    protected boolean
    shouldApplyRuleBasedOnSearchTerm(com.broadleafcommerce.search.api.domain.RelevancyRule rule, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, String locale)
     
    protected boolean
    shouldContribute(org.apache.solr.client.solrj.SolrQuery solrQuery, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, org.springframework.data.domain.Pageable page, com.broadleafcommerce.search.core.service.SearchRequestProperties properties, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
    Determines if this contributor should run.

    Methods inherited from class com.broadleafcommerce.search.provider.solr.AbstractSolrQueryContributor

    contribute

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.broadleafcommerce.search.provider.solr.SolrQueryContributor

    getOrder, isTypeAheadRequest
  • Field Details

    • DEFAULT_BOOST_FUNCTION

      protected static final Double DEFAULT_BOOST_FUNCTION
    • FREQUENCY_BASE_VALUE

      protected static final Double FREQUENCY_BASE_VALUE
  • Constructor Details

    • RelevancyRulesSolrQueryContributor

      public RelevancyRulesSolrQueryContributor(com.broadleafcommerce.search.provider.solr.SolrFieldService solrFieldService, com.broadleafcommerce.search.core.service.relevancyrule.RelevancyRuleUtils<com.broadleafcommerce.search.api.domain.RelevancyRule> relevancyRuleUtils, com.broadleafcommerce.rulesengine.expression.service.RuleEvaluationService ruleEvaluationService, com.broadleafcommerce.rulesengine.expression.context.ExpressionContext expressionContext)
  • Method Details

    • contributeInternal

      protected void contributeInternal(org.apache.solr.client.solrj.SolrQuery solrQuery, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, org.springframework.data.domain.Pageable page, com.broadleafcommerce.search.core.service.SearchRequestProperties properties, @Nullable String locale, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from class: AbstractSolrQueryContributor
      Customize the provided solrQuery with some additional behavior.
      Specified by:
      contributeInternal in class AbstractSolrQueryContributor
      Parameters:
      solrQuery - The query to customize
      searchRequest - The search query request
      fieldDefinitions - The fields to perform the search on
      page - Page information for page to retrieve for query
      properties - Additional properties for this query
      locale - The locale of this search
      context - The context information surrounding sandboxing/multitenant state
    • attachRuleBoosts

      protected void attachRuleBoosts(org.apache.solr.client.solrj.SolrQuery solrQuery, org.springframework.data.domain.Sort sorts, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, List<com.broadleafcommerce.search.api.domain.RelevancyRule> relevancyRules, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, @Nullable String locale)
      Configure Solr result sorting with given page sort information
      Parameters:
      solrQuery - Query to execute in Solr
      sorts - The sort options
      fieldDefinitions - All field definitions for the type being queried
      locale - (optional) the locale of the query
    • shouldApplyRule

      protected boolean shouldApplyRule(com.broadleafcommerce.search.api.domain.RelevancyRule rule, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, @Nullable String locale)
    • shouldApplyRuleBasedOnSearchTerm

      protected boolean shouldApplyRuleBasedOnSearchTerm(com.broadleafcommerce.search.api.domain.RelevancyRule rule, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, @Nullable String locale)
    • shouldApplyRuleBasedOnCategoryContext

      protected boolean shouldApplyRuleBasedOnCategoryContext(com.broadleafcommerce.search.api.domain.RelevancyRule rule, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, @Nullable String locale)
    • buildFilterParamsMap

      protected Map<String,String> buildFilterParamsMap(@Nullable List<com.broadleafcommerce.search.api.domain.Filter> filters)
      Converts the filters in a set of filters to a key/value map to be used in relevancy rule evaluation. Note that this implementation only supports a single filter key/value comparison, and the first item in the filter's value list takes precedence.
      Parameters:
      filters - The filters to convert into a map.
      Returns:
      A key/value map of Filter values.
      See Also:
    • defaultMap

      protected Map<String,String> defaultMap()
      Returns:
      Returns an empty, unmodifiable map.
    • getRuleField

      @Nullable protected String getRuleField(com.broadleafcommerce.search.api.domain.RelevancyRule rule, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, @Nullable String locale)
      Returns the Solr field name for the relevancy rule.
      Parameters:
      rule - the requested relevancy rule
      fieldDefinitions - All field definitions for the type being queried
      locale - (optional) the locale of the query
      Returns:
      the Solr field name for the relevancy rule
    • getRelevancyRuleVariant

      @Nullable protected com.broadleafcommerce.search.api.domain.FieldVariant getRelevancyRuleVariant(com.broadleafcommerce.search.api.domain.RelevancyRule rule, com.broadleafcommerce.search.api.domain.FieldDefinition ruleField)
      Retrieve the FieldVariant to be used for the relevancy rule.
      Returns:
      variant to be used for the relevancy rule, or null
    • buildBoostForField

      protected void buildBoostForField(org.apache.solr.client.solrj.SolrQuery solrQuery, com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField, List<String> boostFunctions, @Nullable String locale)
    • applyMatchingQueryBoostToQueryFields

      protected void applyMatchingQueryBoostToQueryFields(org.apache.solr.client.solrj.SolrQuery solrQuery, com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField)
      Builds the boosted query field for the RelevancyRule of type DefaultRelevancyRuleTypes.MATCHING_VALUE.
      Parameters:
      solrQuery - solrQuery Query to execute in Solr
      rule - the RelevancyRule to configure a boost for
      ruleField - the Solr field name that the boost should be applied to
    • buildMatchingValueBoostFunction

      protected String buildMatchingValueBoostFunction(com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField)
      Builds the boost function to apply for the RelevancyRule of type DefaultRelevancyRuleTypes.MATCHING_VALUE. The default implementation of this method gets the term frequency of the field value and uses that to determine the computed boosted function. That computed boost function will be used if the term frequency is 1 or greater (if the term exists on the field). If the frequency is zero, the default boost function is returned.
      Parameters:
      rule - the RelevancyRule to configure a boost for
      ruleField - the Solr field name that the boost should be applied to
      Returns:
      the boost function computed
    • determineMatchingValueBoostedFunction

      protected String determineMatchingValueBoostedFunction(com.broadleafcommerce.search.api.domain.RelevancyRule rule, String termFreqFunction)
    • buildOrdinalBoostFunction

      protected String buildOrdinalBoostFunction(com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField)
      Builds the boost function to apply for the RelevancyRule of type DefaultRelevancyRuleTypes.ORDINAL. The default implementation of this method gets the order of the field (ascending by default, but can be configured on the rule to ascending), and multiplies it by the boost amount. The result is then compared with the minimum boundary configured on the rule & the higher value is returned.
      Parameters:
      rule - the RelevancyRule to configure a boost for
      ruleField - the Solr field name that the boost should be applied to
      Returns:
      the boost function computed
    • buildRelativeBoostFunction

      protected String buildRelativeBoostFunction(com.broadleafcommerce.search.api.domain.RelevancyRule rule, String ruleField)
      Builds the boost function to apply for the RelevancyRule of type DefaultRelevancyRuleTypes.RELATIVE. The default implementation of this method adds one to the field value (to prevent division-by-zero errors), and then takes to inverse of that value. That inverted value is then multiplied by the boost amount. The result is then compared with the minimum boundary configured on the rule & the higher value is returned.
      Parameters:
      rule - the RelevancyRule to configure a boost for
      ruleField - the Solr field name that the boost should be applied to
      Returns:
      the boost function computed
    • shouldContribute

      protected boolean shouldContribute(org.apache.solr.client.solrj.SolrQuery solrQuery, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, org.springframework.data.domain.Pageable page, com.broadleafcommerce.search.core.service.SearchRequestProperties properties, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from class: AbstractSolrQueryContributor
      Determines if this contributor should run.
      Specified by:
      shouldContribute in class AbstractSolrQueryContributor
      Parameters:
      solrQuery - The query to customize
      searchRequest - The search query request
      page - Page information for page to retrieve for query
      properties - Additional properties for this query.
      context - The context information surrounding sandboxing/multitenant state
      Returns:
      whether this contributor should run
    • contributeOnTypeAhead

      public boolean contributeOnTypeAhead()
      Returns:
      Should this contributor be executed on TypeAhead requests?
    • getSolrFieldService

      protected com.broadleafcommerce.search.provider.solr.SolrFieldService getSolrFieldService()
    • getRelevancyRuleUtils

      protected com.broadleafcommerce.search.core.service.relevancyrule.RelevancyRuleUtils<com.broadleafcommerce.search.api.domain.RelevancyRule> getRelevancyRuleUtils()
    • getRuleEvaluationService

      protected com.broadleafcommerce.rulesengine.expression.service.RuleEvaluationService getRuleEvaluationService()
    • getExpressionContext

      protected com.broadleafcommerce.rulesengine.expression.context.ExpressionContext getExpressionContext()