Search in sources :

Example 1 with DoubleValuesComparatorSource

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

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

the class LuceneTests method randomSortFieldCustomComparatorSource.

private static Tuple<SortField, SortField> randomSortFieldCustomComparatorSource() {
    String field = randomAlphaOfLengthBetween(3, 10);
    IndexFieldData.XFieldComparatorSource comparatorSource;
    boolean reverse = randomBoolean();
    Object missingValue = null;
    switch(randomIntBetween(0, 3)) {
        case 0:
            comparatorSource = new LongValuesComparatorSource(null, randomBoolean() ? randomLong() : null, randomFrom(MultiValueMode.values()), null);
            break;
        case 1:
            comparatorSource = new DoubleValuesComparatorSource(null, randomBoolean() ? randomDouble() : null, randomFrom(MultiValueMode.values()), null);
            break;
        case 2:
            comparatorSource = new FloatValuesComparatorSource(null, randomBoolean() ? randomFloat() : null, randomFrom(MultiValueMode.values()), null);
            break;
        case 3:
            comparatorSource = new BytesRefFieldComparatorSource(null, randomBoolean() ? "_first" : "_last", randomFrom(MultiValueMode.values()), null);
            missingValue = comparatorSource.missingValue(reverse);
            break;
        default:
            throw new UnsupportedOperationException();
    }
    SortField sortField = new SortField(field, comparatorSource, reverse);
    SortField expected = new SortField(field, comparatorSource.reducedType(), reverse);
    expected.setMissingValue(missingValue);
    return Tuple.tuple(sortField, expected);
}
Also used : DoubleValuesComparatorSource(org.opensearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource) FloatValuesComparatorSource(org.opensearch.index.fielddata.fieldcomparator.FloatValuesComparatorSource) LongValuesComparatorSource(org.opensearch.index.fielddata.fieldcomparator.LongValuesComparatorSource) BytesRefFieldComparatorSource(org.opensearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource) IndexFieldData(org.opensearch.index.fielddata.IndexFieldData) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Aggregations

BytesRefFieldComparatorSource (org.opensearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource)2 DoubleValuesComparatorSource (org.opensearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource)2 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Scorable (org.apache.lucene.search.Scorable)1 SortField (org.apache.lucene.search.SortField)1 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)1 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 BigArrays (org.opensearch.common.util.BigArrays)1 AbstractBinaryDocValues (org.opensearch.index.fielddata.AbstractBinaryDocValues)1 IndexFieldData (org.opensearch.index.fielddata.IndexFieldData)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 FloatValuesComparatorSource (org.opensearch.index.fielddata.fieldcomparator.FloatValuesComparatorSource)1 LongValuesComparatorSource (org.opensearch.index.fielddata.fieldcomparator.LongValuesComparatorSource)1 QueryShardException (org.opensearch.index.query.QueryShardException)1 NumberSortScript (org.opensearch.script.NumberSortScript)1