Search in sources :

Example 1 with FacetManager

use of org.hibernate.search.query.engine.spi.FacetManager in project PublicCMS-preview by sanluan.

the class BaseDao method getFacetPage.

/**
 * @param fullTextQuery
 * @param facetFields
 * @param valueMap
 * @param pageIndex
 * @param pageSize
 * @param maxResults
 * @return facet results page
 */
protected FacetPageHandler getFacetPage(QueryBuilder queryBuilder, FullTextQuery fullTextQuery, String[] facetFields, Map<String, List<String>> valueMap, int facetCount, Integer pageIndex, Integer pageSize, Integer maxResults) {
    FacetManager facetManager = fullTextQuery.getFacetManager();
    for (String facetField : facetFields) {
        FacetingRequest facetingRequest = queryBuilder.facet().name(facetField + FACET_NAME_SUFFIX).onField(facetField).discrete().orderedBy(FacetSortOrder.COUNT_DESC).includeZeroCounts(false).maxFacetCount(facetCount).createFacetingRequest();
        facetManager.enableFaceting(facetingRequest);
    }
    FacetPageHandler page = new FacetPageHandler(pageIndex, pageSize, fullTextQuery.getResultSize(), maxResults);
    if (0 < page.getTotalCount()) {
        Set<String> facetSet = new LinkedHashSet<>();
        facetSet.addAll(valueMap.keySet());
        facetSet.addAll(Arrays.asList(facetFields));
        for (String facetField : facetSet) {
            String facetingName = facetField + FACET_NAME_SUFFIX;
            List<Facet> facets = facetManager.getFacets(facetingName);
            Map<String, Integer> facetMap = new LinkedHashMap<>();
            List<String> valueList = valueMap.get(facetField);
            if (null != valueList) {
                List<Facet> facetList = new ArrayList<>();
                for (Facet facet : facets) {
                    facetMap.put(facet.getValue(), facet.getCount());
                    if (valueList.contains(facet.getValue())) {
                        facetList.add(facet);
                    }
                }
                if (!facetList.isEmpty()) {
                    facetManager.getFacetGroup(facetingName).selectFacets(facetList.toArray(new Facet[facetList.size()]));
                }
            } else {
                for (Facet facet : facets) {
                    facetMap.put(facet.getValue(), facet.getCount());
                }
            }
            page.getFacetMap().put(facetField, facetMap);
        }
        page.setTotalCount(fullTextQuery.getResultSize(), maxResults);
        page.init();
    }
    if (CommonUtils.notEmpty(pageSize)) {
        fullTextQuery.setFirstResult(page.getFirstResult()).setMaxResults(page.getPageSize());
    }
    page.setList(fullTextQuery.list());
    return page;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) FacetPageHandler(com.publiccms.common.handler.FacetPageHandler) LinkedHashMap(java.util.LinkedHashMap) FacetingRequest(org.hibernate.search.query.facet.FacetingRequest) FacetManager(org.hibernate.search.query.engine.spi.FacetManager) Facet(org.hibernate.search.query.facet.Facet)

Aggregations

FacetPageHandler (com.publiccms.common.handler.FacetPageHandler)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 FacetManager (org.hibernate.search.query.engine.spi.FacetManager)1 Facet (org.hibernate.search.query.facet.Facet)1 FacetingRequest (org.hibernate.search.query.facet.FacetingRequest)1