@@ -438,6 +438,11 @@ public void addFields(LuceneDocument document, String name, Number value, boolea
438438 }
439439 }
440440
441+ @ Override
442+ public long toSortableLong (Number value ) {
443+ return HalfFloatPoint .halfFloatToSortableShort (value .floatValue ());
444+ }
445+
441446 @ Override
442447 public IndexFieldData .Builder getFieldDataBuilder (MappedFieldType ft , ValuesSourceType valuesSourceType ) {
443448 return new SortedDoublesIndexFieldData .Builder (
@@ -622,6 +627,11 @@ public void addFields(LuceneDocument document, String name, Number value, boolea
622627 }
623628 }
624629
630+ @ Override
631+ public long toSortableLong (Number value ) {
632+ return NumericUtils .floatToSortableInt (value .floatValue ());
633+ }
634+
625635 @ Override
626636 public IndexFieldData .Builder getFieldDataBuilder (MappedFieldType ft , ValuesSourceType valuesSourceType ) {
627637 return new SortedDoublesIndexFieldData .Builder (
@@ -772,6 +782,11 @@ public void addFields(LuceneDocument document, String name, Number value, boolea
772782 }
773783 }
774784
785+ @ Override
786+ public long toSortableLong (Number value ) {
787+ return NumericUtils .doubleToSortableLong (value .doubleValue ());
788+ }
789+
775790 @ Override
776791 public IndexFieldData .Builder getFieldDataBuilder (MappedFieldType ft , ValuesSourceType valuesSourceType ) {
777792 return new SortedDoublesIndexFieldData .Builder (
@@ -891,6 +906,11 @@ public void addFields(LuceneDocument document, String name, Number value, boolea
891906 INTEGER .addFields (document , name , value , indexed , docValued , stored );
892907 }
893908
909+ @ Override
910+ public long toSortableLong (Number value ) {
911+ return INTEGER .toSortableLong (value );
912+ }
913+
894914 @ Override
895915 Number valueForSearch (Number value ) {
896916 return value .byteValue ();
@@ -1009,6 +1029,11 @@ public void addFields(LuceneDocument document, String name, Number value, boolea
10091029 INTEGER .addFields (document , name , value , indexed , docValued , stored );
10101030 }
10111031
1032+ @ Override
1033+ public long toSortableLong (Number value ) {
1034+ return INTEGER .toSortableLong (value );
1035+ }
1036+
10121037 @ Override
10131038 Number valueForSearch (Number value ) {
10141039 return value .shortValue ();
@@ -1206,6 +1231,11 @@ public void addFields(LuceneDocument document, String name, Number value, boolea
12061231 }
12071232 }
12081233
1234+ @ Override
1235+ public long toSortableLong (Number value ) {
1236+ return value .intValue ();
1237+ }
1238+
12091239 @ Override
12101240 public IndexFieldData .Builder getFieldDataBuilder (MappedFieldType ft , ValuesSourceType valuesSourceType ) {
12111241 return new SortedNumericIndexFieldData .Builder (
@@ -1358,6 +1388,11 @@ public void addFields(LuceneDocument document, String name, Number value, boolea
13581388 }
13591389 }
13601390
1391+ @ Override
1392+ public long toSortableLong (Number value ) {
1393+ return value .longValue ();
1394+ }
1395+
13611396 @ Override
13621397 public IndexFieldData .Builder getFieldDataBuilder (MappedFieldType ft , ValuesSourceType valuesSourceType ) {
13631398 return new SortedNumericIndexFieldData .Builder (
@@ -1506,6 +1541,13 @@ public abstract void addFields(
15061541 boolean stored
15071542 );
15081543
1544+ /**
1545+ * For a given {@code Number}, returns the sortable long representation that will be stored in the doc values.
1546+ * @param value number to convert
1547+ * @return sortable long representation
1548+ */
1549+ public abstract long toSortableLong (Number value );
1550+
15091551 public FieldValues <Number > compile (String fieldName , Script script , ScriptCompiler compiler ) {
15101552 // only implemented for long and double fields
15111553 throw new IllegalArgumentException ("Unknown parameter [script] for mapper [" + fieldName + "]" );
@@ -2140,7 +2182,10 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
21402182 }
21412183 if (offsetsFieldName != null && context .isImmediateParentAnArray () && context .canAddIgnoredField ()) {
21422184 if (value != null ) {
2143- context .getOffSetContext ().recordOffset (offsetsFieldName , (Comparable <?>) value );
2185+ // We cannot simply cast value to Comparable<> because we need to also capture the potential loss of precision that occurs
2186+ // when the value is stored into the doc values.
2187+ long sortableLongValue = type .toSortableLong (value );
2188+ context .getOffSetContext ().recordOffset (offsetsFieldName , sortableLongValue );
21442189 } else {
21452190 context .getOffSetContext ().recordNull (offsetsFieldName );
21462191 }
0 commit comments