org.broadleafcommerce.core.search.service.solr
Class SolrSearchServiceImpl

java.lang.Object
  extended by 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 Summary
protected  SolrSearchServiceExtensionManager extensionManager
           
protected  FieldDao fieldDao
           
protected  ProductDao productDao
           
protected  SearchFacetDao searchFacetDao
           
protected  SolrHelperService shs
           
protected  SolrIndexService solrIndexService
           
 
Constructor Summary
SolrSearchServiceImpl(org.apache.solr.client.solrj.SolrServer solrServer)
           
SolrSearchServiceImpl(org.apache.solr.client.solrj.SolrServer solrServer, org.apache.solr.client.solrj.SolrServer reindexServer)
           
SolrSearchServiceImpl(String solrServer)
           
SolrSearchServiceImpl(String solrServer, String reindexServer)
          This constructor serves to mimic the one below this, which takes in two SolrServer arguments.
 
Method Summary
protected  void appendFieldToQuery(StringBuilder queryBuilder, Field currentField)
           
protected  void attachActiveFacetFilters(org.apache.solr.client.solrj.SolrQuery query, Map<String,SearchFacetDTO> namedFacetMap, ProductSearchCriteria searchCriteria)
          Restricts the query by adding active facet filters.
protected  void attachFacets(org.apache.solr.client.solrj.SolrQuery query, Map<String,SearchFacetDTO> namedFacetMap)
          Notifies solr about which facets you want it to determine results and counts for
protected  void attachSortClause(org.apache.solr.client.solrj.SolrQuery query, ProductSearchCriteria searchCriteria, String defaultSort)
          Sets up the sorting criteria.
protected  String buildQueryFieldsString()
           
protected  List<SearchFacetDTO> buildSearchFacetDTOs(List<SearchFacet> searchFacets)
          Create the wrapper DTO around the SearchFacet
 File copyConfigToSolrHome(InputStream configIs, String parentDir, String configFileSimpleName)
           
 void destroy()
           
protected  boolean facetIsAvailable(SearchFacet facet, Map<String,String[]> params)
          Checks to see if the requiredFacets condition for a given facet is met.
 ProductSearchResult findExplicitProductsByCategory(Category category, ProductSearchCriteria searchCriteria)
          Performs a search for products in the given category, taking into consideration the ProductSearchCriteria This method will NOT return products that are in a sub-level of a given category.
protected  ProductSearchResult findProducts(String qualifiedSolrQuery, List<SearchFacetDTO> facets, ProductSearchCriteria searchCriteria, String defaultSort)
          Deprecated. in favor of the other findProducts() method
protected  ProductSearchResult findProducts(String qualifiedSolrQuery, List<SearchFacetDTO> facets, ProductSearchCriteria searchCriteria, String defaultSort, String... filterQueries)
          Given a qualified solr query string (such as "category:2002"), actually performs a solr search.
 ProductSearchResult findProductsByCategory(Category category, ProductSearchCriteria searchCriteria)
          Performs a search for products in the given category, taking into consideration the ProductSearchCriteria This method will return products that are in any sub-level of a given category.
 ProductSearchResult findProductsByCategoryAndQuery(Category category, String query, ProductSearchCriteria searchCriteria)
          Performs a search for products in the given category for the given query, taking into consideration the ProductSearchCriteria
 ProductSearchResult findProductsByQuery(String query, ProductSearchCriteria searchCriteria)
          Performs a search for products across all categories for the given query, taking into consideration the ProductSearchCriteria
 List<SearchFacetDTO> getCategoryFacets(Category category)
          Gets all available facets for a given category
 String getLocalePrefix()
           
protected  Map<String,SearchFacetDTO> getNamedFacetMap(List<SearchFacetDTO> facets, ProductSearchCriteria searchCriteria)
           
protected  List<Product> getProducts(org.apache.solr.client.solrj.response.QueryResponse response)
          Given a list of product IDs from solr, this method will look up the IDs via the productDao and build out actual Product instances.
 List<SearchFacetDTO> getSearchFacets()
          Gets all available facets for search results page
protected  String getSolrFieldKey(Field field, ProductSearchCriteria searchCriteria)
          This method will be used to map a field abbreviation to the appropriate solr index field to use.
protected  Map<String,String> getSolrFieldKeyMap(ProductSearchCriteria searchCriteria)
           
protected  String getSolrFieldString(String indexField, SearchFacetRange range)
          Returns a field string.
protected  String getSolrFieldTag(String tagField, String tag)
          Returns a solr field tag.
protected  String getSolrTaggedFieldString(String indexField, String tagField, String tag, SearchFacetRange range)
          Returns a fully composed solr field string.
 void rebuildIndex()
          This method delegates to SolrIndexService.rebuildIndex().
protected  String sanitizeQuery(String query)
          Perform any necessary query sanitation here.
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.
 void setPagingAttributes(ProductSearchResult result, org.apache.solr.client.solrj.response.QueryResponse response, ProductSearchCriteria searchCriteria)
          Sets the total results, the current page, and the page size on the ProductSearchResult.
protected  void sortFacetResults(Map<String,SearchFacetDTO> namedFacetMap)
          Invoked to sort the facet results.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

productDao

protected ProductDao productDao

fieldDao

protected FieldDao fieldDao

searchFacetDao

protected SearchFacetDao searchFacetDao

shs

protected SolrHelperService shs

solrIndexService

protected SolrIndexService solrIndexService

extensionManager

protected SolrSearchServiceExtensionManager extensionManager
Constructor Detail

SolrSearchServiceImpl

public SolrSearchServiceImpl(String solrServer)
                      throws IOException,
                             ParserConfigurationException,
                             SAXException
Throws:
IOException
ParserConfigurationException
SAXException

SolrSearchServiceImpl

public SolrSearchServiceImpl(org.apache.solr.client.solrj.SolrServer solrServer)

SolrSearchServiceImpl

public SolrSearchServiceImpl(String solrServer,
                             String reindexServer)
                      throws IOException,
                             ParserConfigurationException,
                             SAXException
This constructor serves to mimic the one below this, which takes in two SolrServer arguments. By having this and then simply disregarding the second parameter, we can more easily support 2-core Solr configurations that use embedded/standalone per environment.

Parameters:
solrServer -
reindexServer -
Throws:
SAXException
ParserConfigurationException
IOException

SolrSearchServiceImpl

public SolrSearchServiceImpl(org.apache.solr.client.solrj.SolrServer solrServer,
                             org.apache.solr.client.solrj.SolrServer reindexServer)
Method Detail

copyConfigToSolrHome

public File copyConfigToSolrHome(InputStream configIs,
                                 String parentDir,
                                 String configFileSimpleName)
                          throws IOException
Throws:
IOException

rebuildIndex

public void rebuildIndex()
                  throws ServiceException,
                         IOException
Description copied from interface: SearchService
This method delegates to SolrIndexService.rebuildIndex(). It is here to preserve backwards-compatibility with sites that were originally configured to run Broadleaf with Solr before 2.2.0.

Specified by:
rebuildIndex in interface SearchService
Throws:
ServiceException
IOException

destroy

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

findExplicitProductsByCategory

public ProductSearchResult findExplicitProductsByCategory(Category category,
                                                          ProductSearchCriteria searchCriteria)
                                                   throws ServiceException
Description copied from interface: SearchService
Performs a search for products in the given category, taking into consideration the ProductSearchCriteria This method will NOT return products 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 products in "Routers", would NOT return products that are in the "Enterprise Routers" category.

Specified by:
findExplicitProductsByCategory in interface SearchService
Returns:
Throws:
ServiceException
See Also:
SearchService.findProductsByCategory(Category, ProductSearchCriteria)

findProductsByCategory

public ProductSearchResult findProductsByCategory(Category category,
                                                  ProductSearchCriteria searchCriteria)
                                           throws ServiceException
Description copied from interface: SearchService
Performs a search for products in the given category, taking into consideration the ProductSearchCriteria This method will return products 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 products in "Routers", would return products that are in the "Enterprise Routers" category.

Specified by:
findProductsByCategory in interface SearchService
Returns:
the result of the search
Throws:
ServiceException
See Also:
SearchService.findExplicitProductsByCategory(Category, ProductSearchCriteria)

findProductsByQuery

public ProductSearchResult findProductsByQuery(String query,
                                               ProductSearchCriteria searchCriteria)
                                        throws ServiceException
Description copied from interface: SearchService
Performs a search for products across all categories for the given query, taking into consideration the ProductSearchCriteria

Specified by:
findProductsByQuery in interface SearchService
Returns:
the result of the search
Throws:
ServiceException

findProductsByCategoryAndQuery

public ProductSearchResult findProductsByCategoryAndQuery(Category category,
                                                          String query,
                                                          ProductSearchCriteria searchCriteria)
                                                   throws ServiceException
Description copied from interface: SearchService
Performs a search for products in the given category for the given query, taking into consideration the ProductSearchCriteria

Specified by:
findProductsByCategoryAndQuery in interface SearchService
Throws:
ServiceException

getLocalePrefix

public String getLocalePrefix()

buildQueryFieldsString

protected String buildQueryFieldsString()

appendFieldToQuery

protected void appendFieldToQuery(StringBuilder queryBuilder,
                                  Field currentField)

findProducts

protected ProductSearchResult findProducts(String qualifiedSolrQuery,
                                           List<SearchFacetDTO> facets,
                                           ProductSearchCriteria searchCriteria,
                                           String defaultSort)
                                    throws ServiceException
Deprecated. in favor of the other findProducts() method

Throws:
ServiceException

findProducts

protected ProductSearchResult findProducts(String qualifiedSolrQuery,
                                           List<SearchFacetDTO> facets,
                                           ProductSearchCriteria 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:
qualifiedSolrQuery -
facets -
searchCriteria -
Returns:
the ProductSearchResult of the search
Throws:
ServiceException

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

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,
                                ProductSearchCriteria 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,
                                        ProductSearchCriteria searchCriteria)
Restricts the query by adding active facet filters.

Parameters:
query -
namedFacetMap -
searchCriteria -

attachFacets

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

Parameters:
query -
namedFacetMap -

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(ProductSearchResult result,
                                org.apache.solr.client.solrj.response.QueryResponse response,
                                ProductSearchCriteria 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 -
response -
searchCriteria -

getProducts

protected List<Product> getProducts(org.apache.solr.client.solrj.response.QueryResponse response)
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:
response -
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 -
request -
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

getSolrFieldString

protected String getSolrFieldString(String indexField,
                                    SearchFacetRange range)
Returns a field string. Given indexField = a and a non-null range, would produce the following String: a:[minVal TO maxVal]


getSolrTaggedFieldString

protected String getSolrTaggedFieldString(String indexField,
                                          String tagField,
                                          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: {!ex=a}a:[minVal TO maxVal]


getSolrFieldTag

protected String getSolrFieldTag(String tagField,
                                 String tag)
Returns a solr field tag. Given indexField = a, tag = ex, would produce the following String: {!ex=a}


getNamedFacetMap

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

getSolrFieldKey

protected String getSolrFieldKey(Field field,
                                 ProductSearchCriteria searchCriteria)
This method will be used to map a field abbreviation to the appropriate solr index field to use. Typically, this default implementation that maps to the facet field type will be sufficient. However, there may be cases where you would want to use a different solr index depending on other currently active facets. In that case, you would associate that mapping here. For example, for the "price" abbreviation, we would generally want to use "defaultSku.retailPrice_td". However, if a secondary facet on item condition is selected (such as "refurbished", we may want to index "price" to "refurbishedSku.retailPrice_td". That mapping occurs here.

Parameters:
fields -
searchCriteria - the searchCriteria in case it is needed to determine the field key
Returns:
the solr field index key to use

getSolrFieldKeyMap

protected Map<String,String> getSolrFieldKeyMap(ProductSearchCriteria searchCriteria)
Parameters:
searchCriteria -
Returns:
a map of abbreviated key to fully qualified solr index field key for all product fields


Copyright © 2013. All Rights Reserved.