Search in sources :

Example 1 with NumberSortScript

use of org.opensearch.script.NumberSortScript in project OpenSearch by opensearch-project.

the class ExpressionNumberSortScriptTests method testFieldAccess.

public void testFieldAccess() throws IOException {
    NumberSortScript script = compile("doc['field'].value").newInstance(null);
    script.setDocument(1);
    double result = script.execute();
    assertEquals(2.718, result, 0.0);
}
Also used : NumberSortScript(org.opensearch.script.NumberSortScript)

Example 2 with NumberSortScript

use of org.opensearch.script.NumberSortScript in project OpenSearch by opensearch-project.

the class ExpressionNumberSortScriptTests method testFieldAccessWithFieldAlias.

public void testFieldAccessWithFieldAlias() throws IOException {
    NumberSortScript script = compile("doc['alias'].value").newInstance(null);
    script.setDocument(1);
    double result = script.execute();
    assertEquals(2.718, result, 0.0);
}
Also used : NumberSortScript(org.opensearch.script.NumberSortScript)

Example 3 with NumberSortScript

use of org.opensearch.script.NumberSortScript 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)

Aggregations

NumberSortScript (org.opensearch.script.NumberSortScript)3 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Scorable (org.apache.lucene.search.Scorable)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 BigArrays (org.opensearch.common.util.BigArrays)1 AbstractBinaryDocValues (org.opensearch.index.fielddata.AbstractBinaryDocValues)1 Nested (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)1 NumericDoubleValues (org.opensearch.index.fielddata.NumericDoubleValues)1 SortedBinaryDocValues (org.opensearch.index.fielddata.SortedBinaryDocValues)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 StringSortScript (org.opensearch.script.StringSortScript)1 DocValueFormat (org.opensearch.search.DocValueFormat)1 MultiValueMode (org.opensearch.search.MultiValueMode)1