@Override public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) { assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldName()); final Long dMissingValue = (Long) missingObject(missingValue, reversed); // NOTE: it's important to pass null as a missing value in the constructor so that // the comparator doesn't check docsWithField since we replace missing values in select() return new FieldComparator.LongComparator(numHits, null, null) { @Override protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException { final SortedNumericDocValues values = indexFieldData.load(context).getLongValues(); final NumericDocValues selectedValues; if (nested == null) { selectedValues = sortMode.select(values, dMissingValue); } else { final BitSet rootDocs = nested.rootDocs(context); final DocIdSetIterator innerDocs = nested.innerDocs(context); selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc()); } return selectedValues; } }; }
@Override public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) { assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldName()); final float dMissingValue = (Float) missingObject(missingValue, reversed); // NOTE: it's important to pass null as a missing value in the constructor so that // the comparator doesn't check docsWithField since we replace missing values in select() return new FieldComparator.FloatComparator(numHits, null, null) { @Override protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException { final SortedNumericDoubleValues values = indexFieldData.load(context).getDoubleValues(); final NumericDoubleValues selectedValues; if (nested == null) { selectedValues = sortMode.select(values, dMissingValue); } else { final BitSet rootDocs = nested.rootDocs(context); final DocIdSetIterator innerDocs = nested.innerDocs(context); selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc()); } return selectedValues.getRawFloatValues(); } }; }
@Override public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { return new FieldComparator<Object>() { @Override public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException { return LEAF_FIELD_COMPARATOR; } @Override public int compare(int slot1, int slot2) { return 0; } @Override public void setTopValue(Object value) { } @Override public Object value(int slot) { return missingValue; } }; }
@Override public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldNames().indexName()); final Long dMissingValue = (Long) missingObject(missingValue, reversed); // NOTE: it's important to pass null as a missing value in the constructor so that // the comparator doesn't check docsWithField since we replace missing values in select() return new FieldComparator.LongComparator(numHits, null, null) { @Override protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException { final SortedNumericDocValues values = indexFieldData.load(context).getLongValues(); final NumericDocValues selectedValues; if (nested == null) { selectedValues = sortMode.select(values, dMissingValue); } else { final BitSet rootDocs = nested.rootDocs(context); final DocIdSetIterator innerDocs = nested.innerDocs(context); selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc()); } return selectedValues; } }; }
@Override public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldNames().indexName()); final float dMissingValue = (Float) missingObject(missingValue, reversed); // NOTE: it's important to pass null as a missing value in the constructor so that // the comparator doesn't check docsWithField since we replace missing values in select() return new FieldComparator.FloatComparator(numHits, null, null) { @Override protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException { final SortedNumericDoubleValues values = indexFieldData.load(context).getDoubleValues(); final NumericDoubleValues selectedValues; if (nested == null) { selectedValues = sortMode.select(values, dMissingValue); } else { final BitSet rootDocs = nested.rootDocs(context); final DocIdSetIterator innerDocs = nested.innerDocs(context); selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc()); } return selectedValues.getRawFloatValues(); } }; }
/** * Returns a Lucene {@link SortField} for sorting documents/rows according * to the partition key. * * @return a sort field for sorting by partition key */ public SortField sortField() { return new SortField(FIELD_NAME, new FieldComparatorSource() { @Override public FieldComparator<?> newComparator(String field, int hits, int sort, boolean reversed) throws IOException { return new FieldComparator.TermValComparator(hits, field, false) { @Override public int compareValues(BytesRef val1, BytesRef val2) { ByteBuffer bb1 = ByteBufferUtils.byteBuffer(val1); ByteBuffer bb2 = ByteBufferUtils.byteBuffer(val2); return ByteBufferUtil.compareUnsigned(bb1, bb2); } }; } }); }
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 public int compare(final ShardDoc o1, final ShardDoc o2) { //noinspection unchecked return -fieldComparator.compareValues(sortVal(o1), sortVal(o2)); } }; }
@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)); } }; }
@Override public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) { assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldName()); final double dMissingValue = (Double) missingObject(missingValue, reversed); // NOTE: it's important to pass null as a missing value in the constructor so that // the comparator doesn't check docsWithField since we replace missing values in select() return new FieldComparator.DoubleComparator(numHits, null, null) { @Override protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException { final SortedNumericDoubleValues values = getValues(context); final NumericDoubleValues selectedValues; if (nested == null) { selectedValues = sortMode.select(values, dMissingValue); } else { final BitSet rootDocs = nested.rootDocs(context); final DocIdSetIterator innerDocs = nested.innerDocs(context); selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc()); } return selectedValues.getRawDoubleValues(); } @Override public void setScorer(Scorer scorer) { DoubleValuesComparatorSource.this.setScorer(scorer); } }; }
private static FieldComparatorSource createCurrentUserFirstComparator() { return new FieldComparatorSource() { @Override public FieldComparator<Integer> newComparator(final String fieldName, final int numHits, final int sortPos, boolean reversed) throws IOException { return new CustomLuceneSortComparator(numHits, fieldName, CurrentUser.getUserID()); } }; }
@Override public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldNames().indexName()); final double dMissingValue = (Double) missingObject(missingValue, reversed); // NOTE: it's important to pass null as a missing value in the constructor so that // the comparator doesn't check docsWithField since we replace missing values in select() return new FieldComparator.DoubleComparator(numHits, null, null) { @Override protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException { final SortedNumericDoubleValues values = getValues(context); final NumericDoubleValues selectedValues; if (nested == null) { selectedValues = sortMode.select(values, dMissingValue); } else { final BitSet rootDocs = nested.rootDocs(context); final DocIdSetIterator innerDocs = nested.innerDocs(context); selectedValues = sortMode.select(values, dMissingValue, rootDocs, innerDocs, context.reader().maxDoc()); } return selectedValues.getRawDoubleValues(); } @Override public void setScorer(Scorer scorer) { DoubleValuesComparatorSource.this.setScorer(scorer); } }; }
/** * Returns a Lucene {@link SortField} to sort documents by primary key * according to Cassandra's natural order. * * @return the sort field */ public SortField sortField() { return new SortField(FIELD_NAME, new FieldComparatorSource() { @Override public FieldComparator<?> newComparator(String field, int hits, int sort, boolean reversed) throws IOException { return new FieldComparator.TermValComparator(hits, field, false) { @Override public int compareValues(BytesRef val1, BytesRef val2) { return entry(val1).compareTo(entry(val2)); } }; } }); }
private void setSortFieldRandom() { SORT.setSort(new SortField( "", new FieldComparatorSource() { @Override public FieldComparator<Integer> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { return new RandomOrderFieldComparator(); } } )); }
@Override public void setScorer(Scorer scorer) throws IOException { this.scorer = scorer; for (GroupHead groupHead : groups.values()) { for (FieldComparator<?> comparator : groupHead.comparators) { comparator.setScorer(scorer); } } }
@SuppressWarnings({"unchecked","rawtypes"}) private GroupHead(MutableValue groupValue, Sort sort, int doc) throws IOException { super(groupValue, doc + readerContext.docBase); final SortField[] sortFields = sort.getSort(); comparators = new FieldComparator[sortFields.length]; for (int i = 0; i < sortFields.length; i++) { comparators[i] = sortFields[i].getComparator(1, i).setNextReader(readerContext); comparators[i].setScorer(scorer); comparators[i].copy(0, doc); comparators[i].setBottom(0); } }
@Override public void updateDocHead(int doc) throws IOException { for (FieldComparator<?> comparator : comparators) { comparator.copy(0, doc); comparator.setBottom(0); } this.doc = doc + readerContext.docBase; }
public GroupComparator(Sort groupSort) throws IOException { 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; } }
/** * Returns a mapping from the old document ID to its new location in the * sorted index. Implementations can use the auxiliary * {@link #sort(int, DocComparator)} to compute the old-to-new permutation * given a list of documents and their corresponding values. * <p> * A return value of <tt>null</tt> is allowed and means that * <code>reader</code> is already sorted. * <p> * <b>NOTE:</b> deleted documents are expected to appear in the mapping as * well, they will however be marked as deleted in the sorted view. */ DocMap sort(AtomicReader reader) throws IOException { SortField fields[] = sort.getSort(); final int reverseMul[] = new int[fields.length]; final FieldComparator<?> comparators[] = new FieldComparator[fields.length]; for (int i = 0; i < fields.length; i++) { reverseMul[i] = fields[i].getReverse() ? -1 : 1; comparators[i] = fields[i].getComparator(1, i); comparators[i].setNextReader(reader.getContext()); comparators[i].setScorer(FAKESCORER); } final DocComparator comparator = new DocComparator() { @Override public int compare(int docID1, int docID2) { try { for (int i = 0; i < comparators.length; i++) { // TODO: would be better if copy() didnt cause a term lookup in TermOrdVal & co, // the segments are always the same here... comparators[i].copy(0, docID1); comparators[i].setBottom(0); int comp = reverseMul[i] * comparators[i].compareBottom(docID2); if (comp != 0) { return comp; } } return Integer.compare(docID1, docID2); // docid order tiebreak } catch (IOException e) { throw new RuntimeException(e); } } }; return sort(reader.maxDoc(), comparator); }
@Override @SuppressWarnings("unchecked") public FieldComparator<?> getComparator(int numHits, int sortPos) throws IOException { FieldComparator<Object> wrappedFieldComparator = (FieldComparator) super.getComparator(numHits + 1, sortPos); if (order) { return new ToParentBlockJoinFieldComparator.Highest(wrappedFieldComparator, parentFilter, childFilter, numHits); } else { return new ToParentBlockJoinFieldComparator.Lowest(wrappedFieldComparator, parentFilter, childFilter, numHits); } }
@Override public FieldComparator<?> getComparator(int numHits, int sortPos) throws IOException { return new FieldComparator.TermOrdValComparator(numHits, getField(), missingValue == STRING_LAST) { @Override protected SortedDocValues getSortedDocValues(AtomicReaderContext context, String field) throws IOException { SortedSetDocValues sortedSet = FieldCache.DEFAULT.getDocTermOrds(context.reader(), field); if (sortedSet.getValueCount() >= Integer.MAX_VALUE) { throw new UnsupportedOperationException("fields containing more than " + (Integer.MAX_VALUE-1) + " unique terms are unsupported"); } SortedDocValues singleton = DocValues.unwrapSingleton(sortedSet); if (singleton != null) { // it's actually single-valued in practice, but indexed as multi-valued, // so just sort on the underlying single-valued dv directly. // regardless of selector type, this optimization is safe! return singleton; } else if (selector == Selector.MIN) { return new MinValue(sortedSet); } else { if (sortedSet instanceof RandomAccessOrds == false) { throw new UnsupportedOperationException("codec does not support random access ordinals, cannot use selector: " + selector); } RandomAccessOrds randomOrds = (RandomAccessOrds) sortedSet; switch(selector) { case MAX: return new MaxValue(randomOrds); case MIDDLE_MIN: return new MiddleMinValue(randomOrds); case MIDDLE_MAX: return new MiddleMaxValue(randomOrds); case MIN: default: throw new AssertionError(); } } } }; }
public BinarySortField(final String field, final boolean reverse) { super(field, new FieldComparatorSource() { @Override public FieldComparator.TermOrdValComparator newComparator (final String fieldname, final int numHits, final int sortPos, final boolean reversed) throws IOException { return new FieldComparator.TermOrdValComparator(numHits, fieldname); }}, reverse); }
@Override public FieldComparator<BytesRef> setNextReader(AtomicReaderContext context) throws IOException { docBase = context.docBase; termsIndex = FieldCache.DEFAULT.getTermsIndex(context.reader(), field); currentReaderGen++; if (topValue != null) { // Recompute topOrd/SameReader int ord = termsIndex.lookupTerm(topValue); if (ord >= 0) { topSameReader = true; topOrd = ord; } else { topSameReader = false; topOrd = -ord-2; } } else { topOrd = missingOrd; topSameReader = true; } //System.out.println(" setNextReader topOrd=" + topOrd + " topSameReader=" + topSameReader); if (bottomSlot != -1) { // Recompute bottomOrd/SameReader setBottom(bottomSlot); } return this; }
@Override public FieldComparator<String> setNextReader(AtomicReaderContext context) throws IOException { docTerms = FieldCache.DEFAULT.getTerms(context.reader(), field, true); docsWithField = FieldCache.DEFAULT.getDocsWithField(context.reader(), field); return this; }
/** * Returns a Lucene {@link SortField} array for sorting documents/rows according to the column family name. * * @return A Lucene {@link SortField} array for sorting documents/rows according to the column family name. */ public SortField[] sortFields() { return new SortField[]{new SortField(FIELD_NAME, new FieldComparatorSource() { @Override public FieldComparator<?> newComparator(String field, int hits, int sort, boolean reversed) throws IOException { return new ClusteringKeySorter(ClusteringKeyMapper.this, hits, field); } })}; }
public static FieldComparator createComparator(AtomicReader reader, TermOrdValComparator_SML parent) throws IOException { parent.termsIndex = FieldCache.DEFAULT.getTermsIndex(reader, parent.field); PerSegmentComparator perSegComp = new AnyOrdComparator(parent); if (perSegComp.bottomSlot != -1) { perSegComp.setBottom(perSegComp.bottomSlot); } parent.current = perSegComp; return perSegComp; }