Skip to content

Commit d808e75

Browse files
authored
Mapping: Fix NPE with scaled floats stats when field is not indexed (#23528)
This fixes an NPE in finding scaled float stats. The type of min/max methods on the wrapped long stats returns a boxed type, but in the case this is null, the unbox done for the FieldStats.Double ctor primitive types will cause the NPE. These methods would have null for min/max when the field exists, but does not actually have points values. fixes #23487
1 parent f7b8128 commit d808e75

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

core/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,16 @@ public FieldStats<?> stats(IndexReader reader) throws IOException {
265265
if (stats == null) {
266266
return null;
267267
}
268-
return new FieldStats.Double(stats.getMaxDoc(), stats.getDocCount(),
268+
if (stats.hasMinMax()) {
269+
return new FieldStats.Double(stats.getMaxDoc(), stats.getDocCount(),
269270
stats.getSumDocFreq(), stats.getSumTotalTermFreq(),
270271
stats.isSearchable(), stats.isAggregatable(),
271-
stats.getMinValue() == null ? null : stats.getMinValue() / scalingFactor,
272-
stats.getMaxValue() == null ? null : stats.getMaxValue() / scalingFactor);
272+
stats.getMinValue() / scalingFactor,
273+
stats.getMaxValue() / scalingFactor);
274+
}
275+
return new FieldStats.Double(stats.getMaxDoc(), stats.getDocCount(),
276+
stats.getSumDocFreq(), stats.getSumTotalTermFreq(),
277+
stats.isSearchable(), stats.isAggregatable());
273278
}
274279

275280
@Override

core/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldTypeTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.lucene.document.DoublePoint;
2424
import org.apache.lucene.document.LongPoint;
2525
import org.apache.lucene.document.SortedNumericDocValuesField;
26+
import org.apache.lucene.document.StoredField;
2627
import org.apache.lucene.index.DirectoryReader;
2728
import org.apache.lucene.index.IndexWriter;
2829
import org.apache.lucene.index.IndexWriterConfig;
@@ -143,6 +144,15 @@ public void testStats() throws IOException {
143144
assertNull(ft.stats(reader));
144145
}
145146
Document doc = new Document();
147+
doc.add(new StoredField("scaled_float", -1));
148+
w.addDocument(doc);
149+
try (DirectoryReader reader = DirectoryReader.open(w)) {
150+
// field exists, but has no point values
151+
FieldStats<?> stats = ft.stats(reader);
152+
assertFalse(stats.hasMinMax());
153+
assertNull(stats.getMinValue());
154+
assertNull(stats.getMaxValue());
155+
}
146156
LongPoint point = new LongPoint("scaled_float", -1);
147157
doc.add(point);
148158
w.addDocument(doc);
@@ -152,7 +162,7 @@ public void testStats() throws IOException {
152162
FieldStats<?> stats = ft.stats(reader);
153163
assertEquals(-1/ft.getScalingFactor(), stats.getMinValue());
154164
assertEquals(10/ft.getScalingFactor(), stats.getMaxValue());
155-
assertEquals(2, stats.getMaxDoc());
165+
assertEquals(3, stats.getMaxDoc());
156166
}
157167
w.deleteAll();
158168
try (DirectoryReader reader = DirectoryReader.open(w)) {

0 commit comments

Comments
 (0)