Skip to content

Commit

Permalink
HSEARCH-5133 Push type-sel into each Lucene*Aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever committed Aug 28, 2024
1 parent 6d2e306 commit 444f315
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <F> The type of field values.
Expand Down Expand Up @@ -93,19 +92,16 @@ public K extract(AggregationExtractContext context) {
}
}

protected static class TypeSelector<F> implements FieldMetricAggregationBuilder.TypeSelector {
private final AbstractLuceneNumericFieldCodec<F, ?> codec;
private final LuceneSearchIndexScope<?> scope;
private final LuceneSearchIndexValueFieldContext<F> field;
private final String operation;
protected abstract static class TypeSelector<F> implements FieldMetricAggregationBuilder.TypeSelector {
protected final AbstractLuceneNumericFieldCodec<F, ?> codec;
protected final LuceneSearchIndexScope<?> scope;
protected final LuceneSearchIndexValueFieldContext<F> field;

protected TypeSelector(AbstractLuceneNumericFieldCodec<F, ?> codec,
LuceneSearchIndexScope<?> scope, LuceneSearchIndexValueFieldContext<F> field,
String operation) {
LuceneSearchIndexScope<?> scope, LuceneSearchIndexValueFieldContext<F> field) {
this.codec = codec;
this.scope = scope;
this.field = field;
this.operation = operation;
}

@Override
Expand All @@ -118,22 +114,11 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec<F, ?> 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 <T> Builder<F, ? extends Number, T> getFtBuilder(
ProjectionConverter<F, ? extends T> projectionConverter);
}

protected abstract static class Builder<F, E extends Number, K> extends AbstractBuilder<K>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,22 @@ protected Factory(AbstractLuceneNumericFieldCodec<F, ?> codec) {
@Override
public FieldMetricAggregationBuilder.TypeSelector create(LuceneSearchIndexScope<?> scope,
LuceneSearchIndexValueFieldContext<F> field) {
return new TypeSelector<>( codec, scope, field, "avg" );
return new FunctionTypeSelector<>( codec, scope, field );
}
}

protected static class FunctionTypeSelector<F> extends TypeSelector<F>
implements FieldMetricAggregationBuilder.TypeSelector {

protected FunctionTypeSelector(AbstractLuceneNumericFieldCodec<F, ?> codec, LuceneSearchIndexScope<?> scope,
LuceneSearchIndexValueFieldContext<F> field) {
super( codec, scope, field );
}

@Override
protected <T> Builder<F, ? extends Number, T> getFtBuilder(
ProjectionConverter<F, ? extends T> projectionConverter) {
return new Builder<>( codec, scope, field, projectionConverter );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ protected Factory(AbstractLuceneNumericFieldCodec<F, ?> codec) {
@Override
public FieldMetricAggregationBuilder.TypeSelector create(LuceneSearchIndexScope<?> scope,
LuceneSearchIndexValueFieldContext<F> field) {
return new TypeSelector<>( codec, scope, field, "max" );
return new FunctionTypeSelector<>( codec, scope, field );
}
}

protected static class FunctionTypeSelector<F> extends TypeSelector<F>
implements FieldMetricAggregationBuilder.TypeSelector {

protected FunctionTypeSelector(AbstractLuceneNumericFieldCodec<F, ?> codec, LuceneSearchIndexScope<?> scope,
LuceneSearchIndexValueFieldContext<F> field) {
super( codec, scope, field );
}

@Override
protected <T> Builder<F, ? extends Number, T> getFtBuilder(
ProjectionConverter<F, ? extends T> projectionConverter) {
return new Builder<>( codec, scope, field, projectionConverter );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ protected Factory(AbstractLuceneNumericFieldCodec<F, ?> codec) {
@Override
public FieldMetricAggregationBuilder.TypeSelector create(LuceneSearchIndexScope<?> scope,
LuceneSearchIndexValueFieldContext<F> field) {
return new TypeSelector<>( codec, scope, field, "min" );
return new FunctionTypeSelector<>( codec, scope, field );
}
}

protected static class FunctionTypeSelector<F> extends TypeSelector<F>
implements FieldMetricAggregationBuilder.TypeSelector {

protected FunctionTypeSelector(AbstractLuceneNumericFieldCodec<F, ?> codec, LuceneSearchIndexScope<?> scope,
LuceneSearchIndexValueFieldContext<F> field) {
super( codec, scope, field );
}

@Override
protected <T> Builder<F, ? extends Number, T> getFtBuilder(
ProjectionConverter<F, ? extends T> projectionConverter) {
return new Builder<>( codec, scope, field, projectionConverter );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ protected Factory(AbstractLuceneNumericFieldCodec<F, ?> codec) {
@Override
public FieldMetricAggregationBuilder.TypeSelector create(LuceneSearchIndexScope<?> scope,
LuceneSearchIndexValueFieldContext<F> field) {
return new TypeSelector<>( codec, scope, field, "sum" );
return new FunctionTypeSelector<>( codec, scope, field );
}
}

protected static class FunctionTypeSelector<F> extends TypeSelector<F>
implements FieldMetricAggregationBuilder.TypeSelector {

protected FunctionTypeSelector(AbstractLuceneNumericFieldCodec<F, ?> codec, LuceneSearchIndexScope<?> scope,
LuceneSearchIndexValueFieldContext<F> field) {
super( codec, scope, field );
}

@Override
protected <T> Builder<F, ? extends Number, T> getFtBuilder(
ProjectionConverter<F, ? extends T> projectionConverter) {
return new Builder<>( codec, scope, field, projectionConverter );
}
}

Expand Down

0 comments on commit 444f315

Please sign in to comment.