Search in sources :

Example 11 with SortedBinaryDocValues

use of org.elasticsearch.index.fielddata.SortedBinaryDocValues 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)

Example 12 with SortedBinaryDocValues

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

the class AbstractAtomicParentChildFieldData method getBytesValues.

@Override
public final SortedBinaryDocValues getBytesValues() {
    return new SortedBinaryDocValues() {

        private final BytesRef[] terms = new BytesRef[2];

        private int count;

        @Override
        public void setDocument(int docId) {
            count = 0;
            for (String type : types()) {
                final SortedDocValues values = getOrdinalsValues(type);
                final int ord = values.getOrd(docId);
                if (ord >= 0) {
                    terms[count++] = values.lookupOrd(ord);
                }
            }
            assert count <= 2 : "A single doc can potentially be both parent and child, so the maximum allowed values is 2";
            if (count > 1) {
                int cmp = terms[0].compareTo(terms[1]);
                if (cmp > 0) {
                    ArrayUtil.swap(terms, 0, 1);
                } else if (cmp == 0) {
                    // If the id is the same between types the only omit one. For example: a doc has parent#1 in _uid field and has grand_parent#1 in _parent field.
                    count = 1;
                }
            }
        }

        @Override
        public int count() {
            return count;
        }

        @Override
        public BytesRef valueAt(int index) {
            return terms[index];
        }
    };
}
Also used : SortedDocValues(org.apache.lucene.index.SortedDocValues) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues)

Example 13 with SortedBinaryDocValues

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

the class BytesBinaryDVAtomicFieldData method getBytesValues.

@Override
public SortedBinaryDocValues getBytesValues() {
    return new SortedBinaryDocValues() {

        int count;

        BytesRefBuilder[] refs = new BytesRefBuilder[0];

        final ByteArrayDataInput in = new ByteArrayDataInput();

        @Override
        public void setDocument(int docId) {
            final BytesRef bytes = values.get(docId);
            in.reset(bytes.bytes, bytes.offset, bytes.length);
            if (bytes.length == 0) {
                count = 0;
            } else {
                count = in.readVInt();
                if (count > refs.length) {
                    final int previousLength = refs.length;
                    refs = Arrays.copyOf(refs, ArrayUtil.oversize(count, RamUsageEstimator.NUM_BYTES_OBJECT_REF));
                    for (int i = previousLength; i < refs.length; ++i) {
                        refs[i] = new BytesRefBuilder();
                    }
                }
                for (int i = 0; i < count; ++i) {
                    final int length = in.readVInt();
                    final BytesRefBuilder scratch = refs[i];
                    scratch.grow(length);
                    in.readBytes(scratch.bytes(), 0, length);
                    scratch.setLength(length);
                }
            }
        }

        @Override
        public int count() {
            return count;
        }

        @Override
        public BytesRef valueAt(int index) {
            return refs[index].get();
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) ByteArrayDataInput(org.apache.lucene.store.ByteArrayDataInput) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues)

Example 14 with SortedBinaryDocValues

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

the class MultiValueModeTests method testMultiValuedStrings.

public void testMultiValuedStrings() throws Exception {
    final int numDocs = scaledRandomIntBetween(1, 100);
    final BytesRef[][] array = new BytesRef[numDocs][];
    for (int i = 0; i < numDocs; ++i) {
        final BytesRef[] values = new BytesRef[randomInt(4)];
        for (int j = 0; j < values.length; ++j) {
            values[j] = new BytesRef(RandomStrings.randomAsciiOfLength(random(), 8));
        }
        Arrays.sort(values);
        array[i] = values;
    }
    final SortedBinaryDocValues multiValues = new SortedBinaryDocValues() {

        int doc;

        @Override
        public BytesRef valueAt(int index) {
            return BytesRef.deepCopyOf(array[doc][index]);
        }

        @Override
        public void setDocument(int doc) {
            this.doc = doc;
        }

        @Override
        public int count() {
            return array[doc].length;
        }
    };
    verify(multiValues, numDocs);
    final FixedBitSet rootDocs = randomRootDocs(numDocs);
    final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
    verify(multiValues, numDocs, rootDocs, innerDocs);
}
Also used : FixedBitSet(org.apache.lucene.util.FixedBitSet) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues)

Example 15 with SortedBinaryDocValues

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

the class MissingValuesTests method testMissingBytes.

public void testMissingBytes() {
    final int numDocs = TestUtil.nextInt(random(), 1, 100);
    final BytesRef[][] values = new BytesRef[numDocs][];
    for (int i = 0; i < numDocs; ++i) {
        values[i] = new BytesRef[random().nextInt(4)];
        for (int j = 0; j < values[i].length; ++j) {
            values[i][j] = new BytesRef(RandomStrings.randomAsciiOfLength(random(), 2));
        }
        Arrays.sort(values[i]);
    }
    SortedBinaryDocValues asBinaryValues = new SortedBinaryDocValues() {

        int i = -1;

        @Override
        public BytesRef valueAt(int index) {
            return values[i][index];
        }

        @Override
        public void setDocument(int docId) {
            i = docId;
        }

        @Override
        public int count() {
            return values[i].length;
        }
    };
    final BytesRef missing = new BytesRef(RandomStrings.randomAsciiOfLength(random(), 2));
    SortedBinaryDocValues withMissingReplaced = MissingValues.replaceMissing(asBinaryValues, missing);
    for (int i = 0; i < numDocs; ++i) {
        withMissingReplaced.setDocument(i);
        if (values[i].length > 0) {
            assertEquals(values[i].length, withMissingReplaced.count());
            for (int j = 0; j < values[i].length; ++j) {
                assertEquals(values[i][j], withMissingReplaced.valueAt(j));
            }
        } else {
            assertEquals(1, withMissingReplaced.count());
            assertEquals(missing, withMissingReplaced.valueAt(0));
        }
    }
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues)

Aggregations

SortedBinaryDocValues (org.elasticsearch.index.fielddata.SortedBinaryDocValues)17 BytesRef (org.apache.lucene.util.BytesRef)13 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)6 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)6 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)3 IndexService (org.elasticsearch.index.IndexService)3 Searcher (org.elasticsearch.index.engine.Engine.Searcher)3 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)3 IOException (java.io.IOException)2 SortedDocValues (org.apache.lucene.index.SortedDocValues)2 Scorer (org.apache.lucene.search.Scorer)2 FixedBitSet (org.apache.lucene.util.FixedBitSet)2 LeafBucketCollectorBase (org.elasticsearch.search.aggregations.LeafBucketCollectorBase)2 RandomAccessOrds (org.apache.lucene.index.RandomAccessOrds)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 Explanation (org.apache.lucene.search.Explanation)1 SortField (org.apache.lucene.search.SortField)1 ByteArrayDataInput (org.apache.lucene.store.ByteArrayDataInput)1 BitSet (org.apache.lucene.util.BitSet)1 BitSetIterator (org.apache.lucene.util.BitSetIterator)1