diff --git a/server/src/main/java/org/elasticsearch/search/DocValueFormat.java b/server/src/main/java/org/elasticsearch/search/DocValueFormat.java index 4affcd15af34f..8702bbf926428 100644 --- a/server/src/main/java/org/elasticsearch/search/DocValueFormat.java +++ b/server/src/main/java/org/elasticsearch/search/DocValueFormat.java @@ -433,7 +433,7 @@ public long parseLong(String value, boolean roundUp, LongSupplier now) { try { n = format.parse(value); } catch (ParseException e) { - throw new RuntimeException(e); + throw new RuntimeException("Cannot parse the value [" + value + "] using the pattern [" + pattern + "]", e); } if (format.isParseIntegerOnly()) { return n.longValue(); @@ -454,7 +454,7 @@ public double parseDouble(String value, boolean roundUp, LongSupplier now) { try { n = format.parse(value); } catch (ParseException e) { - throw new RuntimeException(e); + throw new RuntimeException("Cannot parse the value [" + value + "] using the pattern [" + pattern + "]", e); } return n.doubleValue(); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java index 95fd3813e3e41..202c5ad0b0feb 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java @@ -85,7 +85,12 @@ public ValuesSource getField(FieldContext fieldContext, AggregationScript.LeafFa @Override public ValuesSource replaceMissing(ValuesSource valuesSource, Object rawMissing, DocValueFormat docValueFormat, AggregationContext context) { - Number missing = docValueFormat.parseDouble(rawMissing.toString(), false, context::nowInMillis); + Number missing; + if (rawMissing instanceof Number) { + missing = (Number) rawMissing; + } else { + missing = docValueFormat.parseDouble(rawMissing.toString(), false, context::nowInMillis); + } return MissingValues.replaceMissing((ValuesSource.Numeric) valuesSource, missing); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorTests.java index 65a84d9d584e5..2c59355ab9d10 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorTests.java @@ -1421,6 +1421,40 @@ public void testOrderByPipelineAggregation() throws Exception { } } + public void testFormatWithMissing() throws IOException { + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); + + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("name") + .field("number") + .format("$###.00") + .missing(randomFrom(42, "$42", 42.0)); + + testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { + final int numDocs = 10; + iw.addDocument(singleton(new NumericDocValuesField("not_number", 0))); + for (int i = 1; i < numDocs; i++) { + iw.addDocument(singleton(new NumericDocValuesField("number", i + 1))); + } + }, (Consumer>) terms -> assertTrue(AggregationInspectionHelper.hasValue(terms)), fieldType); + } + + public void testFormatCannotParseMissing() throws IOException { + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); + + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("name").field("number").format("$###.00").missing("42"); + + RuntimeException ex = expectThrows(RuntimeException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { + final int numDocs = 10; + iw.addDocument(singleton(new NumericDocValuesField("not_number", 0))); + for (int i = 1; i < numDocs; i++) { + iw.addDocument(singleton(new NumericDocValuesField("number", i + 1))); + } + }, (Consumer>) terms -> fail("Should have thrown"), fieldType)); + + assertThat(ex.getMessage(), equalTo("Cannot parse the value [42] using the pattern [$###.00]")); + } + public void testOrderByCardinality() throws IOException { boolean bIsString = randomBoolean(); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("a").field("a")