Search in sources :

Example 1 with FacetPageHandler

use of com.publiccms.common.handler.FacetPageHandler 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)

Example 2 with FacetPageHandler

use of com.publiccms.common.handler.FacetPageHandler in project PublicCMS-preview by sanluan.

the class CmsFacetSearchDirective method execute.

@Override
public void execute(RenderHandler handler) throws IOException, Exception {
    String word = handler.getString("word");
    Long[] tagIds = handler.getLongArray("tagId");
    if (CommonUtils.notEmpty(word) || CommonUtils.notEmpty(tagIds)) {
        SysSite site = getSite(handler);
        if (CommonUtils.notEmpty(word)) {
            statisticsComponent.search(site.getId(), word);
        }
        if (CommonUtils.notEmpty(tagIds)) {
            for (Long tagId : tagIds) {
                statisticsComponent.searchTag(tagId);
            }
        }
        PageHandler page;
        Integer pageIndex = handler.getInteger("pageIndex", 1);
        Integer count = handler.getInteger("count", 30);
        try {
            page = service.facetQuery(site.getId(), handler.getStringArray("categoryId"), handler.getStringArray("modelId"), word, tagIds, handler.getDate("startPublishDate"), handler.getDate("endPublishDate", CommonUtils.getDate()), pageIndex, count);
        } catch (Exception e) {
            page = new FacetPageHandler(pageIndex, count, 0, null);
        }
        handler.put("page", page).render();
    }
}
Also used : FacetPageHandler(com.publiccms.common.handler.FacetPageHandler) PageHandler(com.publiccms.common.handler.PageHandler) IOException(java.io.IOException) FacetPageHandler(com.publiccms.common.handler.FacetPageHandler) SysSite(com.publiccms.entities.sys.SysSite)

Aggregations

FacetPageHandler (com.publiccms.common.handler.FacetPageHandler)2 PageHandler (com.publiccms.common.handler.PageHandler)1 SysSite (com.publiccms.entities.sys.SysSite)1 IOException (java.io.IOException)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