/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.index.mapper;

import org.apache.lucene.document.HalfFloatPoint;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
import org.elasticsearch.index.mapper.NumberFieldTypeTests.OutOfRangeSpec;

import java.io.IOException;
import java.util.List;

public class HalfFloatFieldMapperTests extends NumberFieldMapperTests {

    @Override
    protected Number missingValue() {
        return 123f;
    }

    @Override
    protected List<OutOfRangeSpec> outOfRangeSpecs() {
        return List.of(
            OutOfRangeSpec.of(NumberType.HALF_FLOAT, "65520", "[half_float] supports only finite values"),
            OutOfRangeSpec.of(NumberType.HALF_FLOAT, "-65520", "[half_float] supports only finite values"),
            OutOfRangeSpec.of(NumberType.HALF_FLOAT, "-65520", "[half_float] supports only finite values"),
            OutOfRangeSpec.of(NumberType.HALF_FLOAT, Float.NaN, "[half_float] supports only finite values"),
            OutOfRangeSpec.of(NumberType.HALF_FLOAT, Float.POSITIVE_INFINITY, "[half_float] supports only finite values"),
            OutOfRangeSpec.of(NumberType.HALF_FLOAT, Float.NEGATIVE_INFINITY, "[half_float] supports only finite values")
        );
    }

    @Override
    protected void minimalMapping(XContentBuilder b) throws IOException {
        b.field("type", "half_float");
    }

    @Override
    protected Number randomNumber() {
        /*
         * The native valueFetcher returns 32 bits of precision but the
         * doc values fetcher returns 16 bits of precision. To make it
         * all line up we round here instead of in the fetcher. This bug
         * is tracked in:
         * https://github.com/elastic/elasticsearch/issues/70260
         */
        return HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat()));
    }
}

@@ -393,58 +393,8 @@
 r;%0A%0A
-import org.apache.lucene.document.HalfFloatPoint;%0A
 impo
@@ -1782,439 +1782,88 @@
     
-/*%0A         * The native valueFetcher returns 32 bits of precision but the%0A         * doc values fetcher returns 16 bits of precision. To make it%0A         * all line up we round here instead of in the fetcher. This bug%0A         * is tracked in:%0A         * https://github.com/elastic/elasticsearch/issues/70260%0A         */%0A        return HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat())
+return randomBoolean() ? randomFloat() : randomDoubleBetween(-65504, 65504, true
 );%0A 

Test unrounding half_floats (#70937)