Search in sources :

Example 6 with SortedBinaryDocValues

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));
}
Also used : FixedBitSet(org.apache.lucene.util.FixedBitSet) IOException(java.io.IOException) AbstractBinaryDocValues(org.opensearch.index.fielddata.AbstractBinaryDocValues) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 7 with SortedBinaryDocValues

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());
    }
}
Also used : IndexService(org.opensearch.index.IndexService) QueryShardContext(org.opensearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Engine(org.opensearch.index.engine.Engine) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 8 with SortedBinaryDocValues

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());
        }
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 9 with SortedBinaryDocValues

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());
            }
        }
    };
}
Also used : BigArrays(org.opensearch.common.util.BigArrays) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) ValuesSource(org.opensearch.search.aggregations.support.ValuesSource) MultiGeoPointValues(org.opensearch.index.fielddata.MultiGeoPointValues) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 10 with SortedBinaryDocValues

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);
            }
        }
    }
}
Also used : BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues) AbstractBinaryDocValues(org.opensearch.index.fielddata.AbstractBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Aggregations

SortedBinaryDocValues (org.opensearch.index.fielddata.SortedBinaryDocValues)18 BytesRef (org.apache.lucene.util.BytesRef)14 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)6 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)4 IndexService (org.opensearch.index.IndexService)4 Engine (org.opensearch.index.engine.Engine)4 AbstractBinaryDocValues (org.opensearch.index.fielddata.AbstractBinaryDocValues)4 QueryShardContext (org.opensearch.index.query.QueryShardContext)4 LeafBucketCollectorBase (org.opensearch.search.aggregations.LeafBucketCollectorBase)4 Scorable (org.apache.lucene.search.Scorable)2 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)2 FixedBitSet (org.apache.lucene.util.FixedBitSet)2 BigArrays (org.opensearch.common.util.BigArrays)2 RangeFieldMapper (org.opensearch.index.mapper.RangeFieldMapper)2 RangeType (org.opensearch.index.mapper.RangeType)2 IOException (java.io.IOException)1 SortedDocValues (org.apache.lucene.index.SortedDocValues)1 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)1 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1