use of org.opensearch.search.MultiValueMode in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilder method buildBucketedSort.
@Override
public BucketedSort buildBucketedSort(QueryShardContext context, int bucketSize, BucketedSort.ExtraData extra) throws IOException {
GeoPoint[] localPoints = localPoints();
MultiValueMode localSortMode = localSortMode();
IndexGeoPointFieldData geoIndexFieldData = fieldData(context);
Nested nested = nested(context);
return comparatorSource(localPoints, localSortMode, geoIndexFieldData, nested).newBucketedSort(context.bigArrays(), order, DocValueFormat.RAW, bucketSize, extra);
}
use of org.opensearch.search.MultiValueMode 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.search.MultiValueMode in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilder method build.
@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
GeoPoint[] localPoints = localPoints();
boolean reverse = order == SortOrder.DESC;
MultiValueMode localSortMode = localSortMode();
IndexGeoPointFieldData geoIndexFieldData = fieldData(context);
Nested nested = nested(context);
if (// only works with 5.x geo_point
geoIndexFieldData.getClass() == LatLonPointIndexFieldData.class && nested == null && // LatLonDocValuesField internally picks the closest point
localSortMode == MultiValueMode.MIN && unit == DistanceUnit.METERS && reverse == false && localPoints.length == 1) {
return new SortFieldAndFormat(LatLonDocValuesField.newDistanceSort(fieldName, localPoints[0].lat(), localPoints[0].lon()), DocValueFormat.RAW);
}
return new SortFieldAndFormat(new SortField(fieldName, comparatorSource(localPoints, localSortMode, geoIndexFieldData, nested), reverse), DocValueFormat.RAW);
}
use of org.opensearch.search.MultiValueMode in project OpenSearch by opensearch-project.
the class DecayFunctionParser method fromXContent.
/**
* Parses bodies of the kind
*
* <pre>
* <code>
* {
* "fieldname1" : {
* "origin" : "someValue",
* "scale" : "someValue"
* },
* "multi_value_mode" : "min"
* }
* </code>
* </pre>
*/
@Override
public DFB fromXContent(XContentParser parser) throws IOException, ParsingException {
String currentFieldName;
XContentParser.Token token;
MultiValueMode multiValueMode = DecayFunctionBuilder.DEFAULT_MULTI_VALUE_MODE;
String fieldName = null;
BytesReference functionBytes = null;
while ((token = parser.nextToken()) == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
fieldName = currentFieldName;
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.copyCurrentStructure(parser);
functionBytes = BytesReference.bytes(builder);
} else if (MULTI_VALUE_MODE.match(currentFieldName, parser.getDeprecationHandler())) {
multiValueMode = MultiValueMode.fromString(parser.text());
} else {
throw new ParsingException(parser.getTokenLocation(), "malformed score function score parameters.");
}
}
if (fieldName == null || functionBytes == null) {
throw new ParsingException(parser.getTokenLocation(), "malformed score function score parameters.");
}
DFB functionBuilder = createFromBytes.apply(fieldName, functionBytes);
functionBuilder.setMultiValueMode(multiValueMode);
return functionBuilder;
}
use of org.opensearch.search.MultiValueMode in project OpenSearch by opensearch-project.
the class AbstractNumberNestedSortingTestCase method assertAvgScoreMode.
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
MultiValueMode sortMode = MultiValueMode.AVG;
Query childFilter = Queries.not(parentFilter);
XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
TopDocs topDocs = searcher.search(query, 5, sort);
assertThat(topDocs.totalHits.value, equalTo(7L));
assertThat(topDocs.scoreDocs.length, equalTo(5));
assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[1].doc, equalTo(3));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(4));
}
Aggregations