Search in sources :

Example 1 with NumericType

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);
    }
}
Also used : NumericType(org.opensearch.index.fielddata.IndexNumericFieldData.NumericType) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 2 with NumericType

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);
}
Also used : NumericType(org.opensearch.index.fielddata.IndexNumericFieldData.NumericType) DocValueFormat(org.opensearch.search.DocValueFormat) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) QueryShardException(org.opensearch.index.query.QueryShardException) SortField(org.apache.lucene.search.SortField)

Aggregations

Nested (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)2 IndexNumericFieldData (org.opensearch.index.fielddata.IndexNumericFieldData)2 NumericType (org.opensearch.index.fielddata.IndexNumericFieldData.NumericType)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 QueryShardException (org.opensearch.index.query.QueryShardException)2 SortField (org.apache.lucene.search.SortField)1 DocValueFormat (org.opensearch.search.DocValueFormat)1