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

The following examples show how to use org.apache.lucene.search.SortField#getReverse() . 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: SortParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token = parser.currentToken();
    List<SortField> sortFields = new ArrayList<>(2);
    if (token == XContentParser.Token.START_ARRAY) {
        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.START_OBJECT) {
                addCompoundSortField(parser, context, sortFields);
            } else if (token == XContentParser.Token.VALUE_STRING) {
                addSortField(context, sortFields, parser.text(), false, null, null, null, null);
            } else {
                throw new IllegalArgumentException("malformed sort format, within the sort array, an object, or an actual string are allowed");
            }
        }
    } else if (token == XContentParser.Token.VALUE_STRING) {
        addSortField(context, sortFields, parser.text(), false, null, null, null, null);
    } else if (token == XContentParser.Token.START_OBJECT) {
        addCompoundSortField(parser, context, sortFields);
    } else {
        throw new IllegalArgumentException("malformed sort format, either start with array, object, or an actual string");
    }
    if (!sortFields.isEmpty()) {
        // optimize if we just sort on score non reversed, we don't really need sorting
        boolean sort;
        if (sortFields.size() > 1) {
            sort = true;
        } else {
            SortField sortField = sortFields.get(0);
            if (sortField.getType() == SortField.Type.SCORE && !sortField.getReverse()) {
                sort = false;
            } else {
                sort = true;
            }
        }
        if (sort) {
            context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
        }
    }
}
 
Example 3
Source File: TestExpressionSorts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void assertQuery(Query query, Sort sort) throws Exception {
  int size = TestUtil.nextInt(random(), 1, searcher.getIndexReader().maxDoc() / 5);
  TopDocs expected = searcher.search(query, size, sort, random().nextBoolean());
  
  // make our actual sort, mutating original by replacing some of the 
  // sortfields with equivalent expressions
  
  SortField original[] = sort.getSort();
  SortField mutated[] = new SortField[original.length];
  for (int i = 0; i < mutated.length; i++) {
    if (random().nextInt(3) > 0) {
      SortField s = original[i];
      Expression expr = JavascriptCompiler.compile(s.getField());
      SimpleBindings simpleBindings = new SimpleBindings();
      simpleBindings.add(s.getField(), fromSortField(s));
      boolean reverse = s.getType() == SortField.Type.SCORE || s.getReverse();
      mutated[i] = expr.getSortField(simpleBindings, reverse);
    } else {
      mutated[i] = original[i];
    }
  }
  
  Sort mutatedSort = new Sort(mutated);
  TopDocs actual = searcher.search(query, size, mutatedSort, random().nextBoolean());
  CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs);
  
  if (size < actual.totalHits.value) {
    expected = searcher.searchAfter(expected.scoreDocs[size-1], query, size, sort);
    actual = searcher.searchAfter(actual.scoreDocs[size-1], query, size, mutatedSort);
    CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs);
  }
}
 
Example 4
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 5
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 6
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 7
Source File: TestGrouping.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Comparator<GroupDoc> getComparator(Sort sort) {
  final SortField[] sortFields = sort.getSort();
  return new Comparator<GroupDoc>() {
    @Override
    public int compare(GroupDoc d1, GroupDoc d2) {
      for(SortField sf : sortFields) {
        final int cmp;
        if (sf.getType() == SortField.Type.SCORE) {
          if (d1.score > d2.score) {
            cmp = -1;
          } else if (d1.score < d2.score) {
            cmp = 1;
          } else {
            cmp = 0;
          }
        } else if (sf.getField().equals("sort1")) {
          cmp = d1.sort1.compareTo(d2.sort1);
        } else if (sf.getField().equals("sort2")) {
          cmp = d1.sort2.compareTo(d2.sort2);
        } else {
          assertEquals(sf.getField(), "id");
          cmp = d1.id - d2.id;
        }
        if (cmp != 0) {
          return sf.getReverse() ? -cmp : cmp;
        }
      }
      // Our sort always fully tie breaks:
      fail();
      return 0;
    }
  };
}
 
Example 8
Source File: AllGroupHeadsCollectorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Comparator<GroupDoc> getComparator(Sort sort, final boolean sortByScoreOnly, final int[] fieldIdToDocID) {
  final SortField[] sortFields = sort.getSort();
  return new Comparator<GroupDoc>() {
    @Override
    public int compare(GroupDoc d1, GroupDoc d2) {
      for (SortField sf : sortFields) {
        final int cmp;
        if (sf.getType() == SortField.Type.SCORE) {
          if (d1.score > d2.score) {
            cmp = -1;
          } else if (d1.score < d2.score) {
            cmp = 1;
          } else {
            cmp = sortByScoreOnly ? fieldIdToDocID[d1.id] - fieldIdToDocID[d2.id] : 0;
          }
        } else if (sf.getField().equals("sort1")) {
          cmp = d1.sort1.compareTo(d2.sort1);
        } else if (sf.getField().equals("sort2")) {
          cmp = d1.sort2.compareTo(d2.sort2);
        } else if (sf.getField().equals("sort3")) {
          cmp = d1.sort3.compareTo(d2.sort3);
        } else {
          assertEquals(sf.getField(), "id");
          cmp = d1.id - d2.id;
        }
        if (cmp != 0) {
          return sf.getReverse() ? -cmp : cmp;
        }
      }
      // Our sort always fully tie breaks:
      fail();
      return 0;
    }
  };
}
 
Example 9
Source File: FieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** 
 * @see #getSortField 
 * @see #getSortedSetSortField 
 */
private static void applySetMissingValue(SchemaField field, SortField sortField, 
                                         Object missingLow, Object missingHigh) {
  final boolean reverse = sortField.getReverse();
  
  if (field.sortMissingLast()) {
    sortField.setMissingValue(reverse ? missingLow : missingHigh);
  } else if (field.sortMissingFirst()) {
    sortField.setMissingValue(reverse ? missingHigh : missingLow);
  }
}