Java Code Examples for org.apache.lucene.search.SortField#getComparator()

The following examples show how to use org.apache.lucene.search.SortField#getComparator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollapsingQParserPlugin.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes"})
public SortFieldsCompare(SortField[] sorts, int initNumGroups) {
  this.sorts = sorts;
  numClauses = sorts.length;
  fieldComparators = new FieldComparator[numClauses];
  leafFieldComparators = new LeafFieldComparator[numClauses];
  reverseMul = new int[numClauses];
  for (int clause = 0; clause < numClauses; clause++) {
    SortField sf = sorts[clause];
    // we only need one slot for every comparator
    fieldComparators[clause] = sf.getComparator(1, clause);
    reverseMul[clause] = sf.getReverse() ? -1 : 1;
  }
  groupHeadValues = new Object[initNumGroups][];
  nullGroupValues = new Object[numClauses];
}
 
Example 2
Source File: ShardFieldSortedHitQueue.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
Comparator<ShardDoc> comparatorFieldComparator(SortField sortField) {
  final FieldComparator fieldComparator;
  try {
    fieldComparator = sortField.getComparator(0, 0);
  } catch (IOException e) {
    throw new RuntimeException("Unable to get FieldComparator for sortField " + sortField);
  }

  return new ShardComparator(sortField) {
    // Since the PriorityQueue keeps the biggest elements by default,
    // we need to reverse the field compare ordering so that the
    // smallest elements are kept instead of the largest... hence
    // the negative sign.
    @Override
    @SuppressWarnings("unchecked")
    public int compare(final ShardDoc o1, final ShardDoc o2) {
      // noinspection unchecked
      return -fieldComparator.compareValues(sortVal(o1), sortVal(o2));
    }
  };
}
 
Example 3
Source File: BlockGroupingCollector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Create the single pass collector.
 *
 *  @param groupSort The {@link Sort} used to sort the
 *    groups.  The top sorted document within each group
 *    according to groupSort, determines how that group
 *    sorts against other groups.  This must be non-null,
 *    ie, if you want to groupSort by relevance use
 *    Sort.RELEVANCE.
 *  @param topNGroups How many top groups to keep.
 *  @param needsScores true if the collected documents
 *    require scores, either because relevance is included
 *    in the withinGroupSort or because you plan to pass true
 *    for either getSscores or getMaxScores to {@link
 *    #getTopGroups}
 *  @param lastDocPerGroup a {@link Weight} that marks the
 *    last document in each group.
 */
public BlockGroupingCollector(Sort groupSort, int topNGroups, boolean needsScores, Weight lastDocPerGroup) {

  if (topNGroups < 1) {
    throw new IllegalArgumentException("topNGroups must be >= 1 (got " + topNGroups + ")");
  }

  groupQueue = new GroupQueue(topNGroups);
  pendingSubDocs = new int[10];
  if (needsScores) {
    pendingSubScores = new float[10];
  }

  this.needsScores = needsScores;
  this.lastDocPerGroup = lastDocPerGroup;

  this.groupSort = groupSort;
  
  this.topNGroups = topNGroups;

  final SortField[] sortFields = groupSort.getSort();
  comparators = new FieldComparator<?>[sortFields.length];
  leafComparators = new LeafFieldComparator[sortFields.length];
  compIDXEnd = comparators.length - 1;
  reversed = new int[sortFields.length];
  for (int i = 0; i < sortFields.length; i++) {
    final SortField sortField = sortFields[i];
    comparators[i] = sortField.getComparator(topNGroups, i);
    reversed[i] = sortField.getReverse() ? -1 : 1;
  }
}
 
Example 4
Source File: FirstPassGroupingCollector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Create the first pass collector.
 *
 * @param groupSelector a GroupSelector used to defined groups
 * @param groupSort The {@link Sort} used to sort the
 *    groups.  The top sorted document within each group
 *    according to groupSort, determines how that group
 *    sorts against other groups.  This must be non-null,
 *    ie, if you want to groupSort by relevance use
 *    Sort.RELEVANCE.
 * @param topNGroups How many top groups to keep.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public FirstPassGroupingCollector(GroupSelector<T> groupSelector, Sort groupSort, int topNGroups) {
  this.groupSelector = groupSelector;
  if (topNGroups < 1) {
    throw new IllegalArgumentException("topNGroups must be >= 1 (got " + topNGroups + ")");
  }

  // TODO: allow null groupSort to mean "by relevance",
  // and specialize it?

  this.topNGroups = topNGroups;
  this.needsScores = groupSort.needsScores();
  final SortField[] sortFields = groupSort.getSort();
  comparators = new FieldComparator[sortFields.length];
  leafComparators = new LeafFieldComparator[sortFields.length];
  compIDXEnd = comparators.length - 1;
  reversed = new int[sortFields.length];
  for (int i = 0; i < sortFields.length; i++) {
    final SortField sortField = sortFields[i];

    // use topNGroups + 1 so we have a spare slot to use for comparing (tracked by this.spareSlot):
    comparators[i] = sortField.getComparator(topNGroups + 1, i);
    reversed[i] = sortField.getReverse() ? -1 : 1;
  }

  spareSlot = topNGroups;
  groupMap = new HashMap<>(topNGroups);
}
 
Example 5
Source File: SearchGroup.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
public GroupComparator(Sort groupSort) {
  final SortField[] sortFields = groupSort.getSort();
  comparators = new FieldComparator[sortFields.length];
  reversed = new int[sortFields.length];
  for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
    final SortField sortField = sortFields[compIDX];
    comparators[compIDX] = sortField.getComparator(1, compIDX);
    reversed[compIDX] = sortField.getReverse() ? -1 : 1;
  }
}
 
Example 6
Source File: ShardFieldSortedHitQueue.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
Comparator<ShardDoc> comparatorFieldComparator(SortField sortField) {
  @SuppressWarnings({"rawtypes"})
  final FieldComparator fieldComparator = sortField.getComparator(0, 0);
  return new ShardComparator(sortField) {
    // Since the PriorityQueue keeps the biggest elements by default,
    // we need to reverse the field compare ordering so that the
    // smallest elements are kept instead of the largest... hence
    // the negative sign.
    @Override
    @SuppressWarnings({"unchecked"})
    public int compare(final ShardDoc o1, final ShardDoc o2) {
      return -fieldComparator.compareValues(sortVal(o1), sortVal(o2));
    }
  };
}