Search in sources :

Example 1 with TermGroupFacetCollector

use of org.apache.lucene.search.grouping.TermGroupFacetCollector in project lucene-solr by apache.

the class SimpleFacets method getGroupedCounts.

public NamedList<Integer> getGroupedCounts(SolrIndexSearcher searcher, DocSet base, String field, boolean multiToken, int offset, int limit, int mincount, boolean missing, String sort, String prefix, Predicate<BytesRef> termFilter) throws IOException {
    GroupingSpecification groupingSpecification = rb.getGroupingSpec();
    final String groupField = groupingSpecification != null ? groupingSpecification.getFields()[0] : null;
    if (groupField == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Specify the group.field as parameter or local parameter");
    }
    BytesRef prefixBytesRef = prefix != null ? new BytesRef(prefix) : null;
    final TermGroupFacetCollector collector = TermGroupFacetCollector.createTermGroupFacetCollector(groupField, field, multiToken, prefixBytesRef, 128);
    Collector groupWrapper = getInsanityWrapper(groupField, collector);
    Collector fieldWrapper = getInsanityWrapper(field, groupWrapper);
    // When GroupedFacetCollector can handle numerics we can remove the wrapped collectors
    searcher.search(base.getTopFilter(), fieldWrapper);
    boolean orderByCount = sort.equals(FacetParams.FACET_SORT_COUNT) || sort.equals(FacetParams.FACET_SORT_COUNT_LEGACY);
    TermGroupFacetCollector.GroupedFacetResult result = collector.mergeSegmentResults(limit < 0 ? Integer.MAX_VALUE : (offset + limit), mincount, orderByCount);
    CharsRefBuilder charsRef = new CharsRefBuilder();
    FieldType facetFieldType = searcher.getSchema().getFieldType(field);
    NamedList<Integer> facetCounts = new NamedList<>();
    List<TermGroupFacetCollector.FacetEntry> scopedEntries = result.getFacetEntries(offset, limit < 0 ? Integer.MAX_VALUE : limit);
    for (TermGroupFacetCollector.FacetEntry facetEntry : scopedEntries) {
        //:TODO:can we filter earlier than this to make it more efficient?
        if (termFilter != null && !termFilter.test(facetEntry.getValue())) {
            continue;
        }
        facetFieldType.indexedToReadable(facetEntry.getValue(), charsRef);
        facetCounts.add(charsRef.toString(), facetEntry.getCount());
    }
    if (missing) {
        facetCounts.add(null, result.getTotalMissingCount());
    }
    return facetCounts;
}
Also used : TermGroupFacetCollector(org.apache.lucene.search.grouping.TermGroupFacetCollector) NamedList(org.apache.solr.common.util.NamedList) FieldType(org.apache.solr.schema.FieldType) AllGroupHeadsCollector(org.apache.lucene.search.grouping.AllGroupHeadsCollector) LeafCollector(org.apache.lucene.search.LeafCollector) AllGroupsCollector(org.apache.lucene.search.grouping.AllGroupsCollector) FilterCollector(org.apache.lucene.search.FilterCollector) TermGroupFacetCollector(org.apache.lucene.search.grouping.TermGroupFacetCollector) Collector(org.apache.lucene.search.Collector) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) GroupingSpecification(org.apache.solr.search.grouping.GroupingSpecification) SolrException(org.apache.solr.common.SolrException) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

Collector (org.apache.lucene.search.Collector)1 FilterCollector (org.apache.lucene.search.FilterCollector)1 LeafCollector (org.apache.lucene.search.LeafCollector)1 AllGroupHeadsCollector (org.apache.lucene.search.grouping.AllGroupHeadsCollector)1 AllGroupsCollector (org.apache.lucene.search.grouping.AllGroupsCollector)1 TermGroupFacetCollector (org.apache.lucene.search.grouping.TermGroupFacetCollector)1 BytesRef (org.apache.lucene.util.BytesRef)1 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)1 SolrException (org.apache.solr.common.SolrException)1 NamedList (org.apache.solr.common.util.NamedList)1 FieldType (org.apache.solr.schema.FieldType)1 GroupingSpecification (org.apache.solr.search.grouping.GroupingSpecification)1