From 220288d9a973d7ff5dd5d2d89368eee2f96c87dd 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 --- .../ElasticsearchMetricFieldAggregation.java | 21 ++++++++++++++++--- ...LuceneMetricCompensatedSumAggregation.java | 21 ++++++++++++++++--- ...ctLuceneMetricNumericFieldAggregation.java | 21 ++++++++++++++++--- .../MetricNumericFieldsAggregationsIT.java | 20 +++++++----------- 4 files changed, 61 insertions(+), 22 deletions(-) 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..e8f7f1b933c 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 @@ -17,6 +17,7 @@ import org.hibernate.search.engine.backend.types.converter.spi.ProjectionConverter; import org.hibernate.search.engine.search.aggregation.spi.FieldMetricAggregationBuilder; import org.hibernate.search.engine.search.common.ValueModel; +import org.hibernate.search.util.common.AssertionFailure; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -126,9 +127,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 +136,22 @@ public Builder type(Class expectedType, ValueModel valueModel) { operation ); } + + private boolean useProjectionConverter(Class expectedType, ValueModel valueModel) { + if ( !Double.class.isAssignableFrom( expectedType ) ) { + if ( ValueModel.RAW.equals( valueModel ) ) { + throw new AssertionFailure( + "Raw projection converter is not supported with metric aggregations at the moment" ); + } + 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..1807b1416fb 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 @@ -19,6 +19,7 @@ import org.hibernate.search.engine.backend.types.converter.spi.ProjectionConverter; import org.hibernate.search.engine.search.aggregation.spi.FieldMetricAggregationBuilder; import org.hibernate.search.engine.search.common.ValueModel; +import org.hibernate.search.util.common.AssertionFailure; /** * @param The type of field values. @@ -95,9 +96,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 +104,22 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec codec, return getFtBuilder( projectionConverter ); } + private boolean useProjectionConverter(Class expectedType, ValueModel valueModel) { + if ( !Double.class.isAssignableFrom( expectedType ) ) { + if ( ValueModel.RAW.equals( valueModel ) ) { + throw new AssertionFailure( + "Raw projection converter is not supported with metric aggregations at the moment" ); + } + 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..aa1acc83f64 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 @@ -20,6 +20,7 @@ import org.hibernate.search.engine.cfg.spi.NumberUtils; import org.hibernate.search.engine.search.aggregation.spi.FieldMetricAggregationBuilder; import org.hibernate.search.engine.search.common.ValueModel; +import org.hibernate.search.util.common.AssertionFailure; /** * @param The type of field values. @@ -107,9 +108,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 +116,22 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec codec, return getFtBuilder( projectionConverter ); } + private boolean useProjectionConverter(Class expectedType, ValueModel valueModel) { + if ( !Double.class.isAssignableFrom( expectedType ) ) { + if ( ValueModel.RAW.equals( valueModel ) ) { + throw new AssertionFailure( + "Raw projection converter is not supported with metric aggregations at the moment" ); + } + 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..36e21e6ce26 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 ) ) @@ -165,19 +169,7 @@ private SearchQuery defineAggregations( options.aggregation( sumIntegersRaw, f -> f.sum().field( "integer", Object.class, ValueModel.RAW ) ); } ) .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" ); + .hasMessageContaining( "Raw projection converter is not supported with metric aggregations at the moment" ); return options .aggregation( sumIntegers, f -> f.sum().field( "integer", Integer.class ) ) @@ -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 ) )