use of org.opensearch.index.fielddata.IndexNumericFieldData.NumericType in project OpenSearch by opensearch-project.
the class FieldSortBuilder method buildBucketedSort.
@Override
public BucketedSort buildBucketedSort(QueryShardContext context, int bucketSize, BucketedSort.ExtraData extra) throws IOException {
if (DOC_FIELD_NAME.equals(fieldName)) {
throw new IllegalArgumentException("sorting by _doc is not supported");
}
MappedFieldType fieldType = context.fieldMapper(fieldName);
Nested nested = nested(context, fieldType);
if (fieldType == null) {
fieldType = resolveUnmappedType(context);
}
IndexFieldData<?> fieldData = context.getForField(fieldType);
if (fieldData instanceof IndexNumericFieldData == false && (sortMode == SortMode.SUM || sortMode == SortMode.AVG || sortMode == SortMode.MEDIAN)) {
throw new QueryShardException(context, "we only support AVG, MEDIAN and SUM on number based fields");
}
if (numericType != null) {
if (fieldData instanceof IndexNumericFieldData == false) {
throw new QueryShardException(context, "[numeric_type] option cannot be set on a non-numeric field, got " + fieldType.typeName());
}
IndexNumericFieldData numericFieldData = (IndexNumericFieldData) fieldData;
NumericType resolvedType = resolveNumericType(numericType);
return numericFieldData.newBucketedSort(resolvedType, context.bigArrays(), missing, localSortMode(), nested, order, fieldType.docValueFormat(null, null), bucketSize, extra);
}
try {
return fieldData.newBucketedSort(context.bigArrays(), missing, localSortMode(), nested, order, fieldType.docValueFormat(null, null), bucketSize, extra);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("error building sort for field [" + fieldName + "] of type [" + fieldType.typeName() + "] in index [" + context.index().getName() + "]: " + e.getMessage(), e);
}
}
use of org.opensearch.index.fielddata.IndexNumericFieldData.NumericType in project OpenSearch by opensearch-project.
the class FieldSortBuilder method build.
@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
if (DOC_FIELD_NAME.equals(fieldName)) {
return order == SortOrder.DESC ? SORT_DOC_REVERSE : SORT_DOC;
}
MappedFieldType fieldType = context.fieldMapper(fieldName);
Nested nested = nested(context, fieldType);
if (fieldType == null) {
fieldType = resolveUnmappedType(context);
}
boolean reverse = order == SortOrder.DESC;
IndexFieldData<?> fieldData = context.getForField(fieldType);
if (fieldData instanceof IndexNumericFieldData == false && (sortMode == SortMode.SUM || sortMode == SortMode.AVG || sortMode == SortMode.MEDIAN)) {
throw new QueryShardException(context, "we only support AVG, MEDIAN and SUM on number based fields");
}
final SortField field;
boolean isNanosecond = false;
if (numericType != null) {
if (fieldData instanceof IndexNumericFieldData == false) {
throw new QueryShardException(context, "[numeric_type] option cannot be set on a non-numeric field, got " + fieldType.typeName());
}
IndexNumericFieldData numericFieldData = (IndexNumericFieldData) fieldData;
NumericType resolvedType = resolveNumericType(numericType);
field = numericFieldData.sortField(resolvedType, missing, localSortMode(), nested, reverse);
isNanosecond = resolvedType == NumericType.DATE_NANOSECONDS;
} else {
field = fieldData.sortField(missing, localSortMode(), nested, reverse);
if (fieldData instanceof IndexNumericFieldData) {
isNanosecond = ((IndexNumericFieldData) fieldData).getNumericType() == NumericType.DATE_NANOSECONDS;
}
}
DocValueFormat format = fieldType.docValueFormat(null, null);
if (isNanosecond) {
format = DocValueFormat.withNanosecondResolution(format);
}
return new SortFieldAndFormat(field, format);
}
Aggregations