From eb334a359aeea5adcf4626273fb483b552ddeb39 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Mon, 11 Nov 2019 17:00:51 -0500 Subject: [PATCH] Fix ignoring missing values in min/max aggregations Fixes the issue when the missing values can be ignored in min/max due to BKD optimization. Fixes #48905 --- .../aggregations/metrics/MinAggregator.java | 2 +- .../metrics/MaxAggregatorTests.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java index 0a40347a3cf4b..38841d7e4d809 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java @@ -181,7 +181,7 @@ static Function getPointReaderOrNull(SearchContext context, Aggr if (parent != null) { return null; } - if (config.fieldContext() != null && config.script() == null) { + if (config.fieldContext() != null && config.script() == null && config.missing() == null) { MappedFieldType fieldType = config.fieldContext().fieldType(); if (fieldType == null || fieldType.indexOptions() == IndexOptions.NONE) { return null; diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java index b841fffd0ba36..739419a534240 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java @@ -87,6 +87,7 @@ import java.util.function.Function; import java.util.function.Supplier; +import static java.util.Collections.emptyList; import static java.util.Collections.singleton; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.hamcrest.Matchers.equalTo; @@ -232,6 +233,22 @@ public void testUnmappedWithMissingField() throws IOException { }, null); } + public void testMissingFieldOptimization() throws IOException { + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); + fieldType.setName("number"); + + MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number").missing(19L); + + testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { + iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7))); + iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 1))); + iw.addDocument(emptyList()); + }, max -> { + assertEquals(max.getValue(), 19.0, 0); + assertTrue(AggregationInspectionHelper.hasValue(max)); + }, fieldType); + } + public void testScript() throws IOException { MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); fieldType.setName("number");