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

org.apache.lucene.search.grouping.DistinctValuesCollector 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.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.SimpleCollector;

/**
 * A second pass grouping collector that keeps track of distinct values for a specified field for the top N group.
 *
 * @lucene.experimental
 */
public class DistinctValuesCollector extends SecondPassGroupingCollector {

  /**
   * Create a DistinctValuesCollector
   * @param groupSelector the group selector to determine the top-level groups
   * @param groups        the top-level groups to collect for
   * @param valueSelector a group selector to determine which values to collect per-group
   */
  public DistinctValuesCollector(GroupSelector groupSelector, Collection> groups,
                                       GroupSelector valueSelector) {
    super(groupSelector, groups, new DistinctValuesReducer<>(valueSelector));
  }

  private static class ValuesCollector extends SimpleCollector {

    final GroupSelector valueSelector;
    final Set values = new HashSet<>();

    private ValuesCollector(GroupSelector valueSelector) {
      this.valueSelector = valueSelector;
    }

    @Override
    public void collect(int doc) throws IOException {
      if (valueSelector.advanceTo(doc) == GroupSelector.State.ACCEPT) {
        R value = valueSelector.currentValue();
        if (values.contains(value) == false)
          values.add(valueSelector.copyValue());
      }
      else {
        if (values.contains(null) == false)
          values.add(null);
      }
    }

    @Override
    protected void doSetNextReader(LeafReaderContext context) throws IOException {
      valueSelector.setNextReader(context);
    }

    @Override
    public ScoreMode scoreMode() {
      return ScoreMode.COMPLETE_NO_SCORES;
    }
  }

  private static class DistinctValuesReducer extends GroupReducer> {

    final GroupSelector valueSelector;

    private DistinctValuesReducer(GroupSelector valueSelector) {
      this.valueSelector = valueSelector;
    }

    @Override
    public boolean needsScores() {
      return false;
    }

    @Override
    protected ValuesCollector newCollector() {
      return new ValuesCollector<>(valueSelector);
    }
  }

  /**
   * Returns all unique values for each top N group.
   *
   * @return all unique values for each top N group
   */
  public List> getGroups() {
    List> counts = new ArrayList<>();
    for (SearchGroup group : groups) {
      @SuppressWarnings("unchecked")
      ValuesCollector vc = (ValuesCollector) groupReducer.getCollector(group.groupValue);
      counts.add(new GroupCount<>(group.groupValue, vc.values));
    }
    return counts;
  }

  /**
   * Returned by {@link DistinctValuesCollector#getGroups()},
   * representing the value and set of distinct values for the group.
   */
  public static class GroupCount {

    public final T groupValue;
    public final Set uniqueValues;

    public GroupCount(T groupValue, Set values) {
      this.groupValue = groupValue;
      this.uniqueValues = values;
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy