Search in sources :

Example 1 with SearchFacetDTO

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) BroadleafAttributeModifier(org.broadleafcommerce.presentation.model.BroadleafAttributeModifier)

Example 2 with SearchFacetDTO

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();
        }
    }
}
Also used : SearchFacetResultDTO(org.broadleafcommerce.core.search.domain.SearchFacetResultDTO) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO)

Example 3 with SearchFacetDTO

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;
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) Product(org.broadleafcommerce.core.catalog.domain.Product) SearchResult(org.broadleafcommerce.core.search.domain.SearchResult) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ServiceException(org.broadleafcommerce.common.exception.ServiceException) IOException(java.io.IOException) SolrDocument(org.apache.solr.common.SolrDocument) ServiceException(org.broadleafcommerce.common.exception.ServiceException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 4 with SearchFacetDTO

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;
}
Also used : Element(net.sf.ehcache.Element) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO)

Example 5 with SearchFacetDTO

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));
            }
        }
    }
}
Also used : SearchFacetRange(org.broadleafcommerce.core.search.domain.SearchFacetRange) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType)

Aggregations

SearchFacetDTO (org.broadleafcommerce.core.search.domain.SearchFacetDTO)12 ArrayList (java.util.ArrayList)4 Product (org.broadleafcommerce.core.catalog.domain.Product)3 SearchFacet (org.broadleafcommerce.core.search.domain.SearchFacet)3 SearchResult (org.broadleafcommerce.core.search.domain.SearchResult)3 BigDecimal (java.math.BigDecimal)2 Element (net.sf.ehcache.Element)2 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)2 CategorySearchFacet (org.broadleafcommerce.core.search.domain.CategorySearchFacet)2 SearchFacetResultDTO (org.broadleafcommerce.core.search.domain.SearchFacetResultDTO)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 FacetField (org.apache.solr.client.solrj.response.FacetField)1 Count (org.apache.solr.client.solrj.response.FacetField.Count)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1