diff --git a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/search/aggregation/impl/ElasticsearchMetricFieldAggregation.java b/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/search/aggregation/impl/ElasticsearchMetricFieldAggregation.java index 8efa7c3fd2b..0a5f728449b 100644 --- a/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/search/aggregation/impl/ElasticsearchMetricFieldAggregation.java +++ b/backend/elasticsearch/src/main/java/org/hibernate/search/backend/elasticsearch/search/aggregation/impl/ElasticsearchMetricFieldAggregation.java @@ -126,9 +126,7 @@ private TypeSelector(ElasticsearchFieldCodec codec, @Override public Builder type(Class expectedType, ValueModel valueModel) { ProjectionConverter projectionConverter = null; - if ( !Double.class.isAssignableFrom( expectedType ) - || - field.type().projectionConverter( valueModel ).valueType().isAssignableFrom( expectedType ) ) { + if ( useProjectionConverter( expectedType, valueModel ) ) { projectionConverter = field.type().projectionConverter( valueModel ) .withConvertedType( expectedType, field ); } @@ -137,6 +135,18 @@ public Builder type(Class expectedType, ValueModel valueModel) { operation ); } + + private boolean useProjectionConverter(Class expectedType, ValueModel valueModel) { + if ( !Double.class.isAssignableFrom( expectedType ) ) { + return true; + } + + // expectedType == Double.class + if ( ValueModel.RAW.equals( valueModel ) ) { + return false; + } + return field.type().projectionConverter( valueModel ).valueType().isAssignableFrom( Double.class ); + } } private class MetricFieldExtractor extends AbstractExtractor { diff --git a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/AbstractLuceneMetricCompensatedSumAggregation.java b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/AbstractLuceneMetricCompensatedSumAggregation.java index 91cc9e3d374..29219e6a465 100644 --- a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/AbstractLuceneMetricCompensatedSumAggregation.java +++ b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/AbstractLuceneMetricCompensatedSumAggregation.java @@ -95,9 +95,7 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec codec, @Override public Builder type(Class expectedType, ValueModel valueModel) { ProjectionConverter projectionConverter = null; - if ( !Double.class.isAssignableFrom( expectedType ) - || - field.type().projectionConverter( valueModel ).valueType().isAssignableFrom( expectedType ) ) { + if ( useProjectionConverter( expectedType, valueModel ) ) { projectionConverter = field.type().projectionConverter( valueModel ) .withConvertedType( expectedType, field ); } @@ -105,6 +103,18 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec codec, return getFtBuilder( projectionConverter ); } + private boolean useProjectionConverter(Class expectedType, ValueModel valueModel) { + if ( !Double.class.isAssignableFrom( expectedType ) ) { + return true; + } + + // expectedType == Double.class + if ( ValueModel.RAW.equals( valueModel ) ) { + return false; + } + return field.type().projectionConverter( valueModel ).valueType().isAssignableFrom( Double.class ); + } + protected abstract Builder getFtBuilder( ProjectionConverter projectionConverter); } diff --git a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/AbstractLuceneMetricNumericFieldAggregation.java b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/AbstractLuceneMetricNumericFieldAggregation.java index b2823f21674..1df4bca92f7 100644 --- a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/AbstractLuceneMetricNumericFieldAggregation.java +++ b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/AbstractLuceneMetricNumericFieldAggregation.java @@ -107,9 +107,7 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec codec, @Override public Builder type(Class expectedType, ValueModel valueModel) { ProjectionConverter projectionConverter = null; - if ( !Double.class.isAssignableFrom( expectedType ) - || - field.type().projectionConverter( valueModel ).valueType().isAssignableFrom( expectedType ) ) { + if ( useProjectionConverter( expectedType, valueModel ) ) { projectionConverter = field.type().projectionConverter( valueModel ) .withConvertedType( expectedType, field ); } @@ -117,6 +115,18 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec codec, return getFtBuilder( projectionConverter ); } + private boolean useProjectionConverter(Class expectedType, ValueModel valueModel) { + if ( !Double.class.isAssignableFrom( expectedType ) ) { + return true; + } + + // expectedType == Double.class + if ( ValueModel.RAW.equals( valueModel ) ) { + return false; + } + return field.type().projectionConverter( valueModel ).valueType().isAssignableFrom( Double.class ); + } + protected abstract Builder getFtBuilder( ProjectionConverter projectionConverter); } diff --git a/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/search/aggregation/MetricNumericFieldsAggregationsIT.java b/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/search/aggregation/MetricNumericFieldsAggregationsIT.java index 6802ada7d64..c7bd366c18d 100644 --- a/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/search/aggregation/MetricNumericFieldsAggregationsIT.java +++ b/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/search/aggregation/MetricNumericFieldsAggregationsIT.java @@ -106,8 +106,10 @@ void test_filteringResults() { assertThat( result.aggregation( avgIntegersAsString ) ).isEqualTo( "5" ); assertThat( result.aggregation( avgConverted ) ).isEqualTo( "5" ); assertThat( result.aggregation( avgIntegersAsDouble ) ).isEqualTo( 5.8 ); + assertThat( result.aggregation( avgIntegersAsDoubleRaw ) ).isEqualTo( 5.8 ); assertThat( result.aggregation( avgIntegersAsDoubleFiltered ) ).isEqualTo( 7.666666666666667 ); assertThat( result.aggregation( sumDoubles ) ).isEqualTo( 29.0 ); + assertThat( result.aggregation( sumDoublesRaw ) ).isEqualTo( 29.0 ); assertThat( result.aggregation( sumFloats ) ).isEqualTo( 29F ); assertThat( result.aggregation( sumBigIntegers ) ).isEqualTo( BigInteger.valueOf( 29 ) ); assertThat( result.aggregation( sumBigDecimals ).setScale( 2, RoundingMode.CEILING ) ) @@ -146,8 +148,10 @@ void test_allResults() { assertThat( result.aggregation( avgIntegersAsString ) ).isEqualTo( "5" ); assertThat( result.aggregation( avgConverted ) ).isEqualTo( "5" ); assertThat( result.aggregation( avgIntegersAsDouble ) ).isEqualTo( 5.5 ); + assertThat( result.aggregation( avgIntegersAsDoubleRaw ) ).isEqualTo( 5.5 ); assertThat( result.aggregation( avgIntegersAsDoubleFiltered ) ).isEqualTo( 11.8 ); assertThat( result.aggregation( sumDoubles ) ).isEqualTo( 55.0 ); + assertThat( result.aggregation( sumDoublesRaw ) ).isEqualTo( 55.0 ); assertThat( result.aggregation( sumFloats ) ).isEqualTo( 55F ); assertThat( result.aggregation( sumBigIntegers ) ).isEqualTo( BigInteger.valueOf( 55 ) ); assertThat( result.aggregation( sumBigDecimals ).setScale( 2, RoundingMode.CEILING ) ) @@ -167,18 +171,6 @@ private SearchQuery defineAggregations( .isInstanceOf( AssertionFailure.class ) .hasMessageContaining( "Raw projection converter is not supported" ); - assertThatThrownBy( () -> { - options.aggregation( avgIntegersAsDoubleRaw, f -> f.avg().field( "integer", Double.class, ValueModel.RAW ) ); - } ) - .isInstanceOf( AssertionFailure.class ) - .hasMessageContaining( "Raw projection converter is not supported" ); - - assertThatThrownBy( () -> { - options.aggregation( sumDoublesRaw, f -> f.sum().field( "doubleF", Double.class, ValueModel.RAW ) ); - } ) - .isInstanceOf( AssertionFailure.class ) - .hasMessageContaining( "Raw projection converter is not supported" ); - return options .aggregation( sumIntegers, f -> f.sum().field( "integer", Integer.class ) ) .aggregation( sumIntegersAsString, f -> f.sum().field( "integer", String.class, ValueModel.STRING ) ) @@ -201,9 +193,11 @@ private SearchQuery defineAggregations( .aggregation( avgIntegersAsString, f -> f.avg().field( "integer", String.class, ValueModel.STRING ) ) .aggregation( avgConverted, f -> f.avg().field( "converted", String.class ) ) .aggregation( avgIntegersAsDouble, f -> f.avg().field( "integer", Double.class ) ) + .aggregation( avgIntegersAsDoubleRaw, f -> f.avg().field( "integer", Double.class, ValueModel.RAW ) ) .aggregation( avgIntegersAsDoubleFiltered, f -> f.avg().field( "object.nestedInteger", Double.class ) .filter( ff -> ff.range().field( "object.nestedInteger" ).atLeast( 5 ) ) ) .aggregation( sumDoubles, f -> f.sum().field( "doubleF", Double.class ) ) + .aggregation( sumDoublesRaw, f -> f.sum().field( "doubleF", Double.class, ValueModel.RAW ) ) .aggregation( sumFloats, f -> f.sum().field( "floatF", Float.class ) ) .aggregation( sumBigIntegers, f -> f.sum().field( "bigInteger", BigInteger.class ) ) .aggregation( sumBigDecimals, f -> f.sum().field( "bigDecimal", BigDecimal.class ) )