From 0acde1ad5e357e8411244aecc404ebbfba266ff0 Mon Sep 17 00:00:00 2001 From: Fabio Massimo Ercoli Date: Wed, 28 Aug 2024 14:22:55 +0200 Subject: [PATCH] HSEARCH-5133 Address a bit the RAW model type --- ...tractLuceneMetricNumericFieldAggregation.java | 16 +++++++++++++--- .../MetricNumericFieldsAggregationsIT.java | 9 +++------ 2 files changed, 16 insertions(+), 9 deletions(-) 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 1aafd9c247d..011b9dadfb8 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 @@ -105,6 +105,7 @@ 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( sumFloats ) ).isEqualTo( 29F ); @@ -145,6 +146,7 @@ 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( sumFloats ) ).isEqualTo( 55F ); @@ -166,12 +168,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" ); - return options .aggregation( sumIntegers, f -> f.sum().field( "integer", Integer.class ) ) .aggregation( sumIntegersAsString, f -> f.sum().field( "integer", String.class, ValueModel.STRING ) ) @@ -194,6 +190,7 @@ 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 ) )