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 95d17dd commit 0acde1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
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 @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -166,12 +168,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" );

return options
.aggregation( sumIntegers, f -> f.sum().field( "integer", Integer.class ) )
.aggregation( sumIntegersAsString, f -> f.sum().field( "integer", String.class, ValueModel.STRING ) )
Expand All @@ -194,6 +190,7 @@ 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 ) )
Expand Down

0 comments on commit 0acde1a

Please sign in to comment.