Migrate sum, min, and max aggs to NamedWriteable

This commit is contained in:
Nik Everett 2016-06-29 16:59:14 -04:00
parent 91b66e3cf4
commit 27e320d5ce
7 changed files with 81 additions and 136 deletions

View File

@ -489,9 +489,9 @@ public class SearchModule extends AbstractModule {
private void registerBuiltinAggregations() {
registerAggregation(AvgAggregationBuilder::new, InternalAvg::new, new AvgParser(), AvgAggregationBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(SumAggregationBuilder::new, new SumParser(), SumAggregationBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(MinAggregationBuilder::new, new MinParser(), MinAggregationBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(MaxAggregationBuilder::new, new MaxParser(), MaxAggregationBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(SumAggregationBuilder::new, InternalSum::new, new SumParser(), SumAggregationBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(MinAggregationBuilder::new, InternalMin::new, new MinParser(), MinAggregationBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(MaxAggregationBuilder::new, InternalMax::new, new MaxParser(), MaxAggregationBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(StatsAggregationBuilder::new, InternalStats::new, new StatsParser(),
StatsAggregationBuilder.AGGREGATION_NAME_FIELD);
registerAggregation(ExtendedStatsAggregationBuilder::new, InternalExtendedStats::new, new ExtendedStatsParser(),
@ -721,9 +721,6 @@ public class SearchModule extends AbstractModule {
static {
// calcs
InternalSum.registerStreams();
InternalMin.registerStreams();
InternalMax.registerStreams();
InternalValueCount.registerStreams();
InternalTDigestPercentiles.registerStreams();
InternalTDigestPercentileRanks.registerStreams();

View File

@ -22,7 +22,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.AggregationStreams;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
@ -31,29 +30,8 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
*
*/
public class InternalMax extends InternalNumericMetricsAggregation.SingleValue implements Max {
public final static Type TYPE = new Type("max");
public final static AggregationStreams.Stream STREAM = new AggregationStreams.Stream() {
@Override
public InternalMax readResult(StreamInput in) throws IOException {
InternalMax result = new InternalMax();
result.readFrom(in);
return result;
}
};
public static void registerStreams() {
AggregationStreams.registerStream(STREAM, TYPE.stream());
}
private double max;
InternalMax() {} // for serialization
private final double max;
public InternalMax(String name, double max, DocValueFormat formatter, List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) {
@ -62,6 +40,26 @@ public class InternalMax extends InternalNumericMetricsAggregation.SingleValue i
this.max = max;
}
/**
* Read from a stream.
*/
public InternalMax(StreamInput in) throws IOException {
super(in);
format = in.readNamedWriteable(DocValueFormat.class);
max = in.readDouble();
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(format);
out.writeDouble(max);
}
@Override
public String getWriteableName() {
return MaxAggregationBuilder.NAME;
}
@Override
public double value() {
return max;
@ -72,11 +70,6 @@ public class InternalMax extends InternalNumericMetricsAggregation.SingleValue i
return max;
}
@Override
public Type type() {
return TYPE;
}
@Override
public InternalMax doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
double max = Double.NEGATIVE_INFINITY;
@ -86,18 +79,6 @@ public class InternalMax extends InternalNumericMetricsAggregation.SingleValue i
return new InternalMax(name, max, format, pipelineAggregators(), getMetaData());
}
@Override
protected void doReadFrom(StreamInput in) throws IOException {
format = in.readNamedWriteable(DocValueFormat.class);
max = in.readDouble();
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(format);
out.writeDouble(max);
}
@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
boolean hasValue = !Double.isInfinite(max);

View File

@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.InternalAggregation.Type;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValueType;
@ -36,18 +37,19 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException;
public class MaxAggregationBuilder extends ValuesSourceAggregationBuilder.LeafOnly<ValuesSource.Numeric, MaxAggregationBuilder> {
public static final String NAME = InternalMax.TYPE.name();
public static final String NAME = "max";
public final static Type TYPE = new Type(NAME);
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
public MaxAggregationBuilder(String name) {
super(name, InternalMax.TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
super(name, TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
}
/**
* Read from a stream.
*/
public MaxAggregationBuilder(StreamInput in) throws IOException {
super(in, InternalMax.TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
super(in, TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
}
@Override

View File

@ -22,7 +22,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.AggregationStreams;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
@ -31,30 +30,8 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
*
*/
public class InternalMin extends InternalNumericMetricsAggregation.SingleValue implements Min {
public final static Type TYPE = new Type("min");
public final static AggregationStreams.Stream STREAM = new AggregationStreams.Stream() {
@Override
public InternalMin readResult(StreamInput in) throws IOException {
InternalMin result = new InternalMin();
result.readFrom(in);
return result;
}
};
public static void registerStreams() {
AggregationStreams.registerStream(STREAM, TYPE.stream());
}
private double min;
InternalMin() {} // for serialization
private final double min;
public InternalMin(String name, double min, DocValueFormat formatter, List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) {
@ -63,6 +40,26 @@ public class InternalMin extends InternalNumericMetricsAggregation.SingleValue i
this.format = formatter;
}
/**
* Read from a stream.
*/
public InternalMin(StreamInput in) throws IOException {
super(in);
format = in.readNamedWriteable(DocValueFormat.class);
min = in.readDouble();
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(format);
out.writeDouble(min);
}
@Override
public String getWriteableName() {
return MinAggregationBuilder.NAME;
}
@Override
public double value() {
return min;
@ -73,11 +70,6 @@ public class InternalMin extends InternalNumericMetricsAggregation.SingleValue i
return min;
}
@Override
public Type type() {
return TYPE;
}
@Override
public InternalMin doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
double min = Double.POSITIVE_INFINITY;
@ -87,18 +79,6 @@ public class InternalMin extends InternalNumericMetricsAggregation.SingleValue i
return new InternalMin(getName(), min, this.format, pipelineAggregators(), getMetaData());
}
@Override
protected void doReadFrom(StreamInput in) throws IOException {
format = in.readNamedWriteable(DocValueFormat.class);
min = in.readDouble();
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(format);
out.writeDouble(min);
}
@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
boolean hasValue = !Double.isInfinite(min);

View File

@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.InternalAggregation.Type;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValueType;
@ -36,18 +37,19 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException;
public class MinAggregationBuilder extends ValuesSourceAggregationBuilder.LeafOnly<ValuesSource.Numeric, MinAggregationBuilder> {
public static final String NAME = InternalMin.TYPE.name();
public static final String NAME = "min";
private final static Type TYPE = new Type(NAME);
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
public MinAggregationBuilder(String name) {
super(name, InternalMin.TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
super(name, TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
}
/**
* Read from a stream.
*/
public MinAggregationBuilder(StreamInput in) throws IOException {
super(in, InternalMin.TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
super(in, TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
}
@Override

View File

@ -22,7 +22,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.AggregationStreams;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
@ -31,29 +30,8 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
*
*/
public class InternalSum extends InternalNumericMetricsAggregation.SingleValue implements Sum {
public final static Type TYPE = new Type("sum");
public final static AggregationStreams.Stream STREAM = new AggregationStreams.Stream() {
@Override
public InternalSum readResult(StreamInput in) throws IOException {
InternalSum result = new InternalSum();
result.readFrom(in);
return result;
}
};
public static void registerStreams() {
AggregationStreams.registerStream(STREAM, TYPE.stream());
}
private double sum;
InternalSum() {} // for serialization
private final double sum;
InternalSum(String name, double sum, DocValueFormat formatter, List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) {
@ -62,6 +40,26 @@ public class InternalSum extends InternalNumericMetricsAggregation.SingleValue i
this.format = formatter;
}
/**
* Read from a stream.
*/
public InternalSum(StreamInput in) throws IOException {
super(in);
format = in.readNamedWriteable(DocValueFormat.class);
sum = in.readDouble();
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(format);
out.writeDouble(sum);
}
@Override
public String getWriteableName() {
return SumAggregationBuilder.NAME;
}
@Override
public double value() {
return sum;
@ -72,11 +70,6 @@ public class InternalSum extends InternalNumericMetricsAggregation.SingleValue i
return sum;
}
@Override
public Type type() {
return TYPE;
}
@Override
public InternalSum doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
double sum = 0;
@ -86,18 +79,6 @@ public class InternalSum extends InternalNumericMetricsAggregation.SingleValue i
return new InternalSum(name, sum, format, pipelineAggregators(), getMetaData());
}
@Override
protected void doReadFrom(StreamInput in) throws IOException {
format = in.readNamedWriteable(DocValueFormat.class);
sum = in.readDouble();
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(format);
out.writeDouble(sum);
}
@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(CommonFields.VALUE, sum);

View File

@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.InternalAggregation.Type;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValueType;
@ -36,18 +37,19 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException;
public class SumAggregationBuilder extends ValuesSourceAggregationBuilder.LeafOnly<ValuesSource.Numeric, SumAggregationBuilder> {
public static final String NAME = InternalSum.TYPE.name();
public static final String NAME = "sum";
private final static Type TYPE = new Type(NAME);
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
public SumAggregationBuilder(String name) {
super(name, InternalSum.TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
super(name, TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
}
/**
* Read from a stream.
*/
public SumAggregationBuilder(StreamInput in) throws IOException {
super(in, InternalSum.TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
super(in, TYPE, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
}
@Override