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

org.apache.lucene.search.grouping.ValueSourceGroupSelector 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.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.search.Scorable;
import org.apache.lucene.util.mutable.MutableValue;

/**
 * A GroupSelector that groups via a ValueSource
 */
public class ValueSourceGroupSelector extends GroupSelector {

  private final ValueSource valueSource;
  private final Map context;

  private Set secondPassGroups;

  /**
   * Create a new ValueSourceGroupSelector
   * @param valueSource the ValueSource to group by
   * @param context     a context map for the ValueSource
   */
  public ValueSourceGroupSelector(ValueSource valueSource, Map context) {
    this.valueSource = valueSource;
    this.context = context;
  }

  private FunctionValues.ValueFiller filler;

  @Override
  public void setNextReader(LeafReaderContext readerContext) throws IOException {
    FunctionValues values = valueSource.getValues(context, readerContext);
    this.filler = values.getValueFiller();
  }

  @Override
  public void setScorer(Scorable scorer) throws IOException { }

  @Override
  public State advanceTo(int doc) throws IOException {
    this.filler.fillValue(doc);
    if (secondPassGroups != null) {
      if (secondPassGroups.contains(filler.getValue()) == false)
        return State.SKIP;
    }
    return State.ACCEPT;
  }

  @Override
  public MutableValue currentValue() throws IOException {
    return filler.getValue();
  }

  @Override
  public MutableValue copyValue() {
    return filler.getValue().duplicate();
  }

  @Override
  public void setGroups(Collection> searchGroups) {
    secondPassGroups = new HashSet<>();
    for (SearchGroup group : searchGroups) {
      secondPassGroups.add(group.groupValue);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy