Class SolrSearchServiceImpl

java.lang.Object
org.broadleafcommerce.core.search.service.solr.SolrSearchServiceImpl
All Implemented Interfaces:
SearchService, org.springframework.beans.factory.DisposableBean

public class SolrSearchServiceImpl extends Object implements SearchService, org.springframework.beans.factory.DisposableBean
An implementation of SearchService that uses Solr.

Note that prior to 2.2.0, this class used to contain all of the logic for interaction with Solr. Since 2.2.0, this class has been refactored and parts of it have been split into the other classes you can find in this package.

Author:
Andre Azzolini (apazzolini)
  • Field Details

    • solrConfiguration

      @Qualifier("blCatalogSolrConfiguration") @Autowired(required=false) protected SolrConfiguration solrConfiguration
    • productDao

      protected ProductDao productDao
    • skuDao

      protected SkuDao skuDao
    • fieldDao

      protected FieldDao fieldDao
    • searchFacetDao

      protected SearchFacetDao searchFacetDao
    • shs

      protected SolrHelperService shs
    • indexFieldDao

      protected IndexFieldDao indexFieldDao
    • extensionManager

      protected SolrSearchServiceExtensionManager extensionManager
    • globalFacetsForCategorySearch

      @Value("${solr.global.facets.category.search:false}") protected boolean globalFacetsForCategorySearch
    • boostSearchResultsCategory

      @Value("${solr.boost.category.results:false}") protected boolean boostSearchResultsCategory
    • shouldShowDebugQuery

      @Value("${solr.showDebugQuery:false}") protected boolean shouldShowDebugQuery
    • solrSearchMethod

      @Value("${solr.search.method:POST}") protected org.apache.solr.client.solrj.SolrRequest.METHOD solrSearchMethod
  • Constructor Details

    • SolrSearchServiceImpl

      public SolrSearchServiceImpl()
  • Method Details

    • shouldShowDebugQuery

      protected boolean shouldShowDebugQuery()
      Returns:
      whether or not to enable debug query info for the SolrQuery
    • findExplicitSearchResultsByCategory

      public SearchResult findExplicitSearchResultsByCategory(Category category, SearchCriteria searchCriteria) throws ServiceException
      Description copied from interface: SearchService
      Performs a search for search results in the given category, taking into consideration the SearchCriteria

      This method will NOT return search results that are in a sub-level of a given category. For example, if you had a "Routers" category and a "Enterprise Routers" sub-category, asking for search results in "Routers", would NOT return search results that are in the "Enterprise Routers" category.

      Specified by:
      findExplicitSearchResultsByCategory in interface SearchService
      Returns:
      Throws:
      ServiceException
      See Also:
    • findSearchResultsByCategory

      @Deprecated public SearchResult findSearchResultsByCategory(Category category, SearchCriteria searchCriteria) throws ServiceException
      Deprecated.
      Description copied from interface: SearchService
      Performs a search for search results in the given category, taking into consideration the SearchCriteria

      This method will return search results that are in any sub-level of a given category. For example, if you had a "Routers" category and a "Enterprise Routers" sub-category, asking for search results in "Routers", would return search results that are in the "Enterprise Routers" category.

      Specified by:
      findSearchResultsByCategory in interface SearchService
      Returns:
      the result of the search
      Throws:
      ServiceException
      See Also:
    • findSearchResultsByQuery

      @Deprecated public SearchResult findSearchResultsByQuery(String query, SearchCriteria searchCriteria) throws ServiceException
      Deprecated.
      Description copied from interface: SearchService
      Performs a search for search results across all categories for the given query, taking into consideration the SearchCriteria
      Specified by:
      findSearchResultsByQuery in interface SearchService
      Returns:
      the result of the search
      Throws:
      ServiceException
    • findSearchResultsByCategoryAndQuery

      @Deprecated public SearchResult findSearchResultsByCategoryAndQuery(Category category, String query, SearchCriteria searchCriteria) throws ServiceException
      Deprecated.
      Description copied from interface: SearchService
      Performs a search for search results in the given category for the given query, taking into consideration the SearchCriteria
      Specified by:
      findSearchResultsByCategoryAndQuery in interface SearchService
      Throws:
      ServiceException
    • findSearchResults

      public SearchResult findSearchResults(SearchCriteria searchCriteria) throws ServiceException
      Description copied from interface: SearchService
      Performs a search for search results based on the given SearchCriteria, if SearchCriteria has a category, the category is considering for the search.
      Specified by:
      findSearchResults in interface SearchService
      Parameters:
      searchCriteria - contains the information about this given search
      Returns:
      the SearchResult
      Throws:
      ServiceException
    • findSearchResults

      @Deprecated(forRemoval=true) protected SearchResult findSearchResults(String qualifiedSolrQuery, List<SearchFacetDTO> facets, SearchCriteria searchCriteria, String defaultSort) throws ServiceException
      Deprecated, for removal: This API element is subject to removal in a future version.
      in favor of the other findSearchResults() method
      Throws:
      ServiceException
    • findSearchResults

      protected SearchResult findSearchResults(String qualifiedSolrQuery, List<SearchFacetDTO> facets, SearchCriteria searchCriteria, String defaultSort, String... filterQueries) throws ServiceException
      Given a qualified solr query string (such as "category:2002"), actually performs a solr search. It will take into considering the search criteria to build out facets / pagination / sorting.
      Parameters:
      searchCriteria -
      facets -
      Returns:
      the ProductSearchResult of the search
      Throws:
      ServiceException
    • filterProductsBasedOnInactiveCategory

      protected void filterProductsBasedOnInactiveCategory(List<Product> products, Category category)
    • filterEmptyFacets

      protected void filterEmptyFacets(List<SearchFacetDTO> facets)
    • getDefaultSort

      protected String getDefaultSort(SearchCriteria criteria)
    • getCategoryFilter

      protected String getCategoryFilter(SearchCriteria searchCriteria)
    • getLocalePrefix

      public String getLocalePrefix()
    • buildQueryFieldsString

      protected String buildQueryFieldsString(org.apache.solr.client.solrj.SolrQuery query, SearchCriteria searchCriteria)
    • getQueryFields

      protected void getQueryFields(org.apache.solr.client.solrj.SolrQuery query, List<String> queryFields, IndexField indexField, SearchCriteria searchCriteria)
      This helper method gathers the query fields for the given field and stores them in the List parameter.
      Parameters:
      query -
      queryFields - the query fields for this query
      indexField -
      searchCriteria -
    • modifySolrQuery

      protected void modifySolrQuery(org.apache.solr.client.solrj.SolrQuery query, String qualifiedSolrQuery, List<SearchFacetDTO> facets, SearchCriteria searchCriteria, String defaultSort)
      Provides a hook point for implementations to modify all SolrQueries before they're executed. Modules should leverage the extension manager method of the same name, SolrSearchServiceExtensionHandler.modifySolrQuery(SolrQuery, String, List, SearchCriteria, String)
      Parameters:
      query -
      qualifiedSolrQuery -
      facets -
      searchCriteria -
      defaultSort -
    • createSearchContextDTO

      protected SearchContextDTO createSearchContextDTO()
    • getResponseDocuments

      protected List<org.apache.solr.common.SolrDocument> getResponseDocuments(org.apache.solr.client.solrj.response.QueryResponse response)
    • getSearchFacets

      public List<SearchFacetDTO> getSearchFacets()
      Description copied from interface: SearchService
      Gets all available facets for search results page
      Specified by:
      getSearchFacets in interface SearchService
      Returns:
      the available facets
    • getSearchFacets

      public List<SearchFacetDTO> getSearchFacets(Category category)
      Description copied from interface: SearchService
      Gets all available facets for the given category and global search
      Specified by:
      getSearchFacets in interface SearchService
      Returns:
    • getCategoryFacets

      public List<SearchFacetDTO> getCategoryFacets(Category category)
      Description copied from interface: SearchService
      Gets all available facets for a given category
      Specified by:
      getCategoryFacets in interface SearchService
      Returns:
      the available facets
    • attachSortClause

      protected void attachSortClause(org.apache.solr.client.solrj.SolrQuery query, SearchCriteria searchCriteria, String defaultSort)
      Sets up the sorting criteria. This will support sorting by multiple fields at a time
      Parameters:
      query -
      searchCriteria -
    • attachActiveFacetFilters

      protected void attachActiveFacetFilters(org.apache.solr.client.solrj.SolrQuery query, Map<String,SearchFacetDTO> namedFacetMap, SearchCriteria searchCriteria)
      Restricts the query by adding active facet filters.
      Parameters:
      query -
      namedFacetMap -
      searchCriteria -
    • scrubFacetValue

      protected String scrubFacetValue(String facetValue)
      Scrubs a facet value string for all Solr special characters, automatically adding escape characters
      Parameters:
      facetValue - The raw facet value
      Returns:
      The facet value with all special characters properly escaped, safe to be used in construction of a Solr query
    • attachFacets

      protected void attachFacets(org.apache.solr.client.solrj.SolrQuery query, Map<String,SearchFacetDTO> namedFacetMap, SearchCriteria searchCriteria)
      Notifies solr about which facets you want it to determine results and counts for
      Parameters:
      query -
      namedFacetMap -
      searchCriteria -
    • setFacetResults

      protected void setFacetResults(Map<String,SearchFacetDTO> namedFacetMap, org.apache.solr.client.solrj.response.QueryResponse response)
      Builds out the DTOs for facet results from the search. This will then be used by the view layer to display which values are available given the current constraints as well as the count of the values.
      Parameters:
      namedFacetMap -
      response -
    • sortFacetResults

      protected void sortFacetResults(Map<String,SearchFacetDTO> namedFacetMap)
      Invoked to sort the facet results. This method will use the natural sorting of the value attribute of the facet (or, if value is null, the minValue of the facet result). Override this method to customize facet sorting for your given needs.
      Parameters:
      namedFacetMap -
    • setPagingAttributes

      public void setPagingAttributes(SearchResult result, int numResults, SearchCriteria searchCriteria)
      Sets the total results, the current page, and the page size on the ProductSearchResult. Total results comes from solr, while page and page size are duplicates of the searchCriteria conditions for ease of use.
      Parameters:
      result -
      numResults -
      searchCriteria -
    • getProducts

      protected List<Product> getProducts(List<org.apache.solr.common.SolrDocument> responseDocuments)
      Given a list of product IDs from solr, this method will look up the IDs via the productDao and build out actual Product instances. It will return a Products that is sorted by the order of the IDs in the passed in list.
      Parameters:
      responseDocuments -
      Returns:
      the actual Product instances as a result of the search
    • buildSearchFacetDTOs

      protected List<SearchFacetDTO> buildSearchFacetDTOs(List<SearchFacet> searchFacets)
      Create the wrapper DTO around the SearchFacet
      Parameters:
      searchFacets -
      Returns:
      the wrapper DTO
    • facetIsAvailable

      protected boolean facetIsAvailable(SearchFacet facet, Map<String,String[]> params)
      Checks to see if the requiredFacets condition for a given facet is met.
      Parameters:
      facet -
      params -
      Returns:
      whether or not the facet parameter is available
    • sanitizeQuery

      protected String sanitizeQuery(String query)
      Perform any necessary query sanitation here. For example, we disallow open and close parentheses, colons, and we also ensure that quotes are actual quotes (") and not the URL encoding (") so that Solr is able to properly handle the user's intent.
      Parameters:
      query -
      Returns:
      the sanitized query
    • getSolrTaggedFieldString

      protected String getSolrTaggedFieldString(String indexField, String tag, SearchFacetRange range)
      Returns a fully composed solr field string. Given indexField = a, tag = ex, and a non-null range, would produce the following String: {!tag=a frange incl=false l=minVal u=maxVal}a
    • getSolrFieldTag

      protected String getSolrFieldTag(String tagField, String tag, SearchFacetRange range)
      Returns a solr field tag. Given indexField = a, tag = tag, would produce the following String: {!tag=a}. if range is not null it will produce {!tag=a frange incl=false l=minVal u=maxVal}
    • getSolrRangeString

      protected String getSolrRangeString(String fieldName, BigDecimal minValue, BigDecimal maxValue)
    • getSolrRangeFunctionString

      protected String getSolrRangeFunctionString(BigDecimal minValue, BigDecimal maxValue)
      Parameters:
      minValue -
      maxValue -
      Returns:
      a string representing a call to the frange solr function. it is not inclusive of lower limit, inclusive of upper limit
    • getNamedFacetMap

      protected Map<String,SearchFacetDTO> getNamedFacetMap(List<SearchFacetDTO> facets, SearchCriteria searchCriteria)
      Parameters:
      facets -
      searchCriteria -
      Returns:
      a map of fully qualified solr index field key to the searchFacetDTO object
    • getSolrQueryMethod

      protected org.apache.solr.client.solrj.SolrRequest.METHOD getSolrQueryMethod()
      Allows the user to choose the query method to use. POST allows for longer, more complex queries with a higher number of facets.

      Default value is POST. Implementors can change the value by setting the solr.search.method property

      Returns:
    • destroy

      public void destroy() throws Exception
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
      Throws:
      Exception
    • isActive

      public boolean isActive()
      Description copied from interface: SearchService
      Determines whether or not the necessary configuration is in place
      Specified by:
      isActive in interface SearchService
      Returns:
      whether or not the necessary configuration is in place