use of org.opensearch.script.StringSortScript 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");
}
}
Aggregations