All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.search.grouping.SecondPassGroupingCollector Maven / Gradle / Ivy

There is a newer version: 9.11.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.lucene.search.grouping;

import java.io.IOException;
import java.util.Collection;
import java.util.Objects;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Scorable;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.SimpleCollector;

/**
 * SecondPassGroupingCollector runs over an already collected set of groups, further applying a
 * {@link GroupReducer} to each group
 *
 * @see TopGroupsCollector
 * @see DistinctValuesCollector
 * @lucene.experimental
 */
public class SecondPassGroupingCollector extends SimpleCollector {

  protected final GroupSelector groupSelector;
  protected final Collection> groups;
  protected final GroupReducer groupReducer;

  protected int totalHitCount;
  protected int totalGroupedHitCount;

  /**
   * Create a new SecondPassGroupingCollector
   *
   * @param groupSelector the GroupSelector that defines groups for this search
   * @param groups the groups to collect documents for
   * @param reducer the reducer to apply to each group
   */
  public SecondPassGroupingCollector(
      GroupSelector groupSelector,
      Collection> groups,
      GroupReducer reducer) {

    // System.out.println("SP init");
    if (groups.isEmpty()) {
      throw new IllegalArgumentException("no groups to collect (groups is empty)");
    }

    this.groupSelector = Objects.requireNonNull(groupSelector);
    this.groupSelector.setGroups(groups);

    this.groups = Objects.requireNonNull(groups);
    this.groupReducer = reducer;
    reducer.setGroups(groups);
  }

  /**
   * @return the GroupSelector used in this collector
   */
  public GroupSelector getGroupSelector() {
    return groupSelector;
  }

  @Override
  public ScoreMode scoreMode() {
    return groupReducer.needsScores() ? ScoreMode.COMPLETE : ScoreMode.COMPLETE_NO_SCORES;
  }

  @Override
  public void setScorer(Scorable scorer) throws IOException {
    groupSelector.setScorer(scorer);
    groupReducer.setScorer(scorer);
  }

  @Override
  public void collect(int doc) throws IOException {
    totalHitCount++;
    if (groupSelector.advanceTo(doc) == GroupSelector.State.SKIP) {
      return;
    }
    totalGroupedHitCount++;
    T value = groupSelector.currentValue();
    groupReducer.collect(value, doc);
  }

  @Override
  protected void doSetNextReader(LeafReaderContext readerContext) throws IOException {
    groupReducer.setNextReader(readerContext);
    groupSelector.setNextReader(readerContext);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy