Skip to content

Commit baa5817

Browse files
authored
Fix ignoring missing values in min/max aggregations (elastic#48970)
Fixes the issue when the missing values can be ignored in min/max due to BKD optimization. Fixes elastic#48905
1 parent 0de3555 commit baa5817

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static Function<byte[], Number> getPointReaderOrNull(SearchContext context, Aggr
181181
if (parent != null) {
182182
return null;
183183
}
184-
if (config.fieldContext() != null && config.script() == null) {
184+
if (config.fieldContext() != null && config.script() == null && config.missing() == null) {
185185
MappedFieldType fieldType = config.fieldContext().fieldType();
186186
if (fieldType == null || fieldType.indexOptions() == IndexOptions.NONE) {
187187
return null;

server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import java.util.function.Function;
8888
import java.util.function.Supplier;
8989

90+
import static java.util.Collections.emptyList;
9091
import static java.util.Collections.singleton;
9192
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
9293
import static org.hamcrest.Matchers.equalTo;
@@ -232,6 +233,22 @@ public void testUnmappedWithMissingField() throws IOException {
232233
}, null);
233234
}
234235

236+
public void testMissingFieldOptimization() throws IOException {
237+
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
238+
fieldType.setName("number");
239+
240+
MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number").missing(19L);
241+
242+
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
243+
iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
244+
iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1)));
245+
iw.addDocument(emptyList());
246+
}, max -> {
247+
assertEquals(max.getValue(), 19.0, 0);
248+
assertTrue(AggregationInspectionHelper.hasValue(max));
249+
}, fieldType);
250+
}
251+
235252
public void testScript() throws IOException {
236253
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
237254
fieldType.setName("number");

0 commit comments

Comments
 (0)