Search in sources :

Example 6 with SearchFacetDTO

use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.

the class SolrHelperServiceImpl method attachActiveFacetFilters.

@Override
public void attachActiveFacetFilters(SolrQuery query, Map<String, SearchFacetDTO> namedFacetMap, SearchCriteria searchCriteria) {
    if (searchCriteria.getFilterCriteria() != null) {
        for (Entry<String, String[]> entry : searchCriteria.getFilterCriteria().entrySet()) {
            String solrKey = null;
            for (Entry<String, SearchFacetDTO> dtoEntry : namedFacetMap.entrySet()) {
                if (dtoEntry.getValue().getFacet().getField().getAbbreviation().equals(entry.getKey())) {
                    solrKey = dtoEntry.getKey();
                    dtoEntry.getValue().setActive(true);
                }
            }
            if (solrKey != null) {
                String[] selectedValues = entry.getValue().clone();
                boolean rangeQuery = false;
                for (int i = 0; i < selectedValues.length; i++) {
                    if (selectedValues[i].contains("range[")) {
                        rangeQuery = true;
                        String rangeValue = selectedValues[i].substring(selectedValues[i].indexOf('[') + 1, selectedValues[i].indexOf(']'));
                        String[] rangeValues = StringUtils.split(rangeValue, ':');
                        BigDecimal minValue = new BigDecimal(rangeValues[0]);
                        BigDecimal maxValue = null;
                        if (!rangeValues[1].equals("null")) {
                            maxValue = new BigDecimal(rangeValues[1]);
                        }
                        selectedValues[i] = getSolrRangeString(solrKey, minValue, maxValue);
                    } else {
                        selectedValues[i] = "\"" + scrubFacetValue(selectedValues[i]) + "\"";
                    }
                }
                List<String> valueStrings = new ArrayList<>();
                ExtensionResultStatusType status = searchExtensionManager.getProxy().buildActiveFacetFilter(namedFacetMap.get(solrKey).getFacet(), selectedValues, valueStrings);
                if (ExtensionResultStatusType.NOT_HANDLED.equals(status)) {
                    StringBuilder valueString = new StringBuilder();
                    if (rangeQuery) {
                        valueString.append(solrKey).append(":(");
                        valueString.append(StringUtils.join(selectedValues, " OR "));
                        valueString.append(")");
                    } else {
                        valueString.append("{!tag=").append(solrKey).append("}");
                        valueString.append(solrKey).append(":(");
                        valueString.append(StringUtils.join(selectedValues, " OR "));
                        valueString.append(")");
                    }
                    valueStrings.add(valueString.toString());
                }
                query.addFilterQuery(valueStrings.toArray(new String[valueStrings.size()]));
            }
        }
    }
}
Also used : SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) ArrayList(java.util.ArrayList) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) BigDecimal(java.math.BigDecimal)

Example 7 with SearchFacetDTO

use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.

the class SolrHelperServiceImpl method buildSearchFacetDTOs.

@Override
public List<SearchFacetDTO> buildSearchFacetDTOs(List<SearchFacet> searchFacets) {
    List<SearchFacetDTO> facets = new ArrayList<SearchFacetDTO>();
    Map<String, String[]> requestParameters = BroadleafRequestContext.getRequestParameterMap();
    for (SearchFacet facet : searchFacets) {
        if (isFacetAvailable(facet, requestParameters)) {
            SearchFacetDTO dto = new SearchFacetDTO();
            dto.setFacet(facet);
            dto.setShowQuantity(true);
            facets.add(dto);
        }
    }
    return facets;
}
Also used : SearchFacet(org.broadleafcommerce.core.search.domain.SearchFacet) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) ArrayList(java.util.ArrayList)

Example 8 with SearchFacetDTO

use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.

the class SolrHelperServiceImpl method setFacetResults.

@Override
public void setFacetResults(Map<String, SearchFacetDTO> namedFacetMap, QueryResponse response) {
    if (response.getFacetFields() != null) {
        for (FacetField facet : response.getFacetFields()) {
            String facetFieldName = facet.getName();
            SearchFacetDTO facetDTO = namedFacetMap.get(facetFieldName);
            for (Count value : facet.getValues()) {
                SearchFacetResultDTO resultDTO = new SearchFacetResultDTO();
                resultDTO.setFacet(facetDTO.getFacet());
                resultDTO.setQuantity(new Long(value.getCount()).intValue());
                resultDTO.setValue(value.getName());
                facetDTO.getFacetValues().add(resultDTO);
            }
        }
    }
    if (response.getFacetQuery() != null) {
        for (Entry<String, Integer> entry : response.getFacetQuery().entrySet()) {
            String key = entry.getKey();
            String facetFieldName = key.substring(0, key.indexOf("["));
            SearchFacetDTO facetDTO = namedFacetMap.get(facetFieldName);
            String minValue = key.substring(key.indexOf("[") + 1, key.indexOf(":"));
            String maxValue = key.substring(key.indexOf(":") + 1, key.indexOf("]"));
            if (maxValue.equals("*")) {
                maxValue = null;
            }
            SearchFacetResultDTO resultDTO = new SearchFacetResultDTO();
            resultDTO.setFacet(facetDTO.getFacet());
            resultDTO.setQuantity(entry.getValue());
            resultDTO.setMinValue(new BigDecimal(minValue));
            resultDTO.setMaxValue(maxValue == null ? null : new BigDecimal(maxValue));
            facetDTO.getFacetValues().add(resultDTO);
        }
    }
    searchExtensionManager.getProxy().setFacetResults(namedFacetMap, response);
}
Also used : SearchFacetResultDTO(org.broadleafcommerce.core.search.domain.SearchFacetResultDTO) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) FacetField(org.apache.solr.client.solrj.response.FacetField) Count(org.apache.solr.client.solrj.response.FacetField.Count) BigDecimal(java.math.BigDecimal)

Example 9 with SearchFacetDTO

use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.

the class DatabaseSearchServiceImpl method buildSearchFacetDtos.

/**
 * Create the wrapper DTO around the SearchFacet
 * @param categoryFacets
 * @return the wrapper DTO
 */
protected List<SearchFacetDTO> buildSearchFacetDtos(List<SearchFacet> categoryFacets) {
    List<SearchFacetDTO> facets = new ArrayList<SearchFacetDTO>();
    for (SearchFacet facet : categoryFacets) {
        SearchFacetDTO dto = new SearchFacetDTO();
        dto.setFacet(facet);
        dto.setShowQuantity(false);
        dto.setFacetValues(getFacetValues(facet));
        dto.setActive(false);
        facets.add(dto);
    }
    return facets;
}
Also used : SearchFacet(org.broadleafcommerce.core.search.domain.SearchFacet) CategorySearchFacet(org.broadleafcommerce.core.search.domain.CategorySearchFacet) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) ArrayList(java.util.ArrayList)

Example 10 with SearchFacetDTO

use of org.broadleafcommerce.core.search.domain.SearchFacetDTO in project BroadleafCommerce by BroadleafCommerce.

the class DatabaseSearchServiceImpl method findSearchResultsByQuery.

@Override
public SearchResult findSearchResultsByQuery(String query, SearchCriteria searchCriteria) {
    SearchResult result = new SearchResult();
    setQualifiedKeys(searchCriteria);
    List<Product> products = catalogService.findFilteredActiveProductsByQuery(query, searchCriteria);
    List<SearchFacetDTO> facets = getSearchFacets();
    setActiveFacets(facets, searchCriteria);
    result.setProducts(products);
    result.setFacets(facets);
    result.setTotalResults(products.size());
    result.setPage(1);
    result.setPageSize(products.size());
    return result;
}
Also used : SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) Product(org.broadleafcommerce.core.catalog.domain.Product) SearchResult(org.broadleafcommerce.core.search.domain.SearchResult)

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