Class SolrResponseSpellCheckDecorator

java.lang.Object
com.broadleafcommerce.search.provider.solr.response.SolrResponseSpellCheckDecorator
All Implemented Interfaces:
SolrResponseDecorator

public class SolrResponseSpellCheckDecorator extends Object implements SolrResponseDecorator

If spell check is enabled, this class handles parsing the spelling suggestions and adding them to the response. The container for spell check results is SpellCheckResult, which encapsulates both suggestions and suggested queries.

Specifically, we look at the SpellCheckResponse, which contains two things we're interested in:

  1. Suggestions, which have corrected spellings for the original search token(s)
  2. Collations, which are essentially spell-checked queries with optional hit counts.

In the event that we find suggestions, these are set on the SearchResponse as SpellCheckSuggestions.

For collations, these are set on the response as SpellCheckResult.SuggestedQuery. In the case that there are no search results on the original query and we have a query that will return result, if auto query correction is enabled, the corrected query value is set to true so that the query can be re-executed. This class does not re-execute the query.

  • Constructor Summary

    Constructors
    Constructor
    Description
    SolrResponseSpellCheckDecorator(com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties spellCheckProperties)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    addCollatedResults(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Collation> collatedResults, com.broadleafcommerce.search.api.domain.SpellCheckResult spellCheckResult)
    Apply any Solr auto-corrected query suggestions to the spell check results.
    protected void
    addSpellCheckSuggestions(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion> suggestions, com.broadleafcommerce.search.api.domain.SpellCheckResult spellCheckResult)
    Deprecated.
    protected void
    addSpellCheckSuggestions(org.apache.solr.client.solrj.response.SpellCheckResponse spellCheckResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, com.broadleafcommerce.search.api.domain.SpellCheckResult spellCheckResult)
    Create the SpellCheckResult with search suggestions and query recommendations if present in the Solr query response.
    protected boolean
    autoRequeryEnabled(com.broadleafcommerce.search.api.domain.SearchRequest searchRequest)
    Check if auto-correction querying is enabled for the SearchRequest, or by default configuration.
    protected com.broadleafcommerce.search.api.domain.SpellCheckResult
    buildSpellCheckResult(org.apache.solr.client.solrj.response.SpellCheckResponse spellCheckResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest)
    Create the SpellCheckResult with search suggestions and query recommendations if present in the Solr query response.
    <T extends com.broadleafcommerce.search.api.domain.SearchResponse>
    void
    decorate(T searchResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, org.apache.solr.client.solrj.response.QueryResponse queryResponse, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, org.springframework.data.domain.Pageable page, com.broadleafcommerce.search.core.service.SearchRequestProperties properties, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
    Add some extra information to the SearchResponse.
    protected com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties
     
    protected boolean
    hasSpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse spellCheckResponse)
    Check whether the Solr SpellCheckResponse contains any spelling suggestions.
    protected boolean
    hasSpellingSuggestions(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion> suggestions)
    Check if the response has any spell check suggestions.
    protected boolean
    hasSuggestedQueries(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Collation> collations)
    Check if the response has any auto-corrected query suggestions.
    protected boolean
    isValidTokenWithinQuery(int startOffset, int endOffset, String originalQuery, String token)
    Attempts to determine if the token provided is actually a token for the purposes of suggesting a replacement.
    protected void
    populateSpellCheckResponse(com.broadleafcommerce.search.api.domain.SearchResponse searchResponse, org.apache.solr.client.solrj.response.QueryResponse queryResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest)
    Extract the spell check response from the Solr QueryResponse and populate it on the SearchResponse.
    protected void
    setAutoRequery(com.broadleafcommerce.search.api.domain.SearchResponse searchResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, org.apache.solr.client.solrj.response.QueryResponse queryResponse)
    Mark the SearchResponse to be re-queried for an auto-corrected query, if applicable.
    protected boolean
    shouldCorrectQuery(com.broadleafcommerce.search.api.domain.SearchResponse searchResponse, org.apache.solr.client.solrj.response.QueryResponse queryResponse)
    Should we autocorrect this query? We autocorrect if
    1.
    protected boolean
    shouldDecorate(com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties spellCheckProperties, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest)
    Check whether or not spell checking info should be added to the Solr response based on spell check being enabled.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SolrResponseSpellCheckDecorator

      public SolrResponseSpellCheckDecorator(com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties spellCheckProperties)
  • Method Details

    • decorate

      public <T extends com.broadleafcommerce.search.api.domain.SearchResponse> void decorate(T searchResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, org.apache.solr.client.solrj.response.QueryResponse queryResponse, List<com.broadleafcommerce.search.api.domain.FieldDefinition> fieldDefinitions, org.springframework.data.domain.Pageable page, com.broadleafcommerce.search.core.service.SearchRequestProperties properties, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Description copied from interface: SolrResponseDecorator
      Add some extra information to the SearchResponse.
      Specified by:
      decorate in interface SolrResponseDecorator
      Parameters:
      searchResponse - Search response to be updated
      searchRequest - Original search request
      queryResponse - Response from Solr for search
      fieldDefinitions - An unmodifiable list of field definitions used on the original search
      page - The requested page for the search
      properties - Additional properties for this search
      context - (optional) The context information surrounding sandboxing/multitenant state
    • populateSpellCheckResponse

      protected void populateSpellCheckResponse(com.broadleafcommerce.search.api.domain.SearchResponse searchResponse, org.apache.solr.client.solrj.response.QueryResponse queryResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest)
      Extract the spell check response from the Solr QueryResponse and populate it on the SearchResponse. Also mark the response to be re-queried for auto-correction if applicable.
      Parameters:
      searchResponse - the search response to populate spell check response on
      queryResponse - the Solr query response which might contain spell check information
      searchRequest - the original search request
    • hasSpellCheckResponse

      protected boolean hasSpellCheckResponse(@Nullable org.apache.solr.client.solrj.response.SpellCheckResponse spellCheckResponse)
      Check whether the Solr SpellCheckResponse contains any spelling suggestions.
      Parameters:
      spellCheckResponse - the response to check for spelling suggestions
      Returns:
      whether the spellCheckResponse contains spelling suggestions
    • setAutoRequery

      protected void setAutoRequery(com.broadleafcommerce.search.api.domain.SearchResponse searchResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, org.apache.solr.client.solrj.response.QueryResponse queryResponse)
      Mark the SearchResponse to be re-queried for an auto-corrected query, if applicable.
      Parameters:
      searchResponse - the response to mark to be re-queried
      searchRequest - the original search request
      queryResponse - the Solr query response
    • shouldCorrectQuery

      protected boolean shouldCorrectQuery(com.broadleafcommerce.search.api.domain.SearchResponse searchResponse, org.apache.solr.client.solrj.response.QueryResponse queryResponse)
      Should we autocorrect this query? We autocorrect if
      1. There are no search results with the existing query
      2. There is a suggested query to evaluate.
      Parameters:
      searchResponse - the search response containing query suggestions
      queryResponse - the Solr query response
      Returns:
      whether the query should be auto-corrected
    • autoRequeryEnabled

      protected boolean autoRequeryEnabled(com.broadleafcommerce.search.api.domain.SearchRequest searchRequest)
      Check if auto-correction querying is enabled for the SearchRequest, or by default configuration.
      Parameters:
      searchRequest - the search request with auto-correct query settings
      Returns:
      whether auto-correction should be performed for the request
    • buildSpellCheckResult

      protected com.broadleafcommerce.search.api.domain.SpellCheckResult buildSpellCheckResult(org.apache.solr.client.solrj.response.SpellCheckResponse spellCheckResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest)
      Create the SpellCheckResult with search suggestions and query recommendations if present in the Solr query response.
      Parameters:
      spellCheckResponse - the Solr spell check response, with suggestions
      searchRequest - the original search request
      Returns:
      spell check result containing any spell check suggestions and query suggestions
    • addSpellCheckSuggestions

      protected void addSpellCheckSuggestions(org.apache.solr.client.solrj.response.SpellCheckResponse spellCheckResponse, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest, com.broadleafcommerce.search.api.domain.SpellCheckResult spellCheckResult)
      Create the SpellCheckResult with search suggestions and query recommendations if present in the Solr query response. This aligns the tokens with suggestions with the first occurrences of those tokens in the original query string (not the one that was issued to Solr).
      Parameters:
      spellCheckResponse - the Solr spell check response, with suggestions
      searchRequest - the original search request
      spellCheckResult - the spellcheck result that will be returned to the client
    • addSpellCheckSuggestions

      @Deprecated protected void addSpellCheckSuggestions(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion> suggestions, com.broadleafcommerce.search.api.domain.SpellCheckResult spellCheckResult)
      Deprecated.
      Apply any Solr spell check suggestions to the spell check results.
      Parameters:
      suggestions - Solr spell check suggestions
      spellCheckResult - spell check results and suggestions
    • addCollatedResults

      protected void addCollatedResults(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Collation> collatedResults, com.broadleafcommerce.search.api.domain.SpellCheckResult spellCheckResult)
      Apply any Solr auto-corrected query suggestions to the spell check results.
      Parameters:
      collatedResults - Solr auto-corrected query suggestions
      spellCheckResult - spell check results and suggestions
    • hasSuggestedQueries

      protected boolean hasSuggestedQueries(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Collation> collations)
      Check if the response has any auto-corrected query suggestions.
      Parameters:
      collations - auto-corrected query suggestions
      Returns:
      whether the auto-corrected query response has any suggestions
    • hasSpellingSuggestions

      protected boolean hasSpellingSuggestions(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion> suggestions)
      Check if the response has any spell check suggestions.
      Parameters:
      suggestions - spell check response suggestions
      Returns:
      whether the response has any spell check suggestions
    • shouldDecorate

      protected boolean shouldDecorate(com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties spellCheckProperties, com.broadleafcommerce.search.api.domain.SearchRequest searchRequest)
      Check whether or not spell checking info should be added to the Solr response based on spell check being enabled.
      Parameters:
      spellCheckProperties - configuration properties for spell check
      searchRequest - the original search request
      Returns:
      whether the response should be decorated with spell check information
    • isValidTokenWithinQuery

      protected boolean isValidTokenWithinQuery(int startOffset, int endOffset, String originalQuery, String token)
      Attempts to determine if the token provided is actually a token for the purposes of suggesting a replacement. For example, if someone typed "sauc" instead of "sauce", then "sauc" is a token that needs a suggestion. But if someone typed "saucy sauc", then we don't want to make a recommendation on "saucy", even though the token "sauc" exists as a substring.
      Parameters:
      startOffset -
      endOffset -
      originalQuery -
      token -
      Returns:
    • getSpellCheckProperties

      protected com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties getSpellCheckProperties()