Search in sources :

Example 1 with TermGroupSelector

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

the class SimpleFacets method getGroupedFacetQueryCount.

/**
   * Returns a grouped facet count for the facet query
   *
   * @see FacetParams#FACET_QUERY
   */
public int getGroupedFacetQueryCount(Query facetQuery, DocSet docSet) throws IOException {
    // It is okay to retrieve group.field from global because it is never a local param
    String groupField = global.get(GroupParams.GROUP_FIELD);
    if (groupField == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Specify the group.field as parameter or local parameter");
    }
    AllGroupsCollector collector = new AllGroupsCollector<>(new TermGroupSelector(groupField));
    // This returns a filter that only matches documents matching with q param and fq params
    Filter mainQueryFilter = docSet.getTopFilter();
    Query filteredFacetQuery = new BooleanQuery.Builder().add(facetQuery, Occur.MUST).add(mainQueryFilter, Occur.FILTER).build();
    searcher.search(filteredFacetQuery, collector);
    return collector.getGroupCount();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) Filter(org.apache.solr.search.Filter) AllGroupsCollector(org.apache.lucene.search.grouping.AllGroupsCollector) TermGroupSelector(org.apache.lucene.search.grouping.TermGroupSelector) SolrException(org.apache.solr.common.SolrException)

Example 2 with TermGroupSelector

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

the class CommandHandler method computeGroupedDocSet.

private DocSet computeGroupedDocSet(Query query, ProcessedFilter filter, List<Collector> collectors) throws IOException {
    Command firstCommand = commands.get(0);
    String field = firstCommand.getKey();
    SchemaField sf = searcher.getSchema().getField(field);
    FieldType fieldType = sf.getType();
    final AllGroupHeadsCollector allGroupHeadsCollector;
    if (fieldType.getNumberType() != null) {
        ValueSource vs = fieldType.getValueSource(sf, null);
        allGroupHeadsCollector = AllGroupHeadsCollector.newCollector(new ValueSourceGroupSelector(vs, new HashMap<>()), firstCommand.getWithinGroupSort());
    } else {
        allGroupHeadsCollector = AllGroupHeadsCollector.newCollector(new TermGroupSelector(firstCommand.getKey()), firstCommand.getWithinGroupSort());
    }
    if (collectors.isEmpty()) {
        searchWithTimeLimiter(query, filter, allGroupHeadsCollector);
    } else {
        collectors.add(allGroupHeadsCollector);
        searchWithTimeLimiter(query, filter, MultiCollector.wrap(collectors.toArray(new Collector[collectors.size()])));
    }
    return new BitDocSet(allGroupHeadsCollector.retrieveGroupHeads(searcher.maxDoc()));
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) BitDocSet(org.apache.solr.search.BitDocSet) QueryCommand(org.apache.solr.search.QueryCommand) ValueSource(org.apache.lucene.queries.function.ValueSource) ValueSourceGroupSelector(org.apache.lucene.search.grouping.ValueSourceGroupSelector) TermGroupSelector(org.apache.lucene.search.grouping.TermGroupSelector) AllGroupHeadsCollector(org.apache.lucene.search.grouping.AllGroupHeadsCollector) FieldType(org.apache.solr.schema.FieldType)

Example 3 with TermGroupSelector

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

the class SearchGroupsFieldCommand method create.

@Override
public List<Collector> create() throws IOException {
    final List<Collector> collectors = new ArrayList<>(2);
    final FieldType fieldType = field.getType();
    if (topNGroups > 0) {
        if (fieldType.getNumberType() != null) {
            ValueSource vs = fieldType.getValueSource(field, null);
            firstPassGroupingCollector = new FirstPassGroupingCollector<>(new ValueSourceGroupSelector(vs, new HashMap<>()), groupSort, topNGroups);
        } else {
            firstPassGroupingCollector = new FirstPassGroupingCollector<>(new TermGroupSelector(field.getName()), groupSort, topNGroups);
        }
        collectors.add(firstPassGroupingCollector);
    }
    if (includeGroupCount) {
        if (fieldType.getNumberType() != null) {
            ValueSource vs = fieldType.getValueSource(field, null);
            allGroupsCollector = new AllGroupsCollector<>(new ValueSourceGroupSelector(vs, new HashMap<>()));
        } else {
            allGroupsCollector = new AllGroupsCollector<>(new TermGroupSelector(field.getName()));
        }
        collectors.add(allGroupsCollector);
    }
    return collectors;
}
Also used : ValueSource(org.apache.lucene.queries.function.ValueSource) ArrayList(java.util.ArrayList) AllGroupsCollector(org.apache.lucene.search.grouping.AllGroupsCollector) Collector(org.apache.lucene.search.Collector) FirstPassGroupingCollector(org.apache.lucene.search.grouping.FirstPassGroupingCollector) ValueSourceGroupSelector(org.apache.lucene.search.grouping.ValueSourceGroupSelector) TermGroupSelector(org.apache.lucene.search.grouping.TermGroupSelector) FieldType(org.apache.solr.schema.FieldType)

Example 4 with TermGroupSelector

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

the class TopGroupsFieldCommand method create.

@Override
public List<Collector> create() throws IOException {
    if (firstPhaseGroups.isEmpty()) {
        return Collections.emptyList();
    }
    final List<Collector> collectors = new ArrayList<>(1);
    final FieldType fieldType = field.getType();
    if (fieldType.getNumberType() != null) {
        ValueSource vs = fieldType.getValueSource(field, null);
        Collection<SearchGroup<MutableValue>> v = GroupConverter.toMutable(field, firstPhaseGroups);
        secondPassCollector = new TopGroupsCollector<>(new ValueSourceGroupSelector(vs, new HashMap<>()), v, groupSort, withinGroupSort, maxDocPerGroup, needScores, needMaxScore, true);
    } else {
        secondPassCollector = new TopGroupsCollector<>(new TermGroupSelector(field.getName()), firstPhaseGroups, groupSort, withinGroupSort, maxDocPerGroup, needScores, needMaxScore, true);
    }
    collectors.add(secondPassCollector);
    return collectors;
}
Also used : SearchGroup(org.apache.lucene.search.grouping.SearchGroup) ValueSource(org.apache.lucene.queries.function.ValueSource) ArrayList(java.util.ArrayList) Collector(org.apache.lucene.search.Collector) TopGroupsCollector(org.apache.lucene.search.grouping.TopGroupsCollector) ValueSourceGroupSelector(org.apache.lucene.search.grouping.ValueSourceGroupSelector) TermGroupSelector(org.apache.lucene.search.grouping.TermGroupSelector) FieldType(org.apache.solr.schema.FieldType)

Aggregations

TermGroupSelector (org.apache.lucene.search.grouping.TermGroupSelector)4 ValueSource (org.apache.lucene.queries.function.ValueSource)3 ValueSourceGroupSelector (org.apache.lucene.search.grouping.ValueSourceGroupSelector)3 FieldType (org.apache.solr.schema.FieldType)3 ArrayList (java.util.ArrayList)2 Collector (org.apache.lucene.search.Collector)2 AllGroupsCollector (org.apache.lucene.search.grouping.AllGroupsCollector)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 Query (org.apache.lucene.search.Query)1 AllGroupHeadsCollector (org.apache.lucene.search.grouping.AllGroupHeadsCollector)1 FirstPassGroupingCollector (org.apache.lucene.search.grouping.FirstPassGroupingCollector)1 SearchGroup (org.apache.lucene.search.grouping.SearchGroup)1 TopGroupsCollector (org.apache.lucene.search.grouping.TopGroupsCollector)1 SolrException (org.apache.solr.common.SolrException)1 SchemaField (org.apache.solr.schema.SchemaField)1 BitDocSet (org.apache.solr.search.BitDocSet)1 Filter (org.apache.solr.search.Filter)1 QueryCommand (org.apache.solr.search.QueryCommand)1