Class TypeAheadKeywordPostProcessor

  • All Implemented Interfaces:
    com.broadleafcommerce.search.core.service.TypeAheadPostProcessor<org.apache.solr.client.solrj.response.QueryResponse>, org.springframework.core.Ordered

    public class TypeAheadKeywordPostProcessor
    extends Object
    implements com.broadleafcommerce.search.core.service.TypeAheadPostProcessor<org.apache.solr.client.solrj.response.QueryResponse>
    Parses highlighting results from Solr and adds them to the TypeAhead response. In this use case, these are the text predictions the customer will see on the UI.

    A typical Solr response will return an entire field with the relevant word(s) highlighted. For example, a highlighted result might be

         Apple <em>iPhone 8</em> 64 GB
     
    This PostProcessor extracts the highlighted word from the highlight response, in this case "iPhone 8" and sets that on the TypeAheadResponse
    See Also:
    TypeAheadConfiguration.getHighlightPrepend(), TypeAheadConfiguration.getHighlightAppend()
    • Field Summary

      • Fields inherited from interface org.springframework.core.Ordered

        HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addSingleKeywordSuggestion​(TypeAheadKeywordPostProcessor.KeywordContainer container, String additionalTerms, String prefix, String postfix, String term, boolean highPriority)
      Adds a highlighted word to the container to be used in results.
      protected com.broadleafcommerce.search.api.domain.typeahead.SuggestionPhrase buildSuggestionPhrase​(String keywordHighlighted, com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration, boolean highlightingEnabled)
      Creates the suggestion object for the keyword.
      protected int getAutocompleteSuggestionLimit​(com.broadleafcommerce.search.api.domain.TypeAheadConfiguration typeAheadConfiguration)  
      protected String[] getHighlightedStrings​(String highlightedString, String prefix, String postfix)
      Returns an array of all the individual highlighted strings that occur in the highlighted result in order of their occurrence.
      protected Set<String> getHighlighting​(org.apache.solr.client.solrj.response.QueryResponse queryResponse)
      Returns a Set of keywords retrieved from Solr's QueryResponse.
      int getOrder()  
      protected boolean highlightingEnabled​(com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration)  
      protected String mergeContiguous​(com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration, String highlightedString)
      Merges consecutive highlighted terms into a single highlighted term.
      protected void parseHighlightedWord​(TypeAheadKeywordPostProcessor.KeywordContainer container, com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration, String highlightedString, String query)
      Handles parsing the highlighted term.
      protected void parseKeyword​(TypeAheadKeywordPostProcessor.KeywordContainer container, com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration, String highlightedString, String query)
      Parses, then adds, a highlighted term to the container results.
      protected Collection<String> parseKeywords​(org.apache.solr.client.solrj.response.QueryResponse queryResponse, com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration, com.broadleafcommerce.search.api.domain.typeahead.TypeAheadRequest request)
      Parses keywords for highlighting from the Solr query response.
      void postProcess​(org.apache.solr.client.solrj.response.QueryResponse queryResponse, com.broadleafcommerce.search.api.domain.typeahead.TypeAheadResponse response, com.broadleafcommerce.search.api.domain.typeahead.TypeAheadRequest request, com.broadleafcommerce.search.api.domain.TypeAheadConfiguration typeAheadConfiguration, com.broadleafcommerce.search.core.service.SearchRequestProperties properties, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)  
    • Constructor Detail

      • TypeAheadKeywordPostProcessor

        public TypeAheadKeywordPostProcessor()
    • Method Detail

      • postProcess

        public void postProcess​(org.apache.solr.client.solrj.response.QueryResponse queryResponse,
                                com.broadleafcommerce.search.api.domain.typeahead.TypeAheadResponse response,
                                com.broadleafcommerce.search.api.domain.typeahead.TypeAheadRequest request,
                                com.broadleafcommerce.search.api.domain.TypeAheadConfiguration typeAheadConfiguration,
                                com.broadleafcommerce.search.core.service.SearchRequestProperties properties,
                                @Nullable
                                com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Specified by:
        postProcess in interface com.broadleafcommerce.search.core.service.TypeAheadPostProcessor<org.apache.solr.client.solrj.response.QueryResponse>
      • buildSuggestionPhrase

        protected com.broadleafcommerce.search.api.domain.typeahead.SuggestionPhrase buildSuggestionPhrase​(String keywordHighlighted,
                                                                                                           com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration,
                                                                                                           boolean highlightingEnabled)
        Creates the suggestion object for the keyword.
        Parameters:
        keywordHighlighted - Word/phrase to be suggested
        Returns:
        suggestion phrase for the keyword
      • highlightingEnabled

        protected boolean highlightingEnabled​(com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration)
      • parseKeywords

        protected Collection<String> parseKeywords​(org.apache.solr.client.solrj.response.QueryResponse queryResponse,
                                                   com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration,
                                                   com.broadleafcommerce.search.api.domain.typeahead.TypeAheadRequest request)
        Parses keywords for highlighting from the Solr query response.
        Parameters:
        queryResponse - Solr query response
        configuration - The configuration used on the request
        Returns:
        all keywords for highlighting
      • getAutocompleteSuggestionLimit

        protected int getAutocompleteSuggestionLimit​(com.broadleafcommerce.search.api.domain.TypeAheadConfiguration typeAheadConfiguration)
      • parseKeyword

        protected void parseKeyword​(TypeAheadKeywordPostProcessor.KeywordContainer container,
                                    com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration,
                                    String highlightedString,
                                    String query)
        Parses, then adds, a highlighted term to the container results.
        Parameters:
        container - The container to store keyword results
        configuration - The configuration used on the request
        highlightedString - Highlighted String
        query - The original query on the request.
      • parseHighlightedWord

        protected void parseHighlightedWord​(TypeAheadKeywordPostProcessor.KeywordContainer container,
                                            com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration,
                                            String highlightedString,
                                            String query)
        Handles parsing the highlighted term.

        This implementation prefers results that start with the first letter of the original query.

        For example, consider the query "baseball g". Due to the way highlighting works, a term like "green baseball" could potentially show up before "baseball glove", so we add those terms as a lower priority in the returned TypeAheadKeywordPostProcessor.KeywordContainer.

        Parameters:
        container - The container used to store keywords
        configuration - The configuration used on the request
        highlightedString - Entire highlighted result String
        query - The original query on the request
      • getHighlightedStrings

        @Nullable
        protected String[] getHighlightedStrings​(String highlightedString,
                                                 String prefix,
                                                 String postfix)
        Returns an array of all the individual highlighted strings that occur in the highlighted result in order of their occurrence.
        Parameters:
        highlightedString - The complete highlighted string
        prefix - The highlight prefix
        postfix - The highlight postfix
        Returns:
        An array containing all highlighted terms with duplicates removed.
      • addSingleKeywordSuggestion

        protected void addSingleKeywordSuggestion​(TypeAheadKeywordPostProcessor.KeywordContainer container,
                                                  String additionalTerms,
                                                  String prefix,
                                                  String postfix,
                                                  String term,
                                                  boolean highPriority)
        Adds a highlighted word to the container to be used in results.

        If this is a single highlighted term, (no spaces), any terms that occur immediately after the highlighted word are appended and that value is added to the results.

        Parameters:
        container - The keyword container
        additionalTerms - Additional terms that occur after the highlighted term
        prefix - The highlight prefix
        postfix - The higlight postfix
        term - The highlighted term
        highPriority - If this is a highPriority highlight term
      • mergeContiguous

        protected String mergeContiguous​(com.broadleafcommerce.search.api.domain.TypeAheadConfiguration configuration,
                                         String highlightedString)
        Merges consecutive highlighted terms into a single highlighted term.

        Solr's highlighter does not merge contiguous highlighted terms, so when we end up with a result with consecutive highlighted terms, we merge them into one. For example:

         
             <highlight>highlighted</highlight> <highlight>term</highlight>
         
        Will be merged into a single string in the format of:
         
              <highlight>highlighted term</highlight>
         

        Stopwords are not taken into account, so a string such as

         
             <highlight>highlighted</highlight> with <highlight>term</highlight>
         
        would not be modified.
        Parameters:
        configuration - The configuration used on this query
        highlightedString - The highlighted value returned from Solr.
        Returns:
        A string with all highlighted elements merged.
      • getHighlighting

        protected Set<String> getHighlighting​(org.apache.solr.client.solrj.response.QueryResponse queryResponse)
        Returns a Set of keywords retrieved from Solr's QueryResponse. This is intentionally not using QueryResponse.getHighlighting() as it uses a HashMap and, as a result, document rank is lost.
        Parameters:
        queryResponse - The Solr query response
        Returns:
        A set of highlighted strings with blank and duplicate values removed.
      • getOrder

        public int getOrder()
        Specified by:
        getOrder in interface org.springframework.core.Ordered