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()]));
}
}
}
}
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;
}
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);
}
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;
}
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;
}
Aggregations