Search in sources :

Example 1 with IndexOrdinalsFieldData

use of org.elasticsearch.index.fielddata.IndexOrdinalsFieldData in project elasticsearch by elastic.

the class BytesRefFieldComparatorSource method newComparator.

@Override
public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
    assert indexFieldData == null || fieldname.equals(indexFieldData.getFieldName());
    final boolean sortMissingLast = sortMissingLast(missingValue) ^ reversed;
    final BytesRef missingBytes = (BytesRef) missingObject(missingValue, reversed);
    if (indexFieldData instanceof IndexOrdinalsFieldData) {
        return new FieldComparator.TermOrdValComparator(numHits, null, sortMissingLast) {

            @Override
            protected SortedDocValues getSortedDocValues(LeafReaderContext context, String field) throws IOException {
                final RandomAccessOrds values = ((IndexOrdinalsFieldData) indexFieldData).load(context).getOrdinalsValues();
                final SortedDocValues selectedValues;
                if (nested == null) {
                    selectedValues = sortMode.select(values);
                } else {
                    final BitSet rootDocs = nested.rootDocs(context);
                    final DocIdSetIterator innerDocs = nested.innerDocs(context);
                    selectedValues = sortMode.select(values, rootDocs, innerDocs);
                }
                if (sortMissingFirst(missingValue) || sortMissingLast(missingValue)) {
                    return selectedValues;
                } else {
                    return new ReplaceMissing(selectedValues, missingBytes);
                }
            }

            @Override
            public void setScorer(Scorer scorer) {
                BytesRefFieldComparatorSource.this.setScorer(scorer);
            }
        };
    }
    final BytesRef nullPlaceHolder = new BytesRef();
    final BytesRef nonNullMissingBytes = missingBytes == null ? nullPlaceHolder : missingBytes;
    return new FieldComparator.TermValComparator(numHits, null, sortMissingLast) {

        @Override
        protected BinaryDocValues getBinaryDocValues(LeafReaderContext context, String field) throws IOException {
            final SortedBinaryDocValues values = getValues(context);
            final BinaryDocValues selectedValues;
            if (nested == null) {
                selectedValues = sortMode.select(values, nonNullMissingBytes);
            } else {
                final BitSet rootDocs = nested.rootDocs(context);
                final DocIdSetIterator innerDocs = nested.innerDocs(context);
                selectedValues = sortMode.select(values, nonNullMissingBytes, rootDocs, innerDocs, context.reader().maxDoc());
            }
            return selectedValues;
        }

        @Override
        protected Bits getDocsWithField(LeafReaderContext context, String field) throws IOException {
            return new Bits.MatchAllBits(context.reader().maxDoc());
        }

        @Override
        protected boolean isNull(int doc, BytesRef term) {
            return term == nullPlaceHolder;
        }

        @Override
        public void setScorer(Scorer scorer) {
            BytesRefFieldComparatorSource.this.setScorer(scorer);
        }
    };
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) BitSet(org.apache.lucene.util.BitSet) Scorer(org.apache.lucene.search.Scorer) SortedDocValues(org.apache.lucene.index.SortedDocValues) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) IndexOrdinalsFieldData(org.elasticsearch.index.fielddata.IndexOrdinalsFieldData) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 RandomAccessOrds (org.apache.lucene.index.RandomAccessOrds)1 SortedDocValues (org.apache.lucene.index.SortedDocValues)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 Scorer (org.apache.lucene.search.Scorer)1 BitSet (org.apache.lucene.util.BitSet)1 BytesRef (org.apache.lucene.util.BytesRef)1 IndexOrdinalsFieldData (org.elasticsearch.index.fielddata.IndexOrdinalsFieldData)1 SortedBinaryDocValues (org.elasticsearch.index.fielddata.SortedBinaryDocValues)1