Search in sources :

Example 1 with AbstractBinaryDocValues

use of org.opensearch.index.fielddata.AbstractBinaryDocValues 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 2 with AbstractBinaryDocValues

use of org.opensearch.index.fielddata.AbstractBinaryDocValues 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

AbstractBinaryDocValues (org.opensearch.index.fielddata.AbstractBinaryDocValues)2 SortedBinaryDocValues (org.opensearch.index.fielddata.SortedBinaryDocValues)2 IOException (java.io.IOException)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Scorable (org.apache.lucene.search.Scorable)1 BytesRef (org.apache.lucene.util.BytesRef)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 FixedBitSet (org.apache.lucene.util.FixedBitSet)1 BigArrays (org.opensearch.common.util.BigArrays)1 Nested (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)1 NumericDoubleValues (org.opensearch.index.fielddata.NumericDoubleValues)1 SortedNumericDoubleValues (org.opensearch.index.fielddata.SortedNumericDoubleValues)1 BytesRefFieldComparatorSource (org.opensearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource)1 DoubleValuesComparatorSource (org.opensearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource)1 QueryShardException (org.opensearch.index.query.QueryShardException)1 NumberSortScript (org.opensearch.script.NumberSortScript)1 StringSortScript (org.opensearch.script.StringSortScript)1 DocValueFormat (org.opensearch.search.DocValueFormat)1 MultiValueMode (org.opensearch.search.MultiValueMode)1