Search in sources :

Example 1 with SortedBinaryDocValues

use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.

the class BinaryValuesSource method getLeafCollector.

@Override
LeafBucketCollector getLeafCollector(LeafReaderContext context, LeafBucketCollector next) throws IOException {
    final SortedBinaryDocValues dvs = docValuesFunc.apply(context);
    return new LeafBucketCollector() {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (dvs.advanceExact(doc)) {
                int num = dvs.docValueCount();
                for (int i = 0; i < num; i++) {
                    currentValue = dvs.nextValue();
                    next.collect(doc, bucket);
                }
            } else if (missingBucket) {
                currentValue = null;
                next.collect(doc, bucket);
            }
        }
    };
}
Also used : LeafBucketCollector(org.opensearch.search.aggregations.LeafBucketCollector) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 2 with SortedBinaryDocValues

use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.

the class AbstractBinaryDVLeafFieldData method getBytesValues.

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

        int count;

        final BytesStreamInput in = new BytesStreamInput();

        final BytesRef scratch = new BytesRef();

        @Override
        public boolean advanceExact(int doc) throws IOException {
            if (values.advanceExact(doc)) {
                final BytesRef bytes = values.binaryValue();
                assert bytes.length > 0;
                in.reset(bytes.bytes, bytes.offset, bytes.length);
                count = in.readVInt();
                scratch.bytes = bytes.bytes;
                return true;
            } else {
                return false;
            }
        }

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

        @Override
        public BytesRef nextValue() throws IOException {
            scratch.length = in.readVInt();
            scratch.offset = in.getPosition();
            in.setPosition(scratch.offset + scratch.length);
            return scratch;
        }
    };
}
Also used : BytesStreamInput(org.opensearch.common.io.stream.BytesStreamInput) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 3 with SortedBinaryDocValues

use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.

the class ScriptSortBuilder method fieldComparatorSource.

private IndexFieldData.XFieldComparatorSource fieldComparatorSource(QueryShardContext context) throws IOException {
    MultiValueMode valueMode = null;
    if (sortMode != null) {
        valueMode = MultiValueMode.fromString(sortMode.toString());
    }
    if (valueMode == null) {
        valueMode = order == SortOrder.DESC ? MultiValueMode.MAX : MultiValueMode.MIN;
    }
    final Nested nested;
    if (nestedSort != null) {
        // new nested sorts takes priority
        validateMaxChildrenExistOnlyInTopLevelNestedSort(context, nestedSort);
        nested = resolveNested(context, nestedSort);
    } else {
        nested = resolveNested(context, nestedPath, nestedFilter);
    }
    switch(type) {
        case STRING:
            final StringSortScript.Factory factory = context.compile(script, StringSortScript.CONTEXT);
            final StringSortScript.LeafFactory searchScript = factory.newFactory(script.getParams(), context.lookup());
            return new BytesRefFieldComparatorSource(null, null, valueMode, nested) {

                StringSortScript leafScript;

                @Override
                protected SortedBinaryDocValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.newInstance(context);
                    final BinaryDocValues values = new AbstractBinaryDocValues() {

                        final BytesRefBuilder spare = new BytesRefBuilder();

                        @Override
                        public boolean advanceExact(int doc) throws IOException {
                            leafScript.setDocument(doc);
                            return true;
                        }

                        @Override
                        public BytesRef binaryValue() {
                            spare.copyChars(leafScript.execute());
                            return spare.get();
                        }
                    };
                    return FieldData.singleton(values);
                }

                @Override
                protected void setScorer(Scorable scorer) {
                    leafScript.setScorer(scorer);
                }

                @Override
                public BucketedSort newBucketedSort(BigArrays bigArrays, SortOrder sortOrder, DocValueFormat format, int bucketSize, BucketedSort.ExtraData extra) {
                    throw new IllegalArgumentException("error building sort for [_script]: " + "script sorting only supported on [numeric] scripts but was [" + type + "]");
                }
            };
        case NUMBER:
            final NumberSortScript.Factory numberSortFactory = context.compile(script, NumberSortScript.CONTEXT);
            final NumberSortScript.LeafFactory numberSortScript = numberSortFactory.newFactory(script.getParams(), context.lookup());
            return new DoubleValuesComparatorSource(null, Double.MAX_VALUE, valueMode, nested) {

                NumberSortScript leafScript;

                @Override
                protected SortedNumericDoubleValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = numberSortScript.newInstance(context);
                    final NumericDoubleValues values = new NumericDoubleValues() {

                        @Override
                        public boolean advanceExact(int doc) throws IOException {
                            leafScript.setDocument(doc);
                            return true;
                        }

                        @Override
                        public double doubleValue() {
                            return leafScript.execute();
                        }
                    };
                    return FieldData.singleton(values);
                }

                @Override
                protected void setScorer(Scorable scorer) {
                    leafScript.setScorer(scorer);
                }
            };
        default:
            throw new QueryShardException(context, "custom script sort type [" + type + "] not supported");
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DoubleValuesComparatorSource(org.opensearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource) Scorable(org.apache.lucene.search.Scorable) DocValueFormat(org.opensearch.search.DocValueFormat) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BytesRefFieldComparatorSource(org.opensearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource) StringSortScript(org.opensearch.script.StringSortScript) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) AbstractBinaryDocValues(org.opensearch.index.fielddata.AbstractBinaryDocValues) MultiValueMode(org.opensearch.search.MultiValueMode) AbstractBinaryDocValues(org.opensearch.index.fielddata.AbstractBinaryDocValues) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) NumberSortScript(org.opensearch.script.NumberSortScript) BigArrays(org.opensearch.common.util.BigArrays) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 4 with SortedBinaryDocValues

use of org.opensearch.index.fielddata.SortedBinaryDocValues in project OpenSearch by opensearch-project.

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(randomAlphaOfLengthBetween(8, 8));
        }
        Arrays.sort(values);
        array[i] = values;
    }
    final Supplier<SortedBinaryDocValues> multiValues = () -> new SortedBinaryDocValues() {

        int doc;

        int i;

        @Override
        public BytesRef nextValue() {
            return BytesRef.deepCopyOf(array[doc][i++]);
        }

        @Override
        public boolean advanceExact(int doc) {
            this.doc = doc;
            i = 0;
            return array[doc].length > 0;
        }

        @Override
        public int docValueCount() {
            return array[doc].length;
        }
    };
    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) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues)

Example 5 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)

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