Skip to content

Commit

Permalink
HSEARCH-5133 Address a bit the RAW model type
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever committed Aug 28, 2024
1 parent 51b255d commit 220288d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -126,9 +127,7 @@ private TypeSelector(ElasticsearchFieldCodec<F> codec,
@Override
public <T> Builder<F, T> type(Class<T> expectedType, ValueModel valueModel) {
ProjectionConverter<F, ? extends T> 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 );
}
Expand All @@ -137,6 +136,22 @@ public <T> Builder<F, T> type(Class<T> expectedType, ValueModel valueModel) {
operation
);
}

private <T> boolean useProjectionConverter(Class<T> 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<K> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <F> The type of field values.
Expand Down Expand Up @@ -95,16 +96,30 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec<F, ?> codec,
@Override
public <T> Builder<F, ?, T> type(Class<T> expectedType, ValueModel valueModel) {
ProjectionConverter<F, ? extends T> 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 );
}

return getFtBuilder( projectionConverter );
}

private <T> boolean useProjectionConverter(Class<T> 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 <T> Builder<F, ? extends Number, T> getFtBuilder(
ProjectionConverter<F, ? extends T> projectionConverter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <F> The type of field values.
Expand Down Expand Up @@ -107,16 +108,30 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec<F, ?> codec,
@Override
public <T> Builder<F, ?, T> type(Class<T> expectedType, ValueModel valueModel) {
ProjectionConverter<F, ? extends T> 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 );
}

return getFtBuilder( projectionConverter );
}

private <T> boolean useProjectionConverter(Class<T> 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 <T> Builder<F, ? extends Number, T> getFtBuilder(
ProjectionConverter<F, ? extends T> projectionConverter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
Expand Down Expand Up @@ -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 ) )
Expand All @@ -165,19 +169,7 @@ private SearchQuery<DocumentReference> 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 ) )
Expand All @@ -201,9 +193,11 @@ private SearchQuery<DocumentReference> 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 ) )
Expand Down

0 comments on commit 220288d

Please sign in to comment.