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);
}
};
}
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];
}
};
}
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();
}
};
}
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);
}
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));
}
}
}
Aggregations