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");
}
}
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);
}
Aggregations