use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.
the class RemoveFacetValuesLinkProcessor method getModifiedAttributes.
@Override
public BroadleafAttributeModifier getModifiedAttributes(String tagName, Map<String, String> tagAttributes, String attributeName, String attributeValue, BroadleafTemplateContext context) {
BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
HttpServletRequest request = blcContext.getRequest();
String baseUrl = request.getRequestURL().toString();
Map<String, String[]> params = new HashMap<>(request.getParameterMap());
SearchFacetDTO facet = (SearchFacetDTO) context.parseExpression(attributeValue);
String key = searchFacetDTOService.getUrlKey(facet);
params.remove(key);
params.remove(SearchCriteria.PAGE_NUMBER);
String url = ProcessorUtils.getUrl(baseUrl, params);
Map<String, String> newAttributes = new HashMap<>();
newAttributes.put("href", url);
return new BroadleafAttributeModifier(newAttributes);
}
use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.
the class SolrSearchServiceImpl method filterEmptyFacets.
protected void filterEmptyFacets(List<SearchFacetDTO> facets) {
Iterator<SearchFacetDTO> iter = facets.iterator();
while (iter.hasNext()) {
SearchFacetDTO dto = iter.next();
boolean shouldRemove = true;
for (SearchFacetResultDTO result : dto.getFacetValues()) {
if (result.getQuantity() != null && result.getQuantity() > 0) {
shouldRemove = false;
break;
}
}
if (shouldRemove) {
iter.remove();
}
}
}
use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.
the class SolrSearchServiceImpl method findSearchResults.
/**
* 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.
*
* @param searchCriteria
* @param facets
* @return the ProductSearchResult of the search
* @throws ServiceException
*/
protected SearchResult findSearchResults(String qualifiedSolrQuery, List<SearchFacetDTO> facets, SearchCriteria searchCriteria, String defaultSort, String... filterQueries) throws ServiceException {
Map<String, SearchFacetDTO> namedFacetMap = getNamedFacetMap(facets, searchCriteria);
// Left here for backwards compatibility for this method signature
if (searchCriteria.getQuery() == null && qualifiedSolrQuery != null) {
searchCriteria.setQuery(qualifiedSolrQuery);
}
// Build the basic query
// Solr queries with a 'start' parameter cannot be a negative number
int start = (searchCriteria.getPage() <= 0) ? 0 : (searchCriteria.getPage() - 1);
SolrQuery solrQuery = new SolrQuery().setQuery(searchCriteria.getQuery()).setRows(searchCriteria.getPageSize()).setStart((start) * searchCriteria.getPageSize()).setRequestHandler(searchCriteria.getRequestHandler());
// This is for SolrCloud. We assume that we are always searching against a collection aliased as "PRIMARY"
if (solrConfiguration.isSiteCollections()) {
solrQuery.setParam("collection", solrConfiguration.getSiteAliasName(BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite()));
} else {
// This should be ignored if not using SolrCloud
solrQuery.setParam("collection", solrConfiguration.getPrimaryName());
}
solrQuery.setFields(shs.getIndexableIdFieldName());
if (filterQueries != null) {
solrQuery.setFilterQueries(filterQueries);
}
// add category filter if applicable
if (searchCriteria.getCategory() != null) {
solrQuery.addFilterQuery(getCategoryFilter(searchCriteria));
}
solrQuery.addFilterQuery(shs.getNamespaceFieldName() + ":(\"" + solrConfiguration.getNamespace() + "\")");
solrQuery.set("defType", "edismax");
solrQuery.set("qf", buildQueryFieldsString(solrQuery, searchCriteria));
// Attach additional restrictions
attachActiveFacetFilters(solrQuery, namedFacetMap, searchCriteria);
attachFacets(solrQuery, namedFacetMap, searchCriteria);
modifySolrQuery(solrQuery, searchCriteria.getQuery(), facets, searchCriteria, defaultSort);
// on child documents.
if (StringUtils.isNotBlank(defaultSort) || StringUtils.isNotBlank(searchCriteria.getSortQuery())) {
solrQuery.remove("bq");
solrQuery.remove("bf");
solrQuery.remove("boost");
}
attachSortClause(solrQuery, searchCriteria, defaultSort);
solrQuery.setShowDebugInfo(shouldShowDebugQuery());
if (LOG.isTraceEnabled()) {
try {
LOG.trace(URLDecoder.decode(solrQuery.toString(), "UTF-8"));
} catch (Exception e) {
LOG.trace("Couldn't UTF-8 URL Decode: " + solrQuery.toString());
}
}
// Query solr
QueryResponse response;
List<SolrDocument> responseDocuments;
int numResults = 0;
try {
response = solrConfiguration.getServer().query(solrQuery, getSolrQueryMethod());
responseDocuments = getResponseDocuments(response);
numResults = (int) response.getResults().getNumFound();
if (LOG.isTraceEnabled()) {
LOG.trace(response.toString());
for (SolrDocument doc : responseDocuments) {
LOG.trace(doc);
}
}
} catch (SolrServerException e) {
throw new ServiceException("Could not perform search", e);
} catch (IOException e) {
throw new ServiceException("Could not perform search", e);
}
// Get the facets
setFacetResults(namedFacetMap, response);
sortFacetResults(namedFacetMap);
filterEmptyFacets(facets);
SearchResult result = new SearchResult();
result.setFacets(facets);
result.setQueryResponse(response);
setPagingAttributes(result, numResults, searchCriteria);
if (useSku) {
List<Sku> skus = getSkus(responseDocuments);
result.setSkus(skus);
} else {
// Get the products
List<Product> products = getProducts(responseDocuments);
result.setProducts(products);
}
return result;
}
use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.
the class DatabaseSearchServiceImpl method getSearchFacets.
@Override
@SuppressWarnings("unchecked")
public List<SearchFacetDTO> getSearchFacets() {
List<SearchFacetDTO> facets = null;
String cacheKey = CACHE_KEY_PREFIX + "blc-search";
Element element = cache.get(cacheKey);
if (element != null) {
facets = (List<SearchFacetDTO>) element.getValue();
}
if (facets == null) {
facets = buildSearchFacetDtos(searchFacetDao.readAllSearchFacets(FieldEntity.PRODUCT));
element = new Element(cacheKey, facets);
cache.put(element);
}
return facets;
}
use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.
the class SolrHelperServiceImpl method attachFacets.
@Override
public void attachFacets(SolrQuery query, Map<String, SearchFacetDTO> namedFacetMap, SearchCriteria searchCriteria) {
query.setFacet(true);
for (Entry<String, SearchFacetDTO> entry : namedFacetMap.entrySet()) {
SearchFacetDTO dto = entry.getValue();
ExtensionResultStatusType status = searchExtensionManager.getProxy().attachFacet(query, entry.getKey(), dto, searchCriteria);
if (ExtensionResultStatusType.NOT_HANDLED.equals(status)) {
List<SearchFacetRange> facetRanges = searchFacetDao.readSearchFacetRangesForSearchFacet(dto.getFacet());
if (searchExtensionManager != null) {
searchExtensionManager.getProxy().filterSearchFacetRanges(dto, facetRanges);
}
if (CollectionUtils.isNotEmpty(facetRanges)) {
for (SearchFacetRange range : facetRanges) {
query.addFacetQuery(getSolrTaggedFieldString(entry.getKey(), "key", range));
}
} else {
query.addFacetField(getSolrTaggedFieldString(entry.getKey(), "ex", null));
}
}
}
}
Aggregations