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`.
This commit is contained in:
Adrien Grand 2023-01-24 10:51:49 +00:00 committed by GitHub
parent 25623a63bf
commit ce8eaf138c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View File

@ -596,7 +596,7 @@ public class MemoryIndex {
+ fieldName + fieldName
+ "]"); + "]");
} }
info.numericProducer.dvLongValues = new long[] {(long) docValuesValue}; info.numericProducer.dvLongValues = new long[] {((Number) docValuesValue).longValue()};
info.numericProducer.count++; info.numericProducer.count++;
break; break;
case SORTED_NUMERIC: case SORTED_NUMERIC:
@ -605,7 +605,8 @@ public class MemoryIndex {
} }
info.numericProducer.dvLongValues = info.numericProducer.dvLongValues =
ArrayUtil.grow(info.numericProducer.dvLongValues, info.numericProducer.count + 1); ArrayUtil.grow(info.numericProducer.dvLongValues, info.numericProducer.count + 1);
info.numericProducer.dvLongValues[info.numericProducer.count++] = (long) docValuesValue; info.numericProducer.dvLongValues[info.numericProducer.count++] =
((Number) docValuesValue).longValue();
break; break;
case BINARY: case BINARY:
case SORTED: case SORTED:

View File

@ -39,6 +39,7 @@ import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType; import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.FloatPoint; import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.IntPoint; import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint; import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.NumericDocValuesField;
@ -793,4 +794,50 @@ public class TestMemoryIndex extends LuceneTestCase {
} }
return false; return false;
} }
public void testIntegerNumericDocValue() throws IOException {
// MemoryIndex used to fail when doc values are enabled and numericValue() returns an Integer
// such as with IntField.
FieldType ft = new FieldType();
ft.setDocValuesType(DocValuesType.NUMERIC);
ft.freeze();
Field field =
new Field("field", ft) {
{
fieldsData = 35;
}
};
FieldType multiFt = new FieldType();
multiFt.setDocValuesType(DocValuesType.SORTED_NUMERIC);
multiFt.freeze();
Field multiField =
new Field("multi_field", multiFt) {
{
fieldsData = 42;
}
};
Field intField = new IntField("int_field", 50);
MemoryIndex index = MemoryIndex.fromDocument(Arrays.asList(field, multiField, intField), null);
IndexSearcher searcher = index.createSearcher();
NumericDocValues ndv =
searcher.getIndexReader().leaves().get(0).reader().getNumericDocValues("field");
assertTrue(ndv.advanceExact(0));
assertEquals(35, ndv.longValue());
SortedNumericDocValues sndv =
searcher.getIndexReader().leaves().get(0).reader().getSortedNumericDocValues("multi_field");
assertTrue(sndv.advanceExact(0));
assertEquals(1, sndv.docValueCount());
assertEquals(42, sndv.nextValue());
sndv =
searcher.getIndexReader().leaves().get(0).reader().getSortedNumericDocValues("int_field");
assertTrue(sndv.advanceExact(0));
assertEquals(1, sndv.docValueCount());
assertEquals(50, sndv.nextValue());
}
} }