Class SolrResponseSpellCheckDecorator
- All Implemented Interfaces:
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:
Suggestions
, which have corrected spellings for the original search token(s)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
ConstructorsConstructorDescriptionSolrResponseSpellCheckDecorator
(com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties spellCheckProperties) -
Method Summary
Modifier and TypeMethodDescriptionprotected 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 theSpellCheckResult
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 theSearchRequest
, 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 theSpellCheckResult
with search suggestions and query recommendations if present in the Solr query response.<T extends com.broadleafcommerce.search.api.domain.SearchResponse>
voiddecorate
(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 theSearchResponse
.protected com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties
protected boolean
hasSpellCheckResponse
(org.apache.solr.client.solrj.response.SpellCheckResponse spellCheckResponse) Check whether the SolrSpellCheckResponse
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 SolrQueryResponse
and populate it on theSearchResponse
.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 theSearchResponse
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.
-
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 theSearchResponse
.- Specified by:
decorate
in interfaceSolrResponseDecorator
- Parameters:
searchResponse
- Search response to be updatedsearchRequest
- Original search requestqueryResponse
- Response from Solr for searchfieldDefinitions
- An unmodifiable list of field definitions used on the original searchpage
- The requested page for the searchproperties
- Additional properties for this searchcontext
- (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 SolrQueryResponse
and populate it on theSearchResponse
. Also mark the response to be re-queried for auto-correction if applicable.- Parameters:
searchResponse
- the search response to populate spell check response onqueryResponse
- the Solr query response which might contain spell check informationsearchRequest
- the original search request
-
hasSpellCheckResponse
protected boolean hasSpellCheckResponse(@Nullable org.apache.solr.client.solrj.response.SpellCheckResponse spellCheckResponse) Check whether the SolrSpellCheckResponse
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 theSearchResponse
to be re-queried for an auto-corrected query, if applicable.- Parameters:
searchResponse
- the response to mark to be re-queriedsearchRequest
- the original search requestqueryResponse
- 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 suggestionsqueryResponse
- 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 theSearchRequest
, 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 theSpellCheckResult
with search suggestions and query recommendations if present in the Solr query response.- Parameters:
spellCheckResponse
- the Solr spell check response, with suggestionssearchRequest
- 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 theSpellCheckResult
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 suggestionssearchRequest
- the original search requestspellCheckResult
- 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 suggestionsspellCheckResult
- 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 suggestionsspellCheckResult
- 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 checksearchRequest
- 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()
-