Example usage for org.springframework.data.solr.core.query.result FacetPage getAllFacets

List of usage examples for org.springframework.data.solr.core.query.result FacetPage getAllFacets

Introduction

In this page you can find the example usage for org.springframework.data.solr.core.query.result FacetPage getAllFacets.

Prototype

Collection<Page<? extends FacetEntry>> getAllFacets();

Source Link

Usage

From source file:com.dm.estore.search.services.impl.CatalogSearchServiceImpl.java

@Override
public ProductSearchResultDto findProducts(ProductSearchDto searchRequest) {

    Sort sort = new Sort(Sort.Direction.fromString(searchRequest.getSortOrder()),
            searchRequest.getSortColumn());
    PageRequest pageRequest = new PageRequest(searchRequest.getPage(), searchRequest.getPageSize(), sort);
    FacetPage<CatalogItemDto> catalogItems = catalogSearchRepository.searchProducts(
            searchRequest.getSearchTerms(), searchRequest.getCategory(), searchRequest.getMaterials(),
            searchRequest.getColors(), pageRequest);

    ProductSearchResultDto result = new ProductSearchResultDto();
    result.setTotalCount(catalogItems.getTotalElements());

    for (Page<? extends FacetEntry> page : catalogItems.getAllFacets()) {
        for (FacetEntry facetEntry : page.getContent()) {
            String key = String.valueOf(facetEntry.getKey());
            String facetCode = facetEntry.getValue(); // name of the category
            long count = facetEntry.getValueCount(); // number of books in this category

            if (StringUtils.isEmpty(facetCode))
                continue;

            if (CatalogItemDto.FIELD_CATEGORY.equalsIgnoreCase(key)) {
                result.getCategoriesFacet().put(facetCode, count);
            } else if (CatalogItemDto.FIELD_VARIANT_GROUP.equalsIgnoreCase(key)) {
                result.getGroupFacet().put(facetCode, count);
            } else if (CatalogItemDto.FIELD_CATEGORY.equalsIgnoreCase(key)) {
                result.getSubGroupFacet().put(facetCode, count);
            }//from ww  w.  j  a va 2s.c  om
        }
    }

    result.setProducts(productConverter.convert(catalogItems.getContent()));
    result.setTotalCount((int) (catalogItems.getTotalElements()));
    return result;
}