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 e083fbe commit de44e4e
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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 @@ -81,19 +80,16 @@ public K extract(AggregationExtractContext context) {

abstract E extractEncoded(AggregationExtractContext context, LuceneNumericDomain<E> numericDomain);

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 @@ -106,16 +102,11 @@ protected TypeSelector(AbstractLuceneNumericFieldCodec<F, ?> codec,
.withConvertedType( expectedType, field );
}

if ( "sum".equals( operation ) ) {
return new LuceneSumCompensatedSumAggregation.Builder<>( codec, scope, field, projectionConverter );
}
else if ( "avg".equals( operation ) ) {
return new LuceneAvgCompensatedSumAggregation.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 @@ -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 @@ -61,7 +61,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 @@ -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 @@ -54,7 +54,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
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 de44e4e

Please sign in to comment.