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:
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
asSpellCheckSuggestions
.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, ifauto query correction
is enabled, thecorrected 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
All Methods Instance Methods Concrete Methods 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)
Apply any Solr spell check suggestions to the spell check results.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
getSpellCheckProperties()
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 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.
-
-
-
Method Detail
-
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(List<org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion> suggestions, com.broadleafcommerce.search.api.domain.SpellCheckResult spellCheckResult)
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
-
getSpellCheckProperties
protected com.broadleafcommerce.search.core.spellcheck.SpellCheckProperties getSpellCheckProperties()
-
-