Skip to content

Commit e1a7dfe

Browse files
committed
MemoryIndex should not fail integer fields that enable doc values. (#12109)
When a field indexes numeric doc values, `MemoryIndex` does an unchecked cast to `java.lang.Long`. However, the new `IntField` represents the value as a `java.lang.Integer` so this cast fails. This commit aligns `MemoryIndex` with `IndexingChain` by casting to `Number` and calling `Number#longValue` instead of casting to `Long`.
1 parent 8aad5cd commit e1a7dfe

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ private void storeDocValues(Info info, DocValuesType docValuesType, Object docVa
596596
+ fieldName
597597
+ "]");
598598
}
599-
info.numericProducer.dvLongValues = new long[] {(long) docValuesValue};
599+
info.numericProducer.dvLongValues = new long[] {((Number) docValuesValue).longValue()};
600600
info.numericProducer.count++;
601601
break;
602602
case SORTED_NUMERIC:
@@ -605,7 +605,8 @@ private void storeDocValues(Info info, DocValuesType docValuesType, Object docVa
605605
}
606606
info.numericProducer.dvLongValues =
607607
ArrayUtil.grow(info.numericProducer.dvLongValues, info.numericProducer.count + 1);
608-
info.numericProducer.dvLongValues[info.numericProducer.count++] = (long) docValuesValue;
608+
info.numericProducer.dvLongValues[info.numericProducer.count++] =
609+
((Number) docValuesValue).longValue();
609610
break;
610611
case BINARY:
611612
case SORTED:

lucene/memory/src/test/org/apache/lucene/index/memory/TestMemoryIndex.java

+47
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.lucene.document.Field;
4040
import org.apache.lucene.document.FieldType;
4141
import org.apache.lucene.document.FloatPoint;
42+
import org.apache.lucene.document.IntField;
4243
import org.apache.lucene.document.IntPoint;
4344
import org.apache.lucene.document.LongPoint;
4445
import org.apache.lucene.document.NumericDocValuesField;
@@ -795,4 +796,50 @@ private static boolean arrayBinaryContains(BytesRef[] array, BytesRef value) {
795796
}
796797
return false;
797798
}
799+
800+
public void testIntegerNumericDocValue() throws IOException {
801+
// MemoryIndex used to fail when doc values are enabled and numericValue() returns an Integer
802+
// such as with IntField.
803+
FieldType ft = new FieldType();
804+
ft.setDocValuesType(DocValuesType.NUMERIC);
805+
ft.freeze();
806+
Field field =
807+
new Field("field", ft) {
808+
{
809+
fieldsData = 35;
810+
}
811+
};
812+
813+
FieldType multiFt = new FieldType();
814+
multiFt.setDocValuesType(DocValuesType.SORTED_NUMERIC);
815+
multiFt.freeze();
816+
Field multiField =
817+
new Field("multi_field", multiFt) {
818+
{
819+
fieldsData = 42;
820+
}
821+
};
822+
823+
Field intField = new IntField("int_field", 50);
824+
825+
MemoryIndex index = MemoryIndex.fromDocument(Arrays.asList(field, multiField, intField), null);
826+
IndexSearcher searcher = index.createSearcher();
827+
828+
NumericDocValues ndv =
829+
searcher.getIndexReader().leaves().get(0).reader().getNumericDocValues("field");
830+
assertTrue(ndv.advanceExact(0));
831+
assertEquals(35, ndv.longValue());
832+
833+
SortedNumericDocValues sndv =
834+
searcher.getIndexReader().leaves().get(0).reader().getSortedNumericDocValues("multi_field");
835+
assertTrue(sndv.advanceExact(0));
836+
assertEquals(1, sndv.docValueCount());
837+
assertEquals(42, sndv.nextValue());
838+
839+
sndv =
840+
searcher.getIndexReader().leaves().get(0).reader().getSortedNumericDocValues("int_field");
841+
assertTrue(sndv.advanceExact(0));
842+
assertEquals(1, sndv.docValueCount());
843+
assertEquals(50, sndv.nextValue());
844+
}
798845
}

0 commit comments

Comments
 (0)