use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.
the class MultiValueModeTests method testSingleValuedStrings.
public void testSingleValuedStrings() throws Exception {
final int numDocs = scaledRandomIntBetween(1, 100);
final BytesRef[] array = new BytesRef[numDocs];
final FixedBitSet docsWithValue = randomBoolean() ? null : new FixedBitSet(numDocs);
for (int i = 0; i < array.length; ++i) {
if (randomBoolean()) {
array[i] = new BytesRef(randomAlphaOfLengthBetween(8, 8));
if (docsWithValue != null) {
docsWithValue.set(i);
}
} else {
array[i] = new BytesRef();
if (docsWithValue != null && randomBoolean()) {
docsWithValue.set(i);
}
}
}
final Supplier<SortedBinaryDocValues> multiValues = () -> FieldData.singleton(new AbstractBinaryDocValues() {
int docID;
@Override
public boolean advanceExact(int target) throws IOException {
docID = target;
return docsWithValue == null || docsWithValue.get(docID);
}
@Override
public BytesRef binaryValue() {
return BytesRef.deepCopyOf(array[docID]);
}
});
verifySortedBinary(multiValues, numDocs);
final FixedBitSet rootDocs = randomRootDocs(numDocs);
final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
verifySortedBinary(multiValues, numDocs, rootDocs, innerDocs, Integer.MAX_VALUE);
verifySortedBinary(multiValues, numDocs, rootDocs, innerDocs, randomIntBetween(1, numDocs));
}
use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.
the class ValuesSourceConfigTests method testEmptyKeyword.
public void testEmptyKeyword() throws Exception {
IndexService indexService = createIndex("index", Settings.EMPTY, "type", "bytes", "type=keyword");
client().prepareIndex("index").setId("1").setSource().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve(context, null, "bytes", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
assertFalse(values.advanceExact(0));
config = ValuesSourceConfig.resolve(context, null, "bytes", null, "abc", null, null, CoreValuesSourceType.BYTES);
valuesSource = (ValuesSource.Bytes) config.getValuesSource();
values = valuesSource.bytesValues(ctx);
assertTrue(values.advanceExact(0));
assertEquals(1, values.docValueCount());
assertEquals(new BytesRef("abc"), values.nextValue());
}
}
use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.
the class MissingValuesTests method testMissingBytes.
public void testMissingBytes() throws IOException {
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 doc = -1;
int i;
@Override
public BytesRef nextValue() {
return values[doc][i++];
}
@Override
public boolean advanceExact(int docId) {
doc = docId;
i = 0;
return values[doc].length > 0;
}
@Override
public int docValueCount() {
return values[doc].length;
}
};
final BytesRef missing = new BytesRef(RandomStrings.randomAsciiOfLength(random(), 2));
SortedBinaryDocValues withMissingReplaced = MissingValues.replaceMissing(asBinaryValues, missing);
for (int i = 0; i < numDocs; ++i) {
assertTrue(withMissingReplaced.advanceExact(i));
if (values[i].length > 0) {
assertEquals(values[i].length, withMissingReplaced.docValueCount());
for (int j = 0; j < values[i].length; ++j) {
assertEquals(values[i][j], withMissingReplaced.nextValue());
}
} else {
assertEquals(1, withMissingReplaced.docValueCount());
assertEquals(missing, withMissingReplaced.nextValue());
}
}
}
use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.
the class ValueCountAggregator method getLeafCollector.
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
if (valuesSource == null) {
return LeafBucketCollector.NO_OP_COLLECTOR;
}
final BigArrays bigArrays = context.bigArrays();
if (valuesSource instanceof ValuesSource.Numeric) {
final SortedNumericDocValues values = ((ValuesSource.Numeric) valuesSource).longValues(ctx);
return new LeafBucketCollectorBase(sub, values) {
@Override
public void collect(int doc, long bucket) throws IOException {
counts = bigArrays.grow(counts, bucket + 1);
if (values.advanceExact(doc)) {
counts.increment(bucket, values.docValueCount());
}
}
};
}
if (valuesSource instanceof ValuesSource.Bytes.GeoPoint) {
MultiGeoPointValues values = ((ValuesSource.GeoPoint) valuesSource).geoPointValues(ctx);
return new LeafBucketCollectorBase(sub, null) {
@Override
public void collect(int doc, long bucket) throws IOException {
counts = bigArrays.grow(counts, bucket + 1);
if (values.advanceExact(doc)) {
counts.increment(bucket, values.docValueCount());
}
}
};
}
// The following is default collector. Including the keyword FieldType
final SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
return new LeafBucketCollectorBase(sub, values) {
@Override
public void collect(int doc, long bucket) throws IOException {
counts = bigArrays.grow(counts, bucket + 1);
if (values.advanceExact(doc)) {
counts.increment(bucket, values.docValueCount());
}
}
};
}
use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.
the class MultiValueModeTests method verifySortedBinary.
private void verifySortedBinary(Supplier<SortedBinaryDocValues> supplier, int maxDoc) throws IOException {
for (BytesRef missingValue : new BytesRef[] { new BytesRef(), new BytesRef(randomAlphaOfLengthBetween(8, 8)) }) {
for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX }) {
SortedBinaryDocValues values = supplier.get();
final BinaryDocValues selected = mode.select(values, missingValue);
for (int i = 0; i < maxDoc; ++i) {
assertTrue(selected.advanceExact(i));
final BytesRef actual = selected.binaryValue();
verifyBinaryValueCanCalledMoreThanOnce(selected, actual);
BytesRef expected = null;
if (values.advanceExact(i) == false) {
expected = missingValue;
} else {
int numValues = values.docValueCount();
for (int j = 0; j < numValues; ++j) {
if (expected == null) {
expected = BytesRef.deepCopyOf(values.nextValue());
} else {
BytesRef value = values.nextValue();
if (mode == MultiValueMode.MIN) {
expected = expected.compareTo(value) <= 0 ? expected : BytesRef.deepCopyOf(value);
} else if (mode == MultiValueMode.MAX) {
expected = expected.compareTo(value) > 0 ? expected : BytesRef.deepCopyOf(value);
}
}
}
if (expected == null) {
expected = missingValue;
}
}
assertEquals(mode.toString() + " docId=" + i, expected, actual);
}
}
}
}
Aggregations