Class TypeAheadKeywordPostProcessor
- java.lang.Object
-
- com.broadleafcommerce.search.provider.solr.typeahead.postprocessors.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()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
TypeAheadKeywordPostProcessor.KeywordContainer
Class holding potential suggestions.
-
Constructor Summary
Constructors Constructor Description TypeAheadKeywordPostProcessor()
-
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'sQueryResponse
.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)
-
-
-
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 interfacecom.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 responseconfiguration
- 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 resultsconfiguration
- The configuration used on the requesthighlightedString
- Highlighted Stringquery
- 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 keywordsconfiguration
- The configuration used on the requesthighlightedString
- Entire highlighted result Stringquery
- 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 stringprefix
- The highlight prefixpostfix
- 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 containeradditionalTerms
- Additional terms that occur after the highlighted termprefix
- The highlight prefixpostfix
- The higlight postfixterm
- The highlighted termhighPriority
- 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>
<highlight>highlighted term</highlight>
Stopwords are not taken into account, so a string such as
<highlight>highlighted</highlight> with <highlight>term</highlight>
- Parameters:
configuration
- The configuration used on this queryhighlightedString
- 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'sQueryResponse
. This is intentionally not usingQueryResponse.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 interfaceorg.springframework.core.Ordered
-
-