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 c9423156a82..b2823f21674 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,7 +20,6 @@ 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. @@ -93,19 +92,16 @@ public K extract(AggregationExtractContext context) { } } - protected static class TypeSelector implements FieldMetricAggregationBuilder.TypeSelector { - private final AbstractLuceneNumericFieldCodec codec; - private final LuceneSearchIndexScope scope; - private final LuceneSearchIndexValueFieldContext field; - private final String operation; + protected abstract static class TypeSelector implements FieldMetricAggregationBuilder.TypeSelector { + protected final AbstractLuceneNumericFieldCodec codec; + protected final LuceneSearchIndexScope scope; + protected final LuceneSearchIndexValueFieldContext field; protected TypeSelector(AbstractLuceneNumericFieldCodec codec, - LuceneSearchIndexScope scope, LuceneSearchIndexValueFieldContext field, - String operation) { + LuceneSearchIndexScope scope, LuceneSearchIndexValueFieldContext field) { this.codec = codec; this.scope = scope; this.field = field; - this.operation = operation; } @Override @@ -118,22 +114,11 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec codec, .withConvertedType( expectedType, field ); } - if ( "sum".equals( operation ) ) { - return new LuceneSumNumericFieldAggregation.Builder<>( codec, scope, field, projectionConverter ); - } - else if ( "min".equals( operation ) ) { - return new LuceneMinNumericFieldAggregation.Builder<>( codec, scope, field, projectionConverter ); - } - else if ( "max".equals( operation ) ) { - return new LuceneMaxNumericFieldAggregation.Builder<>( codec, scope, field, projectionConverter ); - } - else if ( "avg".equals( operation ) ) { - return new LuceneAvgNumericFieldAggregation.Builder<>( codec, scope, field, projectionConverter ); - } - else { - throw new AssertionFailure( "Aggregation operation not supported: " + operation ); - } + return getFtBuilder( projectionConverter ); } + + protected abstract Builder getFtBuilder( + ProjectionConverter projectionConverter); } protected abstract static class Builder extends AbstractBuilder diff --git a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneAvgNumericFieldAggregation.java b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneAvgNumericFieldAggregation.java index 12f1abe95e8..ed0f5c98b52 100644 --- a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneAvgNumericFieldAggregation.java +++ b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneAvgNumericFieldAggregation.java @@ -48,7 +48,22 @@ protected Factory(AbstractLuceneNumericFieldCodec codec) { @Override public FieldMetricAggregationBuilder.TypeSelector create(LuceneSearchIndexScope scope, LuceneSearchIndexValueFieldContext field) { - return new TypeSelector<>( codec, scope, field, "avg" ); + return new FunctionTypeSelector<>( codec, scope, field ); + } + } + + protected static class FunctionTypeSelector extends TypeSelector + implements FieldMetricAggregationBuilder.TypeSelector { + + protected FunctionTypeSelector(AbstractLuceneNumericFieldCodec codec, LuceneSearchIndexScope scope, + LuceneSearchIndexValueFieldContext field) { + super( codec, scope, field ); + } + + @Override + protected Builder getFtBuilder( + ProjectionConverter projectionConverter) { + return new Builder<>( codec, scope, field, projectionConverter ); } } diff --git a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneMaxNumericFieldAggregation.java b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneMaxNumericFieldAggregation.java index 54f36b8e0fa..33829737b32 100644 --- a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneMaxNumericFieldAggregation.java +++ b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneMaxNumericFieldAggregation.java @@ -44,7 +44,22 @@ protected Factory(AbstractLuceneNumericFieldCodec codec) { @Override public FieldMetricAggregationBuilder.TypeSelector create(LuceneSearchIndexScope scope, LuceneSearchIndexValueFieldContext field) { - return new TypeSelector<>( codec, scope, field, "max" ); + return new FunctionTypeSelector<>( codec, scope, field ); + } + } + + protected static class FunctionTypeSelector extends TypeSelector + implements FieldMetricAggregationBuilder.TypeSelector { + + protected FunctionTypeSelector(AbstractLuceneNumericFieldCodec codec, LuceneSearchIndexScope scope, + LuceneSearchIndexValueFieldContext field) { + super( codec, scope, field ); + } + + @Override + protected Builder getFtBuilder( + ProjectionConverter projectionConverter) { + return new Builder<>( codec, scope, field, projectionConverter ); } } diff --git a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneMinNumericFieldAggregation.java b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneMinNumericFieldAggregation.java index 458c3c504bd..70bbd260518 100644 --- a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneMinNumericFieldAggregation.java +++ b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneMinNumericFieldAggregation.java @@ -44,7 +44,22 @@ protected Factory(AbstractLuceneNumericFieldCodec codec) { @Override public FieldMetricAggregationBuilder.TypeSelector create(LuceneSearchIndexScope scope, LuceneSearchIndexValueFieldContext field) { - return new TypeSelector<>( codec, scope, field, "min" ); + return new FunctionTypeSelector<>( codec, scope, field ); + } + } + + protected static class FunctionTypeSelector extends TypeSelector + implements FieldMetricAggregationBuilder.TypeSelector { + + protected FunctionTypeSelector(AbstractLuceneNumericFieldCodec codec, LuceneSearchIndexScope scope, + LuceneSearchIndexValueFieldContext field) { + super( codec, scope, field ); + } + + @Override + protected Builder getFtBuilder( + ProjectionConverter projectionConverter) { + return new Builder<>( codec, scope, field, projectionConverter ); } } diff --git a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneSumNumericFieldAggregation.java b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneSumNumericFieldAggregation.java index f2a7e1c715c..3630e971016 100644 --- a/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneSumNumericFieldAggregation.java +++ b/backend/lucene/src/main/java/org/hibernate/search/backend/lucene/types/aggregation/impl/LuceneSumNumericFieldAggregation.java @@ -44,7 +44,22 @@ protected Factory(AbstractLuceneNumericFieldCodec codec) { @Override public FieldMetricAggregationBuilder.TypeSelector create(LuceneSearchIndexScope scope, LuceneSearchIndexValueFieldContext field) { - return new TypeSelector<>( codec, scope, field, "sum" ); + return new FunctionTypeSelector<>( codec, scope, field ); + } + } + + protected static class FunctionTypeSelector extends TypeSelector + implements FieldMetricAggregationBuilder.TypeSelector { + + protected FunctionTypeSelector(AbstractLuceneNumericFieldCodec codec, LuceneSearchIndexScope scope, + LuceneSearchIndexValueFieldContext field) { + super( codec, scope, field ); + } + + @Override + protected Builder getFtBuilder( + ProjectionConverter projectionConverter) { + return new Builder<>( codec, scope, field, projectionConverter ); } }