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 42b8d77 commit 7df846b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,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 +135,18 @@ 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 ) ) {
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 @@ -95,16 +95,26 @@ 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 ) ) {
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 @@ -107,16 +107,26 @@ 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 ) ) {
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 @@ -167,18 +171,6 @@ private SearchQuery<DocumentReference> 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 ) )
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 7df846b

Please sign in to comment.