From 4e008952b23b748a2c91e3a7e5830486060a44ed Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Mon, 6 Jul 2015 10:23:44 +0100 Subject: [PATCH] Aggregations Refactor: Refactor Min Aggregation --- .../metrics/min/MinAggregator.java | 37 ++++++++++++++++-- .../aggregations/metrics/min/MinParser.java | 38 ++++++++++++++----- .../aggregations/BaseAggregationTestCase.java | 1 + .../search/aggregations/metrics/MinTests.java | 32 ++++++++++++++++ 4 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/search/aggregations/metrics/MinTests.java diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinAggregator.java index 409616d5879..ad0365c332a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinAggregator.java @@ -19,9 +19,12 @@ package org.elasticsearch.search.aggregations.metrics.min; import org.apache.lucene.index.LeafReaderContext; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.NumericDoubleValues; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import org.elasticsearch.search.MultiValueMode; @@ -32,9 +35,11 @@ import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; +import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; @@ -115,8 +120,8 @@ public class MinAggregator extends NumericMetricsAggregator.SingleValue { public static class Factory extends ValuesSourceAggregatorFactory.LeafOnly { - public Factory(String name, ValuesSourceParser.Input valuesSourceInput) { - super(name, InternalMin.TYPE.name(), valuesSourceInput); + public Factory(String name) { + super(name, InternalMin.TYPE.name(), ValuesSourceType.NUMERIC, ValueType.NUMERIC); } @Override @@ -131,6 +136,32 @@ public class MinAggregator extends NumericMetricsAggregator.SingleValue { throws IOException { return new MinAggregator(name, valuesSource, config.formatter(), aggregationContext, parent, pipelineAggregators, metaData); } + + @Override + protected ValuesSourceAggregatorFactory innerReadFrom(String name, ValuesSourceType valuesSourceType, + ValueType targetValueType, StreamInput in) { + return new MinAggregator.Factory(name); + } + + @Override + protected void innerWriteTo(StreamOutput out) { + // Do nothing, no extra state to write to stream + } + + @Override + public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { + return builder; + } + + @Override + protected int innerHashCode() { + return 0; + } + + @Override + protected boolean innerEquals(Object obj) { + return true; + } } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinParser.java index cd88cac990a..9b8261bd0bb 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinParser.java @@ -18,28 +18,48 @@ */ package org.elasticsearch.search.aggregations.metrics.min; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentParser.Token; import org.elasticsearch.search.aggregations.AggregatorFactory; -import org.elasticsearch.search.aggregations.metrics.NumericValuesSourceMetricsAggregatorParser; -import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; +import org.elasticsearch.search.aggregations.support.AbstractValuesSourceParser.NumericValuesSourceParser; +import org.elasticsearch.search.aggregations.support.ValueType; +import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; + +import java.io.IOException; +import java.util.Map; /** * */ -public class MinParser extends NumericValuesSourceMetricsAggregatorParser { +public class MinParser extends NumericValuesSourceParser { public MinParser() { - super(InternalMin.TYPE); + super(true, true, false); } @Override - protected AggregatorFactory createFactory(String aggregationName, ValuesSourceParser.Input input) { - return new MinAggregator.Factory(aggregationName, input); + public String type() { + return InternalMin.TYPE.name(); + } + + @Override + protected boolean token(String aggregationName, String currentFieldName, Token token, XContentParser parser, + ParseFieldMatcher parseFieldMatcher, Map otherOptions) throws IOException { + return false; + } + + @Override + protected ValuesSourceAggregatorFactory createFactory(String aggregationName, ValuesSourceType valuesSourceType, + ValueType targetValueType, Map otherOptions) { + return new MinAggregator.Factory(aggregationName); } - // NORELEASE implement this method when refactoring this aggregation @Override public AggregatorFactory getFactoryPrototype() { - return null; + return new MinAggregator.Factory(null); } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java b/core/src/test/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java index 70d70f2da82..6a7eb9d5229 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java @@ -119,6 +119,7 @@ public abstract class BaseAggregationTestCase exte } }, new IndexSettingsModule(index, settings), + new AbstractModule() { @Override protected void configure() { diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/MinTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/MinTests.java new file mode 100644 index 00000000000..b24cccafda4 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/MinTests.java @@ -0,0 +1,32 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.metrics; + +import org.elasticsearch.search.aggregations.metrics.min.MinAggregator; +import org.elasticsearch.search.aggregations.metrics.min.MinAggregator.Factory; + +public class MinTests extends AbstractNumericMetricTestCase { + + @Override + protected Factory doCreateTestAggregatorFactory() { + return new MinAggregator.Factory("foo"); + } + +}