From c33300c543de8be04b817f7cecd3c858d18c2546 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 5 Apr 2016 17:56:21 +0200 Subject: [PATCH] Make MappedFieldType responsible for providing a parser/formatter. #17546 Aggregations need to perform instanceof calls on MappedFieldType instances in order to know how they should be parsed or formatted. Instead, we should let the field types provide a formatter/parser that can can be used. --- .../common/io/stream/StreamInput.java | 8 + .../common/io/stream/StreamOutput.java | 5 + .../index/mapper/MappedFieldType.java | 14 + .../index/mapper/core/BooleanFieldMapper.java | 15 + .../index/mapper/core/DateFieldMapper.java | 13 + .../index/mapper/core/NumberFieldMapper.java | 15 + .../mapper/geo/BaseGeoPointFieldMapper.java | 15 + .../index/mapper/ip/IpFieldMapper.java | 14 + .../elasticsearch/indices/IndicesService.java | 11 +- .../elasticsearch/search/DocValueFormat.java | 340 ++++++++++++++++++ .../elasticsearch/search/SearchModule.java | 19 + .../bucket/BucketStreamContext.java | 17 +- .../AbstractHistogramAggregatorFactory.java | 6 +- .../bucket/histogram/ExtendedBounds.java | 11 +- .../bucket/histogram/HistogramAggregator.java | 6 +- .../histogram/InternalDateHistogram.java | 14 +- .../bucket/histogram/InternalHistogram.java | 76 ++-- .../bucket/range/InternalRange.java | 57 ++- .../bucket/range/RangeAggregator.java | 40 +-- .../bucket/range/date/InternalDateRange.java | 26 +- .../geodistance/InternalGeoDistance.java | 39 +- .../ipv4/IPv4RangeAggregatorBuilder.java | 8 +- .../bucket/range/ipv4/InternalIPv4Range.java | 18 +- .../significant/SignificantLongTerms.java | 42 +-- .../SignificantLongTermsAggregator.java | 10 +- .../bucket/terms/DoubleTerms.java | 39 +- .../bucket/terms/DoubleTermsAggregator.java | 8 +- .../bucket/terms/InternalTerms.java | 10 +- .../aggregations/bucket/terms/LongTerms.java | 41 ++- .../bucket/terms/LongTermsAggregator.java | 15 +- .../InternalNumericMetricsAggregation.java | 10 +- .../metrics/avg/AvgAggregator.java | 12 +- .../metrics/avg/AvgAggregatorFactory.java | 4 +- .../aggregations/metrics/avg/InternalAvg.java | 17 +- .../cardinality/CardinalityAggregator.java | 10 +- .../CardinalityAggregatorFactory.java | 4 +- .../cardinality/InternalCardinality.java | 14 +- .../aggregations/metrics/max/InternalMax.java | 17 +- .../metrics/max/MaxAggregator.java | 6 +- .../metrics/max/MaxAggregatorFactory.java | 4 +- .../aggregations/metrics/min/InternalMin.java | 17 +- .../metrics/min/MinAggregator.java | 12 +- .../metrics/min/MinAggregatorFactory.java | 4 +- .../hdr/AbstractHDRPercentilesAggregator.java | 8 +- .../hdr/AbstractInternalHDRPercentiles.java | 19 +- .../hdr/HDRPercentileRanksAggregator.java | 10 +- .../HDRPercentileRanksAggregatorFactory.java | 4 +- .../hdr/HDRPercentilesAggregator.java | 8 +- .../hdr/HDRPercentilesAggregatorFactory.java | 4 +- .../hdr/InternalHDRPercentileRanks.java | 6 +- .../hdr/InternalHDRPercentiles.java | 6 +- .../AbstractInternalTDigestPercentiles.java | 19 +- .../AbstractTDigestPercentilesAggregator.java | 6 +- .../InternalTDigestPercentileRanks.java | 6 +- .../tdigest/InternalTDigestPercentiles.java | 6 +- .../TDigestPercentileRanksAggregator.java | 4 +- ...igestPercentileRanksAggregatorFactory.java | 4 +- .../tdigest/TDigestPercentilesAggregator.java | 4 +- .../TDigestPercentilesAggregatorFactory.java | 4 +- .../metrics/stats/InternalStats.java | 23 +- .../metrics/stats/StatsAggregator.java | 12 +- .../metrics/stats/StatsAggregatorFactory.java | 4 +- .../extended/ExtendedStatsAggregator.java | 13 +- .../ExtendedStatsAggregatorFactory.java | 4 +- .../stats/extended/InternalExtendedStats.java | 12 +- .../aggregations/metrics/sum/InternalSum.java | 17 +- .../metrics/sum/SumAggregator.java | 12 +- .../metrics/sum/SumAggregatorFactory.java | 4 +- .../valuecount/InternalValueCount.java | 12 +- .../valuecount/ValueCountAggregator.java | 9 +- .../ValueCountAggregatorFactory.java | 4 +- .../pipeline/InternalSimpleValue.java | 15 +- .../BucketMetricsPipelineAggregator.java | 13 +- ...ucketMetricsPipelineAggregatorBuilder.java | 9 +- .../InternalBucketMetricValue.java | 15 +- .../avg/AvgBucketPipelineAggregator.java | 8 +- .../max/MaxBucketPipelineAggregator.java | 6 +- .../min/MinBucketPipelineAggregator.java | 6 +- .../percentile/InternalPercentilesBucket.java | 15 +- .../PercentilesBucketPipelineAggregator.java | 6 +- .../stats/InternalStatsBucket.java | 4 +- .../stats/StatsBucketPipelineAggregator.java | 6 +- ...ExtendedStatsBucketPipelineAggregator.java | 6 +- .../extended/InternalExtendedStatsBucket.java | 4 +- .../sum/SumBucketPipelineAggregator.java | 6 +- .../BucketScriptPipelineAggregator.java | 11 +- ...BucketScriptPipelineAggregatorBuilder.java | 9 +- .../CumulativeSumPipelineAggregator.java | 11 +- ...umulativeSumPipelineAggregatorBuilder.java | 9 +- .../DerivativePipelineAggregator.java | 11 +- .../DerivativePipelineAggregatorBuilder.java | 9 +- .../derivative/InternalDerivative.java | 8 +- .../movavg/MovAvgPipelineAggregator.java | 13 +- .../MovAvgPipelineAggregatorBuilder.java | 9 +- .../SerialDiffPipelineAggregator.java | 11 +- .../SerialDiffPipelineAggregatorBuilder.java | 9 +- .../support/AggregationContext.java | 18 +- .../aggregations/support/ValueType.java | 30 +- .../ValuesSourceAggregatorBuilder.java | 37 +- .../support/ValuesSourceConfig.java | 16 +- .../support/format/ValueFormat.java | 105 ------ .../support/format/ValueFormatter.java | 299 --------------- .../support/format/ValueFormatterStreams.java | 65 ---- .../support/format/ValueParser.java | 210 ----------- .../search/internal/SearchContext.java | 10 + .../common/io/stream/BytesStreamsTests.java | 5 + .../mapper/core/BooleanFieldTypeTests.java | 6 + .../index/mapper/core/DateFieldTypeTests.java | 18 +- .../search/DocValueFormatTests.java | 79 ++++ .../SignificantTermsSignificanceScoreIT.java | 2 +- .../SignificanceHeuristicTests.java | 18 +- .../hamcrest/ElasticsearchAssertions.java | 6 +- 112 files changed, 1146 insertions(+), 1334 deletions(-) create mode 100644 core/src/main/java/org/elasticsearch/search/DocValueFormat.java delete mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormat.java delete mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormatter.java delete mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormatterStreams.java delete mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueParser.java create mode 100644 core/src/test/java/org/elasticsearch/search/DocValueFormatTests.java diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 108c7343c8a..d817eba6fb7 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -37,6 +37,7 @@ import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.common.text.Text; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregatorBuilder; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder; import org.elasticsearch.search.rescore.RescoreBuilder; @@ -790,6 +791,13 @@ public abstract class StreamInput extends InputStream { return readNamedWriteable(Task.Status.class); } + /** + * Reads a {@link DocValueFormat} from the current stream. + */ + public DocValueFormat readValueFormat() throws IOException { + return readNamedWriteable(DocValueFormat.class); + } + /** * Reads a list of objects */ diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index b838e4f2a19..14cfecd5c19 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -36,6 +36,7 @@ import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.common.text.Text; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregatorBuilder; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder; import org.elasticsearch.search.rescore.RescoreBuilder; @@ -789,4 +790,8 @@ public abstract class StreamOutput extends OutputStream { writeNamedWriteable(sort); } + /** Writes a {@link DocValueFormat}. */ + public void writeValueFormat(DocValueFormat format) throws IOException { + writeNamedWriteable(format); + } } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index 42f77bb6c23..4b2930ca10a 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -47,6 +47,7 @@ import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; import org.elasticsearch.index.similarity.SimilarityProvider; +import org.elasticsearch.search.DocValueFormat; import org.joda.time.DateTimeZone; import java.io.IOException; @@ -458,4 +459,17 @@ public abstract class MappedFieldType extends FieldType { this.eagerGlobalOrdinals = eagerGlobalOrdinals; } + /** Return a {@link DocValueFormat} that can be used to display and parse + * values as returned by the fielddata API. + * The default implementation returns a {@link DocValueFormat#RAW}. */ + public DocValueFormat docValueFormat(@Nullable String format, DateTimeZone timeZone) { + if (format != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats"); + } + if (timeZone != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones"); + } + return DocValueFormat.RAW; + } + } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java index 02d6a536812..34381d70bc5 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java @@ -24,6 +24,7 @@ import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.Booleans; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; @@ -37,6 +38,8 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.ParseContext; +import org.elasticsearch.search.DocValueFormat; +import org.joda.time.DateTimeZone; import java.io.IOException; import java.util.Iterator; @@ -198,6 +201,18 @@ public class BooleanFieldMapper extends FieldMapper { failIfNoDocValues(); return new DocValuesIndexFieldData.Builder().numericType(NumericType.BOOLEAN); } + + @Override + public DocValueFormat docValueFormat(@Nullable String format, DateTimeZone timeZone) { + if (format != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats"); + } + if (timeZone != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + + "] does not support custom time zones"); + } + return DocValueFormat.BOOLEAN; + } } protected BooleanFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java index 17c8671c662..046ffb0d236 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java @@ -53,6 +53,7 @@ import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.core.LongFieldMapper.CustomLongNumericField; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.internal.SearchContext; import org.joda.time.DateTimeZone; @@ -496,6 +497,18 @@ public class DateFieldMapper extends NumberFieldMapper { failIfNoDocValues(); return new DocValuesIndexFieldData.Builder().numericType(NumericType.LONG); } + + @Override + public DocValueFormat docValueFormat(@Nullable String format, DateTimeZone timeZone) { + FormatDateTimeFormatter dateTimeFormatter = this.dateTimeFormatter; + if (format != null) { + dateTimeFormatter = Joda.forPattern(format); + } + if (timeZone == null) { + timeZone = DateTimeZone.UTC; + } + return new DocValueFormat.DateTime(dateTimeFormatter, timeZone); + } } protected DateFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, Explicit ignoreMalformed,Explicit coerce, diff --git a/core/src/main/java/org/elasticsearch/index/mapper/core/NumberFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/core/NumberFieldMapper.java index b73dfc59930..02d27d69a9d 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/core/NumberFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/core/NumberFieldMapper.java @@ -33,6 +33,7 @@ import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; import org.elasticsearch.Version; import org.elasticsearch.common.Explicit; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; @@ -48,6 +49,8 @@ import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.internal.AllFieldMapper; +import org.elasticsearch.search.DocValueFormat; +import org.joda.time.DateTimeZone; import java.io.IOException; import java.io.Reader; @@ -178,6 +181,18 @@ public abstract class NumberFieldMapper extends FieldMapper implements AllFieldM public boolean isNumeric() { return true; } + + @Override + public DocValueFormat docValueFormat(@Nullable String format, DateTimeZone timeZone) { + if (timeZone != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones"); + } + if (format == null) { + return DocValueFormat.RAW; + } else { + return new DocValueFormat.Decimal(format); + } + } } protected Boolean includeInAll; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/geo/BaseGeoPointFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/geo/BaseGeoPointFieldMapper.java index 4e97b3131cc..03c475e5d86 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/geo/BaseGeoPointFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/geo/BaseGeoPointFieldMapper.java @@ -25,6 +25,7 @@ import org.apache.lucene.util.LegacyNumericUtils; import org.elasticsearch.Version; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Explicit; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.Iterators; import org.elasticsearch.common.geo.GeoPoint; @@ -46,6 +47,8 @@ import org.elasticsearch.index.mapper.core.DoubleFieldMapper; import org.elasticsearch.index.mapper.core.KeywordFieldMapper; import org.elasticsearch.index.mapper.core.NumberFieldMapper; import org.elasticsearch.index.mapper.object.ArrayValueMapperParser; +import org.elasticsearch.search.DocValueFormat; +import org.joda.time.DateTimeZone; import java.io.IOException; import java.util.ArrayList; @@ -345,6 +348,18 @@ public abstract class BaseGeoPointFieldMapper extends FieldMapper implements Arr public IndexFieldData.Builder fielddataBuilder() { return new AbstractGeoPointDVIndexFieldData.Builder(); } + + @Override + public DocValueFormat docValueFormat(@Nullable String format, DateTimeZone timeZone) { + if (format != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats"); + } + if (timeZone != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + + "] does not support custom time zones"); + } + return DocValueFormat.GEOHASH; + } } protected DoubleFieldMapper latMapper; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java index b2b82529551..0bbfdda1b8f 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java @@ -55,7 +55,9 @@ import org.elasticsearch.index.mapper.core.LongFieldMapper; import org.elasticsearch.index.mapper.core.LongFieldMapper.CustomLongNumericField; import org.elasticsearch.index.mapper.core.NumberFieldMapper; import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.bucket.range.ipv4.InternalIPv4Range; +import org.joda.time.DateTimeZone; import java.io.IOException; import java.util.Iterator; @@ -285,6 +287,18 @@ public class IpFieldMapper extends NumberFieldMapper { failIfNoDocValues(); return new DocValuesIndexFieldData.Builder().numericType(NumericType.LONG); } + + @Override + public DocValueFormat docValueFormat(@Nullable String format, DateTimeZone timeZone) { + if (format != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats"); + } + if (timeZone != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + + "] does not support custom time zones"); + } + return DocValueFormat.IP; + } } protected IpFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesService.java b/core/src/main/java/org/elasticsearch/indices/IndicesService.java index 3b149cea655..9a1aed286cc 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -44,6 +44,9 @@ import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.lease.Releasable; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.settings.ClusterSettings; @@ -141,6 +144,7 @@ public class IndicesService extends AbstractLifecycleComponent i private final OldShardsStats oldShardsStats = new OldShardsStats(); private final IndexStoreConfig indexStoreConfig; private final MapperRegistry mapperRegistry; + private final NamedWriteableRegistry namedWriteableRegistry; private final IndexingMemoryController indexingMemoryController; private final TimeValue cleanInterval; private final IndicesRequestCache indicesRequestCache; @@ -156,7 +160,8 @@ public class IndicesService extends AbstractLifecycleComponent i public IndicesService(Settings settings, PluginsService pluginsService, NodeEnvironment nodeEnv, ClusterSettings clusterSettings, AnalysisRegistry analysisRegistry, IndicesQueriesRegistry indicesQueriesRegistry, IndexNameExpressionResolver indexNameExpressionResolver, - ClusterService clusterService, MapperRegistry mapperRegistry, ThreadPool threadPool, IndexScopedSettings indexScopedSettings, CircuitBreakerService circuitBreakerService) { + ClusterService clusterService, MapperRegistry mapperRegistry, NamedWriteableRegistry namedWriteableRegistry, + ThreadPool threadPool, IndexScopedSettings indexScopedSettings, CircuitBreakerService circuitBreakerService) { super(settings); this.threadPool = threadPool; this.pluginsService = pluginsService; @@ -170,6 +175,7 @@ public class IndicesService extends AbstractLifecycleComponent i this.indicesRequestCache = new IndicesRequestCache(settings); this.indicesQueryCache = new IndicesQueryCache(settings); this.mapperRegistry = mapperRegistry; + this.namedWriteableRegistry = namedWriteableRegistry; clusterSettings.addSettingsUpdateConsumer(IndexStoreConfig.INDICES_STORE_THROTTLE_TYPE_SETTING, indexStoreConfig::setRateLimitingType); clusterSettings.addSettingsUpdateConsumer(IndexStoreConfig.INDICES_STORE_THROTTLE_MAX_BYTES_PER_SEC_SETTING, indexStoreConfig::setRateLimitingThrottle); indexingMemoryController = new IndexingMemoryController(settings, threadPool, Iterables.flatten(this)); @@ -981,7 +987,8 @@ public class IndicesService extends AbstractLifecycleComponent i if (entity.loaded == false) { // if we have loaded this we don't need to do anything // restore the cached query result into the context final QuerySearchResult result = context.queryResult(); - result.readFromWithId(context.id(), bytesReference.streamInput()); + StreamInput in = new NamedWriteableAwareStreamInput(bytesReference.streamInput(), namedWriteableRegistry); + result.readFromWithId(context.id(), in); result.shardTarget(context.shardTarget()); } } diff --git a/core/src/main/java/org/elasticsearch/search/DocValueFormat.java b/core/src/main/java/org/elasticsearch/search/DocValueFormat.java new file mode 100644 index 00000000000..f1746a9c55f --- /dev/null +++ b/core/src/main/java/org/elasticsearch/search/DocValueFormat.java @@ -0,0 +1,340 @@ +/* + * 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; + +import org.apache.lucene.index.Term; +import org.apache.lucene.spatial.util.GeoHashUtils; +import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.io.stream.NamedWriteable; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.joda.DateMathParser; +import org.elasticsearch.common.joda.FormatDateTimeFormatter; +import org.elasticsearch.common.joda.Joda; +import org.elasticsearch.index.mapper.ip.IpFieldMapper; +import org.joda.time.DateTimeZone; + +import java.io.IOException; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.Locale; +import java.util.Objects; +import java.util.concurrent.Callable; + +/** A formatter for values as returned by the fielddata/doc-values APIs. */ +public interface DocValueFormat extends NamedWriteable { + + String format(long value); + + String format(double value); + + String format(BytesRef value); + + long parseLong(String value, boolean roundUp, Callable now); + + double parseDouble(String value, boolean roundUp, Callable now); + + public static final DocValueFormat RAW = new DocValueFormat() { + + @Override + public String getWriteableName() { + return "raw"; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + } + + @Override + public String format(long value) { + return Long.toString(value); + } + + @Override + public String format(double value) { + return Double.toString(value); + } + + @Override + public String format(BytesRef value) { + return Term.toString(value); + } + + @Override + public long parseLong(String value, boolean roundUp, Callable now) { + double d = Double.parseDouble(value); + if (roundUp) { + d = Math.ceil(d); + } else { + d = Math.floor(d); + } + return Math.round(d); + } + + @Override + public double parseDouble(String value, boolean roundUp, Callable now) { + return Double.parseDouble(value); + } + }; + + public static final class DateTime implements DocValueFormat { + + public static final String NAME = "date_time"; + + final FormatDateTimeFormatter formatter; + final DateTimeZone timeZone; + private final DateMathParser parser; + + public DateTime(FormatDateTimeFormatter formatter, DateTimeZone timeZone) { + this.formatter = Objects.requireNonNull(formatter); + this.timeZone = Objects.requireNonNull(timeZone); + this.parser = new DateMathParser(formatter); + } + + public DateTime(StreamInput in) throws IOException { + this(Joda.forPattern(in.readString()), DateTimeZone.forID(in.readString())); + } + + @Override + public String getWriteableName() { + return NAME; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeString(formatter.format()); + out.writeString(timeZone.getID()); + } + + @Override + public String format(long value) { + return formatter.printer().withZone(timeZone).print(value); + } + + @Override + public String format(double value) { + return format((long) value); + } + + @Override + public String format(BytesRef value) { + throw new UnsupportedOperationException(); + } + + @Override + public long parseLong(String value, boolean roundUp, Callable now) { + return parser.parse(value, now, roundUp, timeZone); + } + + @Override + public double parseDouble(String value, boolean roundUp, Callable now) { + return parseLong(value, roundUp, now); + } + } + + public static final DocValueFormat GEOHASH = new DocValueFormat() { + + @Override + public String getWriteableName() { + return "geo_hash"; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + } + + @Override + public String format(long value) { + return GeoHashUtils.stringEncode(value); + } + + @Override + public String format(double value) { + return format((long) value); + } + + @Override + public String format(BytesRef value) { + throw new UnsupportedOperationException(); + } + + @Override + public long parseLong(String value, boolean roundUp, Callable now) { + throw new UnsupportedOperationException(); + } + + @Override + public double parseDouble(String value, boolean roundUp, Callable now) { + throw new UnsupportedOperationException(); + } + }; + + public static final DocValueFormat BOOLEAN = new DocValueFormat() { + + @Override + public String getWriteableName() { + return "bool"; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + } + + @Override + public String format(long value) { + return java.lang.Boolean.valueOf(value != 0).toString(); + } + + @Override + public String format(double value) { + return java.lang.Boolean.valueOf(value != 0).toString(); + } + + @Override + public String format(BytesRef value) { + throw new UnsupportedOperationException(); + } + + @Override + public long parseLong(String value, boolean roundUp, Callable now) { + throw new UnsupportedOperationException(); + } + + @Override + public double parseDouble(String value, boolean roundUp, Callable now) { + throw new UnsupportedOperationException(); + } + }; + + public static final DocValueFormat IP = new DocValueFormat() { + + @Override + public String getWriteableName() { + return "ip"; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + } + + @Override + public String format(long value) { + return IpFieldMapper.longToIp(value); + } + + @Override + public String format(double value) { + return format((long) value); + } + + @Override + public String format(BytesRef value) { + throw new UnsupportedOperationException(); + } + + @Override + public long parseLong(String value, boolean roundUp, Callable now) { + return IpFieldMapper.ipToLong(value); + } + + @Override + public double parseDouble(String value, boolean roundUp, Callable now) { + return parseLong(value, roundUp, now); + } + }; + + public static final class Decimal implements DocValueFormat { + + public static final String NAME = "decimal"; + private static final DecimalFormatSymbols SYMBOLS = new DecimalFormatSymbols(Locale.ROOT); + + final String pattern; + private final NumberFormat format; + + public Decimal(String pattern) { + this.pattern = pattern; + this.format = new DecimalFormat(pattern, SYMBOLS); + } + + public Decimal(StreamInput in) throws IOException { + this(in.readString()); + } + + @Override + public String getWriteableName() { + return NAME; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeString(pattern); + } + + @Override + public String format(long value) { + return format.format(value); + } + + @Override + public String format(double value) { + return format.format(value); + } + + @Override + public String format(BytesRef value) { + throw new UnsupportedOperationException(); + } + + @Override + public long parseLong(String value, boolean roundUp, Callable now) { + Number n; + try { + n = format.parse(value); + } catch (ParseException e) { + throw new RuntimeException(e); + } + if (format.isParseIntegerOnly()) { + return n.longValue(); + } else { + double d = n.doubleValue(); + if (roundUp) { + d = Math.ceil(d); + } else { + d = Math.floor(d); + } + return Math.round(d); + } + } + + @Override + public double parseDouble(String value, boolean roundUp, Callable now) { + Number n; + try { + n = format.parse(value); + } catch (ParseException e) { + throw new RuntimeException(e); + } + return n.doubleValue(); + } + + } +} diff --git a/core/src/main/java/org/elasticsearch/search/SearchModule.java b/core/src/main/java/org/elasticsearch/search/SearchModule.java index de6f3611f05..de9490d5da4 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/core/src/main/java/org/elasticsearch/search/SearchModule.java @@ -276,6 +276,7 @@ public class SearchModule extends AbstractModule { registerBuiltinQueryParsers(); registerBuiltinRescorers(); registerBuiltinSorts(); + registerBuiltinValueFormats(); } public void registerHighlighter(String key, Class clazz) { @@ -314,6 +315,15 @@ public class SearchModule extends AbstractModule { return scoreFunctionsRegistry; } + /** + * Register a new ValueFormat. + */ + // private for now, we can consider making it public if there are actual use cases for plugins + // to register custom value formats + private void registerValueFormat(String name, Writeable.Reader reader) { + namedWriteableRegistry.register(DocValueFormat.class, name, reader); + } + /** * Register a query. * @@ -528,6 +538,15 @@ public class SearchModule extends AbstractModule { namedWriteableRegistry.register(ScoreFunctionBuilder.class, WeightBuilder.NAME, WeightBuilder::new); } + private void registerBuiltinValueFormats() { + registerValueFormat(DocValueFormat.BOOLEAN.getWriteableName(), in -> DocValueFormat.BOOLEAN); + registerValueFormat(DocValueFormat.DateTime.NAME, DocValueFormat.DateTime::new); + registerValueFormat(DocValueFormat.Decimal.NAME, DocValueFormat.Decimal::new); + registerValueFormat(DocValueFormat.GEOHASH.getWriteableName(), in -> DocValueFormat.GEOHASH); + registerValueFormat(DocValueFormat.IP.getWriteableName(), in -> DocValueFormat.IP); + registerValueFormat(DocValueFormat.RAW.getWriteableName(), in -> DocValueFormat.RAW); + } + private void registerBuiltinQueryParsers() { registerQuery(MatchQueryBuilder.PROTOTYPE::readFrom, MatchQueryBuilder::fromXContent, MatchQueryBuilder.QUERY_NAME_FIELD); registerQuery(MatchPhraseQueryBuilder.PROTOTYPE::readFrom, MatchPhraseQueryBuilder::fromXContent, diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketStreamContext.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketStreamContext.java index 1e3676f9ea9..794383e8d70 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketStreamContext.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketStreamContext.java @@ -22,27 +22,26 @@ package org.elasticsearch.search.aggregations.bucket; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; +import org.elasticsearch.search.DocValueFormat; import java.io.IOException; import java.util.Map; public class BucketStreamContext implements Streamable { - private ValueFormatter formatter; + private DocValueFormat format; private boolean keyed; private Map attributes; public BucketStreamContext() { } - public void formatter(ValueFormatter formatter) { - this.formatter = formatter; + public void format(DocValueFormat format) { + this.format = format; } - public ValueFormatter formatter() { - return formatter; + public DocValueFormat format() { + return format; } public void keyed(boolean keyed) { @@ -63,14 +62,14 @@ public class BucketStreamContext implements Streamable { @Override public void readFrom(StreamInput in) throws IOException { - formatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); keyed = in.readBoolean(); attributes = in.readMap(); } @Override public void writeTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(format); out.writeBoolean(keyed); out.writeMap(attributes); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramAggregatorFactory.java index a53b31aa715..a9c3d24a429 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramAggregatorFactory.java @@ -70,7 +70,7 @@ public abstract class AbstractHistogramAggregatorFactory pipelineAggregators, Map metaData) throws IOException { Rounding rounding = createRounding(); - return new HistogramAggregator(name, factories, rounding, order, keyed, minDocCount, extendedBounds, null, config.formatter(), + return new HistogramAggregator(name, factories, rounding, order, keyed, minDocCount, extendedBounds, null, config.format(), histogramFactory, context, parent, pipelineAggregators, metaData); } @@ -101,11 +101,11 @@ public abstract class AbstractHistogramAggregatorFactory 0) { throw new SearchParseException(context, "[extended_bounds.min][" + min + "] cannot be greater than " + diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregator.java index b471e11d25c..ca797b06049 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregator.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.inject.internal.Nullable; import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.rounding.Rounding; import org.elasticsearch.common.util.LongHash; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -35,7 +36,6 @@ import org.elasticsearch.search.aggregations.bucket.BucketsAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.ArrayList; @@ -50,7 +50,7 @@ public class HistogramAggregator extends BucketsAggregator { public static final ParseField MIN_DOC_COUNT_FIELD = new ParseField("min_doc_count"); private final ValuesSource.Numeric valuesSource; - private final ValueFormatter formatter; + private final DocValueFormat formatter; private final Rounding rounding; private final InternalOrder order; private final boolean keyed; @@ -63,7 +63,7 @@ public class HistogramAggregator extends BucketsAggregator { public HistogramAggregator(String name, AggregatorFactories factories, Rounding rounding, InternalOrder order, boolean keyed, long minDocCount, @Nullable ExtendedBounds extendedBounds, @Nullable ValuesSource.Numeric valuesSource, - ValueFormatter formatter, InternalHistogram.Factory histogramFactory, AggregationContext aggregationContext, + DocValueFormat formatter, InternalHistogram.Factory histogramFactory, AggregationContext aggregationContext, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { super(name, factories, aggregationContext, parent, pipelineAggregators, metaData); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalDateHistogram.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalDateHistogram.java index 9808eed625d..84cc98515dd 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalDateHistogram.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalDateHistogram.java @@ -18,11 +18,11 @@ */ package org.elasticsearch.search.aggregations.bucket.histogram; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.support.ValueType; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -36,18 +36,18 @@ public class InternalDateHistogram { static class Bucket extends InternalHistogram.Bucket { - Bucket(boolean keyed, ValueFormatter formatter, InternalHistogram.Factory factory) { + Bucket(boolean keyed, DocValueFormat formatter, InternalHistogram.Factory factory) { super(keyed, formatter, factory); } - Bucket(long key, long docCount, InternalAggregations aggregations, boolean keyed, ValueFormatter formatter, + Bucket(long key, long docCount, InternalAggregations aggregations, boolean keyed, DocValueFormat formatter, InternalHistogram.Factory factory) { super(key, docCount, keyed, formatter, factory, aggregations); } @Override public String getKeyAsString() { - return formatter != null ? formatter.format(key) : ValueFormatter.DateTime.DEFAULT.format(key); + return format.format(key); } @Override @@ -78,12 +78,12 @@ public class InternalDateHistogram { @Override public InternalDateHistogram.Bucket createBucket(InternalAggregations aggregations, InternalDateHistogram.Bucket prototype) { - return new Bucket(prototype.key, prototype.docCount, aggregations, prototype.getKeyed(), prototype.formatter, this); + return new Bucket(prototype.key, prototype.docCount, aggregations, prototype.getKeyed(), prototype.format, this); } @Override public InternalDateHistogram.Bucket createBucket(Object key, long docCount, InternalAggregations aggregations, boolean keyed, - ValueFormatter formatter) { + DocValueFormat formatter) { if (key instanceof Number) { return new Bucket(((Number) key).longValue(), docCount, aggregations, keyed, formatter, this); } else if (key instanceof DateTime) { @@ -94,7 +94,7 @@ public class InternalDateHistogram { } @Override - protected InternalDateHistogram.Bucket createEmptyBucket(boolean keyed, ValueFormatter formatter) { + protected InternalDateHistogram.Bucket createEmptyBucket(boolean keyed, DocValueFormat formatter) { return new Bucket(keyed, formatter, this); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java index 8c51b2403a3..e7d3f47af8e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java @@ -23,8 +23,8 @@ import org.apache.lucene.util.PriorityQueue; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.rounding.Rounding; -import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.Aggregations; @@ -35,8 +35,6 @@ import org.elasticsearch.search.aggregations.bucket.BucketStreamContext; import org.elasticsearch.search.aggregations.bucket.BucketStreams; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.ValueType; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.ArrayList; @@ -73,7 +71,7 @@ public class InternalHistogram extends Inter if (factory == null) { throw new IllegalStateException("No factory found for histogram buckets"); } - Bucket histogram = new Bucket(context.keyed(), context.formatter(), factory); + Bucket histogram = new Bucket(context.keyed(), context.format(), factory); histogram.readFrom(in); return histogram; } @@ -81,7 +79,7 @@ public class InternalHistogram extends Inter @Override public BucketStreamContext getBucketStreamContext(Bucket bucket) { BucketStreamContext context = new BucketStreamContext(); - context.formatter(bucket.formatter); + context.format(bucket.format); context.keyed(bucket.keyed); return context; } @@ -99,16 +97,16 @@ public class InternalHistogram extends Inter long docCount; InternalAggregations aggregations; private transient final boolean keyed; - protected transient final ValueFormatter formatter; + protected transient final DocValueFormat format; private Factory factory; - public Bucket(boolean keyed, ValueFormatter formatter, Factory factory) { - this.formatter = formatter; + public Bucket(boolean keyed, DocValueFormat formatter, Factory factory) { + this.format = formatter; this.keyed = keyed; this.factory = factory; } - public Bucket(long key, long docCount, boolean keyed, ValueFormatter formatter, Factory factory, + public Bucket(long key, long docCount, boolean keyed, DocValueFormat formatter, Factory factory, InternalAggregations aggregations) { this(keyed, formatter, factory); this.key = key; @@ -122,7 +120,7 @@ public class InternalHistogram extends Inter @Override public String getKeyAsString() { - return formatter != null ? formatter.format(key) : ValueFormatter.RAW.format(key); + return format.format(key); } @Override @@ -149,25 +147,19 @@ public class InternalHistogram extends Inter aggregations.add((InternalAggregations) bucket.getAggregations()); } InternalAggregations aggs = InternalAggregations.reduce(aggregations, context); - return (B) getFactory().createBucket(key, docCount, aggs, keyed, formatter); + return (B) getFactory().createBucket(key, docCount, aggs, keyed, format); } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - if (formatter != ValueFormatter.RAW) { - Text keyTxt = new Text(formatter.format(key)); - if (keyed) { - builder.startObject(keyTxt.string()); - } else { - builder.startObject(); - } - builder.field(CommonFields.KEY_AS_STRING, keyTxt); + String keyAsString = format.format(key); + if (keyed) { + builder.startObject(keyAsString); } else { - if (keyed) { - builder.startObject(String.valueOf(getKey())); - } else { - builder.startObject(); - } + builder.startObject(); + } + if (format != DocValueFormat.RAW) { + builder.field(CommonFields.KEY_AS_STRING, keyAsString); } builder.field(CommonFields.KEY, key); builder.field(CommonFields.DOC_COUNT, docCount); @@ -190,8 +182,8 @@ public class InternalHistogram extends Inter aggregations.writeTo(out); } - public ValueFormatter getFormatter() { - return formatter; + public DocValueFormat getFormatter() { + return format; } public boolean getKeyed() { @@ -249,7 +241,7 @@ public class InternalHistogram extends Inter } public InternalHistogram create(String name, List buckets, InternalOrder order, long minDocCount, - EmptyBucketInfo emptyBucketInfo, ValueFormatter formatter, boolean keyed, + EmptyBucketInfo emptyBucketInfo, DocValueFormat formatter, boolean keyed, List pipelineAggregators, Map metaData) { return new InternalHistogram<>(name, buckets, order, minDocCount, emptyBucketInfo, formatter, keyed, this, pipelineAggregators, @@ -258,16 +250,16 @@ public class InternalHistogram extends Inter public InternalHistogram create(List buckets, InternalHistogram prototype) { return new InternalHistogram<>(prototype.name, buckets, prototype.order, prototype.minDocCount, prototype.emptyBucketInfo, - prototype.formatter, prototype.keyed, this, prototype.pipelineAggregators(), prototype.metaData); + prototype.format, prototype.keyed, this, prototype.pipelineAggregators(), prototype.metaData); } @SuppressWarnings("unchecked") public B createBucket(InternalAggregations aggregations, B prototype) { - return (B) new Bucket(prototype.key, prototype.docCount, prototype.getKeyed(), prototype.formatter, this, aggregations); + return (B) new Bucket(prototype.key, prototype.docCount, prototype.getKeyed(), prototype.format, this, aggregations); } @SuppressWarnings("unchecked") - public B createBucket(Object key, long docCount, InternalAggregations aggregations, boolean keyed, ValueFormatter formatter) { + public B createBucket(Object key, long docCount, InternalAggregations aggregations, boolean keyed, DocValueFormat formatter) { if (key instanceof Number) { return (B) new Bucket(((Number) key).longValue(), docCount, keyed, formatter, this, aggregations); } else { @@ -276,7 +268,7 @@ public class InternalHistogram extends Inter } @SuppressWarnings("unchecked") - protected B createEmptyBucket(boolean keyed, ValueFormatter formatter) { + protected B createEmptyBucket(boolean keyed, DocValueFormat formatter) { return (B) new Bucket(keyed, formatter, this); } @@ -284,7 +276,7 @@ public class InternalHistogram extends Inter protected List buckets; private InternalOrder order; - private ValueFormatter formatter; + private DocValueFormat format; private boolean keyed; private long minDocCount; private EmptyBucketInfo emptyBucketInfo; @@ -293,7 +285,7 @@ public class InternalHistogram extends Inter InternalHistogram() {} // for serialization InternalHistogram(String name, List buckets, InternalOrder order, long minDocCount, EmptyBucketInfo emptyBucketInfo, - ValueFormatter formatter, boolean keyed, Factory factory, List pipelineAggregators, + DocValueFormat formatter, boolean keyed, Factory factory, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.buckets = buckets; @@ -301,7 +293,7 @@ public class InternalHistogram extends Inter assert (minDocCount == 0) == (emptyBucketInfo != null); this.minDocCount = minDocCount; this.emptyBucketInfo = emptyBucketInfo; - this.formatter = formatter; + this.format = formatter; this.keyed = keyed; this.factory = factory; } @@ -421,7 +413,7 @@ public class InternalHistogram extends Inter while (key <= max) { iter.add(getFactory().createBucket(key, 0, reducedEmptySubAggs, - keyed, formatter)); + keyed, format)); key = emptyBucketInfo.rounding.nextRoundingValue(key); } } @@ -432,7 +424,7 @@ public class InternalHistogram extends Inter while (key < firstBucket.key) { iter.add(getFactory().createBucket(key, 0, reducedEmptySubAggs, - keyed, formatter)); + keyed, format)); key = emptyBucketInfo.rounding.nextRoundingValue(key); } } @@ -449,7 +441,7 @@ public class InternalHistogram extends Inter while (key < nextBucket.key) { iter.add(getFactory().createBucket(key, 0, reducedEmptySubAggs, keyed, - formatter)); + format)); key = emptyBucketInfo.rounding.nextRoundingValue(key); } assert key == nextBucket.key; @@ -464,7 +456,7 @@ public class InternalHistogram extends Inter while (key <= max) { iter.add(getFactory().createBucket(key, 0, reducedEmptySubAggs, keyed, - formatter)); + format)); key = emptyBucketInfo.rounding.nextRoundingValue(key); } } @@ -493,7 +485,7 @@ public class InternalHistogram extends Inter CollectionUtil.introSort(reducedBuckets, order.comparator()); } - return getFactory().create(getName(), reducedBuckets, order, minDocCount, emptyBucketInfo, formatter, keyed, pipelineAggregators(), + return getFactory().create(getName(), reducedBuckets, order, minDocCount, emptyBucketInfo, format, keyed, pipelineAggregators(), getMetaData()); } @@ -505,12 +497,12 @@ public class InternalHistogram extends Inter if (minDocCount == 0) { emptyBucketInfo = EmptyBucketInfo.readFrom(in); } - formatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); keyed = in.readBoolean(); int size = in.readVInt(); List buckets = new ArrayList<>(size); for (int i = 0; i < size; i++) { - B bucket = getFactory().createEmptyBucket(keyed, formatter); + B bucket = getFactory().createEmptyBucket(keyed, format); bucket.readFrom(in); buckets.add(bucket); } @@ -536,7 +528,7 @@ public class InternalHistogram extends Inter if (minDocCount == 0) { EmptyBucketInfo.writeTo(emptyBucketInfo, out); } - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(format); out.writeBoolean(keyed); out.writeVInt(buckets.size()); for (B bucket : buckets) { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java index c38334a80f2..ce4e0ba60f1 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.bucket.range; 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.Aggregations; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -31,8 +32,6 @@ import org.elasticsearch.search.aggregations.bucket.BucketStreams; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSourceType; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.ArrayList; @@ -61,7 +60,7 @@ public class InternalRange BUCKET_STREAM = new BucketStreams.Stream() { @Override public Bucket readResult(StreamInput in, BucketStreamContext context) throws IOException { - Bucket buckets = new Bucket(context.keyed(), context.formatter()); + Bucket buckets = new Bucket(context.keyed(), context.format()); buckets.readFrom(in); return buckets; } @@ -69,7 +68,7 @@ public class InternalRange ranges, ValueFormatter formatter, boolean keyed, List pipelineAggregators, + public R create(String name, List ranges, DocValueFormat formatter, boolean keyed, List pipelineAggregators, Map metaData) { return (R) new InternalRange<>(name, ranges, formatter, keyed, pipelineAggregators, metaData); } @SuppressWarnings("unchecked") public B createBucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed, - ValueFormatter formatter) { + DocValueFormat formatter) { return (B) new Bucket(key, from, to, docCount, aggregations, keyed, formatter); } @SuppressWarnings("unchecked") public R create(List ranges, R prototype) { - return (R) new InternalRange<>(prototype.name, ranges, prototype.formatter, prototype.keyed, prototype.pipelineAggregators(), + return (R) new InternalRange<>(prototype.name, ranges, prototype.format, prototype.keyed, prototype.pipelineAggregators(), prototype.metaData); } @SuppressWarnings("unchecked") public B createBucket(InternalAggregations aggregations, B prototype) { return (B) new Bucket(prototype.getKey(), prototype.from, prototype.to, prototype.getDocCount(), aggregations, prototype.keyed, - prototype.formatter); + prototype.format); } } private List ranges; private Map rangeMap; - protected ValueFormatter formatter; + protected DocValueFormat format; protected boolean keyed; public InternalRange() {} // for serialization - public InternalRange(String name, List ranges, ValueFormatter formatter, boolean keyed, + public InternalRange(String name, List ranges, DocValueFormat format, boolean keyed, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.ranges = ranges; - this.formatter = formatter; + this.format = format; this.keyed = keyed; } @@ -320,18 +319,18 @@ public class InternalRange ranges = new ArrayList<>(size); for (int i = 0; i < size; i++) { String key = in.readOptionalString(); - ranges.add(getFactory().createBucket(key, in.readDouble(), in.readDouble(), in.readVLong(), InternalAggregations.readAggregations(in), keyed, formatter)); + ranges.add(getFactory().createBucket(key, in.readDouble(), in.readDouble(), in.readVLong(), InternalAggregations.readAggregations(in), keyed, format)); } this.ranges = ranges; this.rangeMap = null; @@ -339,7 +338,7 @@ public class InternalRange ranges, boolean keyed, AggregationContext aggregationContext, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { super(name, factories, aggregationContext, parent, pipelineAggregators, metaData); assert valuesSource != null; this.valuesSource = valuesSource; - this.formatter = format.formatter(); + this.format = format; this.keyed = keyed; this.rangeFactory = rangeFactory; this.ranges = new Range[ranges.size()]; - ValueParser parser = format != null ? format.parser() : ValueParser.RAW; for (int i = 0; i < this.ranges.length; i++) { - this.ranges[i] = ranges.get(i).process(parser, context.searchContext()); + this.ranges[i] = ranges.get(i).process(format, context.searchContext()); } sortRanges(this.ranges); @@ -320,11 +317,11 @@ public class RangeAggregator extends BucketsAggregator { Range range = ranges[i]; final long bucketOrd = subBucketOrdinal(owningBucketOrdinal, i); org.elasticsearch.search.aggregations.bucket.range.Range.Bucket bucket = - rangeFactory.createBucket(range.key, range.from, range.to, bucketDocCount(bucketOrd), bucketAggregations(bucketOrd), keyed, formatter); + rangeFactory.createBucket(range.key, range.from, range.to, bucketDocCount(bucketOrd), bucketAggregations(bucketOrd), keyed, format); buckets.add(bucket); } // value source can be null in the case of unmapped fields - return rangeFactory.create(name, buckets, formatter, keyed, pipelineAggregators(), metaData()); + return rangeFactory.create(name, buckets, format, keyed, pipelineAggregators(), metaData()); } @Override @@ -334,11 +331,11 @@ public class RangeAggregator extends BucketsAggregator { for (int i = 0; i < ranges.length; i++) { Range range = ranges[i]; org.elasticsearch.search.aggregations.bucket.range.Range.Bucket bucket = - rangeFactory.createBucket(range.key, range.from, range.to, 0, subAggs, keyed, formatter); + rangeFactory.createBucket(range.key, range.from, range.to, 0, subAggs, keyed, format); buckets.add(bucket); } // value source can be null in the case of unmapped fields - return rangeFactory.create(name, buckets, formatter, keyed, pipelineAggregators(), metaData()); + return rangeFactory.create(name, buckets, format, keyed, pipelineAggregators(), metaData()); } private static final void sortRanges(final Range[] ranges) { @@ -367,22 +364,21 @@ public class RangeAggregator extends BucketsAggregator { private final List ranges; private final boolean keyed; private final InternalRange.Factory factory; - private final ValueFormatter formatter; + private final DocValueFormat format; @SuppressWarnings("unchecked") - public Unmapped(String name, List ranges, boolean keyed, ValueFormat format, + public Unmapped(String name, List ranges, boolean keyed, DocValueFormat format, AggregationContext context, Aggregator parent, InternalRange.Factory factory, List pipelineAggregators, Map metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); this.ranges = new ArrayList<>(); - ValueParser parser = format != null ? format.parser() : ValueParser.RAW; for (R range : ranges) { - this.ranges.add((R) range.process(parser, context.searchContext())); + this.ranges.add((R) range.process(format, context.searchContext())); } this.keyed = keyed; - this.formatter = format.formatter(); + this.format = format; this.factory = factory; } @@ -391,9 +387,9 @@ public class RangeAggregator extends BucketsAggregator { InternalAggregations subAggs = buildEmptySubAggregations(); List buckets = new ArrayList<>(ranges.size()); for (RangeAggregator.Range range : ranges) { - buckets.add(factory.createBucket(range.key, range.from, range.to, 0, subAggs, keyed, formatter)); + buckets.add(factory.createBucket(range.key, range.from, range.to, 0, subAggs, keyed, format)); } - return factory.create(name, buckets, formatter, keyed, pipelineAggregators(), metaData()); + return factory.create(name, buckets, format, keyed, pipelineAggregators(), metaData()); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/date/InternalDateRange.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/date/InternalDateRange.java index 88568bcd005..80411c59045 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/date/InternalDateRange.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/date/InternalDateRange.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.aggregations.bucket.range.date; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregations; @@ -27,7 +28,6 @@ import org.elasticsearch.search.aggregations.bucket.BucketStreams; import org.elasticsearch.search.aggregations.bucket.range.InternalRange; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.ValueType; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -54,7 +54,7 @@ public class InternalDateRange extends InternalRange BUCKET_STREAM = new BucketStreams.Stream() { @Override public Bucket readResult(StreamInput in, BucketStreamContext context) throws IOException { - Bucket buckets = new Bucket(context.keyed(), context.formatter()); + Bucket buckets = new Bucket(context.keyed(), context.format()); buckets.readFrom(in); return buckets; } @@ -62,7 +62,7 @@ public class InternalDateRange extends InternalRange aggregations, boolean keyed, ValueFormatter formatter) { + public Bucket(String key, double from, double to, long docCount, List aggregations, boolean keyed, DocValueFormat formatter) { super(key, from, to, docCount, new InternalAggregations(aggregations), keyed, formatter); } - public Bucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed, ValueFormatter formatter) { + public Bucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed, DocValueFormat formatter) { super(key, from, to, docCount, aggregations, keyed, formatter); } @@ -108,8 +108,8 @@ public class InternalDateRange extends InternalRange ranges, ValueFormatter formatter, boolean keyed, + public InternalDateRange create(String name, List ranges, DocValueFormat formatter, boolean keyed, List pipelineAggregators, Map metaData) { return new InternalDateRange(name, ranges, formatter, keyed, pipelineAggregators, metaData); } @Override public InternalDateRange create(List ranges, InternalDateRange prototype) { - return new InternalDateRange(prototype.name, ranges, prototype.formatter, prototype.keyed, prototype.pipelineAggregators(), + return new InternalDateRange(prototype.name, ranges, prototype.format, prototype.keyed, prototype.pipelineAggregators(), prototype.metaData); } @Override - public Bucket createBucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed, ValueFormatter formatter) { + public Bucket createBucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed, DocValueFormat formatter) { return new Bucket(key, from, to, docCount, aggregations, keyed, formatter); } @Override public Bucket createBucket(InternalAggregations aggregations, Bucket prototype) { return new Bucket(prototype.getKey(), ((Number) prototype.getFrom()).doubleValue(), ((Number) prototype.getTo()).doubleValue(), - prototype.getDocCount(), aggregations, prototype.getKeyed(), prototype.getFormatter()); + prototype.getDocCount(), aggregations, prototype.getKeyed(), prototype.getFormat()); } } InternalDateRange() {} // for serialization - InternalDateRange(String name, List ranges, ValueFormatter formatter, boolean keyed, + InternalDateRange(String name, List ranges, DocValueFormat formatter, boolean keyed, List pipelineAggregators, Map metaData) { super(name, ranges, formatter, keyed, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/InternalGeoDistance.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/InternalGeoDistance.java index 2c16d93ea02..13ed3a44fb9 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/InternalGeoDistance.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/InternalGeoDistance.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.aggregations.bucket.range.geodistance; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregations; @@ -28,7 +29,6 @@ import org.elasticsearch.search.aggregations.bucket.range.InternalRange; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSourceType; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -53,7 +53,7 @@ public class InternalGeoDistance extends InternalRange BUCKET_STREAM = new BucketStreams.Stream() { @Override public Bucket readResult(StreamInput in, BucketStreamContext context) throws IOException { - Bucket buckets = new Bucket(context.keyed(), context.formatter()); + Bucket buckets = new Bucket(context.keyed()); buckets.readFrom(in); return buckets; } @@ -61,7 +61,7 @@ public class InternalGeoDistance extends InternalRange aggregations, boolean keyed, - ValueFormatter formatter) { - this(key, from, to, docCount, new InternalAggregations(aggregations), keyed, formatter); + Bucket(String key, double from, double to, long docCount, List aggregations, boolean keyed) { + this(key, from, to, docCount, new InternalAggregations(aggregations), keyed); } - Bucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed, ValueFormatter formatter) { - super(key, from, to, docCount, aggregations, keyed, formatter); + Bucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed) { + super(key, from, to, docCount, aggregations, keyed, DocValueFormat.RAW); } @Override @@ -97,10 +96,6 @@ public class InternalGeoDistance extends InternalRange { @@ -121,36 +116,36 @@ public class InternalGeoDistance extends InternalRange ranges, ValueFormatter formatter, boolean keyed, + public InternalGeoDistance create(String name, List ranges, DocValueFormat format, boolean keyed, List pipelineAggregators, Map metaData) { - return new InternalGeoDistance(name, ranges, formatter, keyed, pipelineAggregators, metaData); + return new InternalGeoDistance(name, ranges, keyed, pipelineAggregators, metaData); } @Override public InternalGeoDistance create(List ranges, InternalGeoDistance prototype) { - return new InternalGeoDistance(prototype.name, ranges, prototype.formatter, prototype.keyed, prototype.pipelineAggregators(), + return new InternalGeoDistance(prototype.name, ranges, prototype.keyed, prototype.pipelineAggregators(), prototype.metaData); } @Override public Bucket createBucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed, - ValueFormatter formatter) { - return new Bucket(key, from, to, docCount, aggregations, keyed, formatter); + DocValueFormat format) { + return new Bucket(key, from, to, docCount, aggregations, keyed); } @Override public Bucket createBucket(InternalAggregations aggregations, Bucket prototype) { return new Bucket(prototype.getKey(), ((Number) prototype.getFrom()).doubleValue(), ((Number) prototype.getTo()).doubleValue(), - prototype.getDocCount(), aggregations, prototype.getKeyed(), prototype.getFormatter()); + prototype.getDocCount(), aggregations, prototype.getKeyed()); } } InternalGeoDistance() {} // for serialization - public InternalGeoDistance(String name, List ranges, ValueFormatter formatter, boolean keyed, + public InternalGeoDistance(String name, List ranges, boolean keyed, List pipelineAggregators, Map metaData) { - super(name, ranges, formatter, keyed, pipelineAggregators, metaData); + super(name, ranges, DocValueFormat.RAW, keyed, pipelineAggregators, metaData); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeAggregatorBuilder.java index a8c1a69126b..7c867d56fa1 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeAggregatorBuilder.java @@ -27,13 +27,13 @@ import org.elasticsearch.common.network.Cidrs; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregatorFactory; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; import org.elasticsearch.search.aggregations.bucket.range.AbstractRangeBuilder; import org.elasticsearch.search.aggregations.bucket.range.RangeAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; -import org.elasticsearch.search.aggregations.support.format.ValueParser; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; @@ -171,16 +171,16 @@ public class IPv4RangeAggregatorBuilder extends AbstractRangeBuilder aggregations, boolean keyed) { - super(key, from, to, docCount, new InternalAggregations(aggregations), keyed, ValueFormatter.IPv4); + super(key, from, to, docCount, new InternalAggregations(aggregations), keyed, DocValueFormat.IP); } public Bucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed) { - super(key, from, to, docCount, aggregations, keyed, ValueFormatter.IPv4); + super(key, from, to, docCount, aggregations, keyed, DocValueFormat.IP); } @Override public String getFromAsString() { double from = ((Number) this.from).doubleValue(); - return Double.isInfinite(from) ? null : from == 0 ? null : ValueFormatter.IPv4.format(from); + return Double.isInfinite(from) ? null : from == 0 ? null : DocValueFormat.IP.format(from); } @Override public String getToAsString() { double to = ((Number) this.to).doubleValue(); - return Double.isInfinite(to) ? null : MAX_IP == to ? null : ValueFormatter.IPv4.format(to); + return Double.isInfinite(to) ? null : MAX_IP == to ? null : DocValueFormat.IP.format(to); } @Override @@ -122,7 +122,7 @@ public class InternalIPv4Range extends InternalRange ranges, ValueFormatter formatter, boolean keyed, + public InternalIPv4Range create(String name, List ranges, DocValueFormat formatter, boolean keyed, List pipelineAggregators, Map metaData) { return new InternalIPv4Range(name, ranges, keyed, pipelineAggregators, metaData); } @@ -134,7 +134,7 @@ public class InternalIPv4Range extends InternalRange ranges, boolean keyed, List pipelineAggregators, Map metaData) { - super(name, ranges, ValueFormatter.IPv4, keyed, pipelineAggregators, metaData); + super(name, ranges, DocValueFormat.IP, keyed, pipelineAggregators, metaData); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTerms.java index 226c6d5b3b5..1a02a672343 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTerms.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTerms.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.bucket.significant; 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.InternalAggregations; import org.elasticsearch.search.aggregations.bucket.BucketStreamContext; @@ -28,14 +29,13 @@ import org.elasticsearch.search.aggregations.bucket.BucketStreams; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristic; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicStreams; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; /** * @@ -57,7 +57,7 @@ public class SignificantLongTerms extends InternalSignificantTerms buckets, List pipelineAggregators, Map metaData) { super(subsetSize, supersetSize, name, requiredSize, minDocCount, significanceHeuristic, buckets, pipelineAggregators, metaData); - this.formatter = formatter; + this.format = Objects.requireNonNull(format); } @Override @@ -181,28 +181,28 @@ public class SignificantLongTerms extends InternalSignificantTerms buckets) { - return new SignificantLongTerms(this.subsetSize, this.supersetSize, this.name, this.formatter, this.requiredSize, this.minDocCount, + return new SignificantLongTerms(this.subsetSize, this.supersetSize, this.name, this.format, this.requiredSize, this.minDocCount, this.significanceHeuristic, buckets, this.pipelineAggregators(), this.metaData); } @Override public Bucket createBucket(InternalAggregations aggregations, SignificantLongTerms.Bucket prototype) { return new Bucket(prototype.subsetDf, prototype.subsetSize, prototype.supersetDf, prototype.supersetSize, prototype.term, - aggregations, prototype.formatter); + aggregations, prototype.format); } @Override protected SignificantLongTerms create(long subsetSize, long supersetSize, List buckets, InternalSignificantTerms prototype) { - return new SignificantLongTerms(subsetSize, supersetSize, prototype.getName(), ((SignificantLongTerms) prototype).formatter, + return new SignificantLongTerms(subsetSize, supersetSize, prototype.getName(), ((SignificantLongTerms) prototype).format, prototype.requiredSize, prototype.minDocCount, prototype.significanceHeuristic, buckets, prototype.pipelineAggregators(), prototype.getMetaData()); } @Override protected void doReadFrom(StreamInput in) throws IOException { - this.formatter = ValueFormatterStreams.readOptional(in); + this.format = in.readValueFormat(); this.requiredSize = readSize(in); this.minDocCount = in.readVLong(); this.subsetSize = in.readVLong(); @@ -212,7 +212,7 @@ public class SignificantLongTerms extends InternalSignificantTerms buckets = new ArrayList<>(size); for (int i = 0; i < size; i++) { - Bucket bucket = new Bucket(subsetSize, supersetSize, formatter); + Bucket bucket = new Bucket(subsetSize, supersetSize, format); bucket.readFrom(in); buckets.add(bucket); @@ -223,7 +223,7 @@ public class SignificantLongTerms extends InternalSignificantTerms pipelineAggregators, Map metaData) throws IOException { @@ -90,7 +90,7 @@ public class SignificantLongTermsAggregator extends LongTermsAggregator { continue; } if (spare == null) { - spare = new SignificantLongTerms.Bucket(0, 0, 0, 0, 0, null, formatter); + spare = new SignificantLongTerms.Bucket(0, 0, 0, 0, 0, null, format); } spare.term = bucketOrds.get(i); spare.subsetDf = docCount; @@ -111,7 +111,7 @@ public class SignificantLongTermsAggregator extends LongTermsAggregator { bucket.aggregations = bucketAggregations(bucket.bucketOrd); list[i] = bucket; } - return new SignificantLongTerms(subsetSize, supersetSize, name, formatter, bucketCountThresholds.getRequiredSize(), + return new SignificantLongTerms(subsetSize, supersetSize, name, format, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getMinDocCount(), significanceHeuristic, Arrays.asList(list), pipelineAggregators(), metaData()); } @@ -122,7 +122,7 @@ public class SignificantLongTermsAggregator extends LongTermsAggregator { ContextIndexSearcher searcher = context.searchContext().searcher(); IndexReader topReader = searcher.getIndexReader(); int supersetSize = topReader.numDocs(); - return new SignificantLongTerms(0, supersetSize, name, formatter, bucketCountThresholds.getRequiredSize(), + return new SignificantLongTerms(0, supersetSize, name, format, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getMinDocCount(), significanceHeuristic, Collections. emptyList(), pipelineAggregators(), metaData()); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTerms.java index 9a5d7599f56..cc06d78da6e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTerms.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTerms.java @@ -22,13 +22,12 @@ import org.elasticsearch.Version; 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.InternalAggregations; import org.elasticsearch.search.aggregations.bucket.BucketStreamContext; import org.elasticsearch.search.aggregations.bucket.BucketStreams; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.ArrayList; @@ -55,7 +54,7 @@ public class DoubleTerms extends InternalTerms private final static BucketStreams.Stream BUCKET_STREAM = new BucketStreams.Stream() { @Override public Bucket readResult(StreamInput in, BucketStreamContext context) throws IOException { - Bucket buckets = new Bucket(context.formatter(), (boolean) context.attributes().get("showDocCountError")); + Bucket buckets = new Bucket(context.format(), (boolean) context.attributes().get("showDocCountError")); buckets.readFrom(in); return buckets; } @@ -66,7 +65,7 @@ public class DoubleTerms extends InternalTerms Map attributes = new HashMap<>(); attributes.put("showDocCountError", bucket.showDocCountError); context.attributes(attributes); - context.formatter(bucket.formatter); + context.format(bucket.format); return context; } }; @@ -80,13 +79,13 @@ public class DoubleTerms extends InternalTerms double term; - public Bucket(ValueFormatter formatter, boolean showDocCountError) { - super(formatter, showDocCountError); + public Bucket(DocValueFormat format, boolean showDocCountError) { + super(format, showDocCountError); } public Bucket(double term, long docCount, InternalAggregations aggregations, boolean showDocCountError, long docCountError, - ValueFormatter formatter) { - super(docCount, aggregations, showDocCountError, docCountError, formatter); + DocValueFormat format) { + super(docCount, aggregations, showDocCountError, docCountError, format); this.term = term; } @@ -112,7 +111,7 @@ public class DoubleTerms extends InternalTerms @Override Bucket newBucket(long docCount, InternalAggregations aggs, long docCountError) { - return new Bucket(term, docCount, aggs, showDocCountError, docCountError, formatter); + return new Bucket(term, docCount, aggs, showDocCountError, docCountError, format); } @Override @@ -140,8 +139,8 @@ public class DoubleTerms extends InternalTerms public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field(CommonFields.KEY, term); - if (formatter != ValueFormatter.RAW) { - builder.field(CommonFields.KEY_AS_STRING, formatter.format(term)); + if (format != DocValueFormat.RAW) { + builder.field(CommonFields.KEY_AS_STRING, format.format(term)); } builder.field(CommonFields.DOC_COUNT, getDocCount()); if (showDocCountError) { @@ -153,17 +152,17 @@ public class DoubleTerms extends InternalTerms } } - private ValueFormatter formatter; + private DocValueFormat format; DoubleTerms() { } // for serialization - public DoubleTerms(String name, Terms.Order order, ValueFormatter formatter, int requiredSize, int shardSize, + public DoubleTerms(String name, Terms.Order order, DocValueFormat format, int requiredSize, int shardSize, long minDocCount, List buckets, boolean showTermDocCountError, long docCountError, long otherDocCount, List pipelineAggregators, Map metaData) { super(name, order, requiredSize, shardSize, minDocCount, buckets, showTermDocCountError, docCountError, otherDocCount, pipelineAggregators, metaData); - this.formatter = formatter; + this.format = format; } @Override @@ -173,20 +172,20 @@ public class DoubleTerms extends InternalTerms @Override public DoubleTerms create(List buckets) { - return new DoubleTerms(this.name, this.order, this.formatter, this.requiredSize, this.shardSize, this.minDocCount, buckets, + return new DoubleTerms(this.name, this.order, this.format, this.requiredSize, this.shardSize, this.minDocCount, buckets, this.showTermDocCountError, this.docCountError, this.otherDocCount, this.pipelineAggregators(), this.metaData); } @Override public Bucket createBucket(InternalAggregations aggregations, Bucket prototype) { return new Bucket(prototype.term, prototype.docCount, aggregations, prototype.showDocCountError, prototype.docCountError, - prototype.formatter); + prototype.format); } @Override protected DoubleTerms create(String name, List buckets, long docCountError, long otherDocCount, InternalTerms prototype) { - return new DoubleTerms(name, prototype.order, ((DoubleTerms) prototype).formatter, prototype.requiredSize, prototype.shardSize, + return new DoubleTerms(name, prototype.order, ((DoubleTerms) prototype).format, prototype.requiredSize, prototype.shardSize, prototype.minDocCount, buckets, prototype.showTermDocCountError, docCountError, otherDocCount, prototype.pipelineAggregators(), prototype.getMetaData()); } @@ -195,7 +194,7 @@ public class DoubleTerms extends InternalTerms protected void doReadFrom(StreamInput in) throws IOException { this.docCountError = in.readLong(); this.order = InternalOrder.Streams.readOrder(in); - this.formatter = ValueFormatterStreams.readOptional(in); + this.format = in.readValueFormat(); this.requiredSize = readSize(in); this.shardSize = readSize(in); this.showTermDocCountError = in.readBoolean(); @@ -204,7 +203,7 @@ public class DoubleTerms extends InternalTerms int size = in.readVInt(); List buckets = new ArrayList<>(size); for (int i = 0; i < size; i++) { - Bucket bucket = new Bucket(formatter, showTermDocCountError); + Bucket bucket = new Bucket(format, showTermDocCountError); bucket.readFrom(in); buckets.add(bucket); } @@ -216,7 +215,7 @@ public class DoubleTerms extends InternalTerms protected void doWriteTo(StreamOutput out) throws IOException { out.writeLong(docCountError); InternalOrder.Streams.writeOrder(order, out); - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(format); writeSize(requiredSize, out); writeSize(shardSize, out); out.writeBoolean(showTermDocCountError); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTermsAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTermsAggregator.java index 0307a995455..fcaf73c334e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTermsAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTermsAggregator.java @@ -22,6 +22,7 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.SortedNumericDocValues; import org.apache.lucene.util.NumericUtils; import org.elasticsearch.index.fielddata.FieldData; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude; @@ -29,7 +30,6 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; import java.io.IOException; import java.util.Arrays; @@ -41,7 +41,7 @@ import java.util.Map; */ public class DoubleTermsAggregator extends LongTermsAggregator { - public DoubleTermsAggregator(String name, AggregatorFactories factories, ValuesSource.Numeric valuesSource, ValueFormat format, + public DoubleTermsAggregator(String name, AggregatorFactories factories, ValuesSource.Numeric valuesSource, DocValueFormat format, Terms.Order order, BucketCountThresholds bucketCountThresholds, AggregationContext aggregationContext, Aggregator parent, SubAggCollectionMode collectionMode, boolean showTermDocCountError, IncludeExclude.LongFilter longFilter, List pipelineAggregators, Map metaData) throws IOException { @@ -69,7 +69,7 @@ public class DoubleTermsAggregator extends LongTermsAggregator { private static DoubleTerms.Bucket convertToDouble(InternalTerms.Bucket bucket) { final long term = ((Number) bucket.getKey()).longValue(); final double value = NumericUtils.sortableLongToDouble(term); - return new DoubleTerms.Bucket(value, bucket.docCount, bucket.aggregations, bucket.showDocCountError, bucket.docCountError, bucket.formatter); + return new DoubleTerms.Bucket(value, bucket.docCount, bucket.aggregations, bucket.showDocCountError, bucket.docCountError, bucket.format); } private static DoubleTerms convertToDouble(LongTerms terms) { @@ -77,7 +77,7 @@ public class DoubleTermsAggregator extends LongTermsAggregator { for (int i = 0; i < buckets.length; ++i) { buckets[i] = convertToDouble(buckets[i]); } - return new DoubleTerms(terms.getName(), terms.order, terms.formatter, terms.requiredSize, terms.shardSize, terms.minDocCount, + return new DoubleTerms(terms.getName(), terms.order, terms.format, terms.requiredSize, terms.shardSize, terms.minDocCount, Arrays.asList(buckets), terms.showTermDocCountError, terms.docCountError, terms.otherDocCount, terms.pipelineAggregators(), terms.getMetaData()); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java index 6e5aaac2e8e..ba3d1475e03 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.aggregations.bucket.terms; import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -27,7 +28,6 @@ import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation; import org.elasticsearch.search.aggregations.bucket.terms.support.BucketPriorityQueue; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.util.ArrayList; import java.util.Arrays; @@ -52,16 +52,16 @@ public abstract class InternalTerms { private final static BucketStreams.Stream BUCKET_STREAM = new BucketStreams.Stream() { @Override public Bucket readResult(StreamInput in, BucketStreamContext context) throws IOException { - Bucket buckets = new Bucket(context.formatter(), (boolean) context.attributes().get("showDocCountError")); + Bucket buckets = new Bucket(context.format(), (boolean) context.attributes().get("showDocCountError")); buckets.readFrom(in); return buckets; } @@ -65,7 +64,7 @@ public class LongTerms extends InternalTerms { Map attributes = new HashMap<>(); attributes.put("showDocCountError", bucket.showDocCountError); context.attributes(attributes); - context.formatter(bucket.formatter); + context.format(bucket.format); return context; } }; @@ -79,19 +78,19 @@ public class LongTerms extends InternalTerms { long term; - public Bucket(ValueFormatter formatter, boolean showDocCountError) { - super(formatter, showDocCountError); + public Bucket(DocValueFormat format, boolean showDocCountError) { + super(format, showDocCountError); } public Bucket(long term, long docCount, InternalAggregations aggregations, boolean showDocCountError, long docCountError, - ValueFormatter formatter) { - super(docCount, aggregations, showDocCountError, docCountError, formatter); + DocValueFormat format) { + super(docCount, aggregations, showDocCountError, docCountError, format); this.term = term; } @Override public String getKeyAsString() { - return formatter.format(term); + return format.format(term); } @Override @@ -111,7 +110,7 @@ public class LongTerms extends InternalTerms { @Override Bucket newBucket(long docCount, InternalAggregations aggs, long docCountError) { - return new Bucket(term, docCount, aggs, showDocCountError, docCountError, formatter); + return new Bucket(term, docCount, aggs, showDocCountError, docCountError, format); } @Override @@ -139,8 +138,8 @@ public class LongTerms extends InternalTerms { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field(CommonFields.KEY, term); - if (formatter != ValueFormatter.RAW) { - builder.field(CommonFields.KEY_AS_STRING, formatter.format(term)); + if (format != DocValueFormat.RAW) { + builder.field(CommonFields.KEY_AS_STRING, format.format(term)); } builder.field(CommonFields.DOC_COUNT, getDocCount()); if (showDocCountError) { @@ -152,16 +151,16 @@ public class LongTerms extends InternalTerms { } } - ValueFormatter formatter; + DocValueFormat format; LongTerms() {} // for serialization - public LongTerms(String name, Terms.Order order, ValueFormatter formatter, int requiredSize, int shardSize, long minDocCount, + public LongTerms(String name, Terms.Order order, DocValueFormat formatter, int requiredSize, int shardSize, long minDocCount, List buckets, boolean showTermDocCountError, long docCountError, long otherDocCount, List pipelineAggregators, Map metaData) { super(name, order, requiredSize, shardSize, minDocCount, buckets, showTermDocCountError, docCountError, otherDocCount, pipelineAggregators, metaData); - this.formatter = formatter; + this.format = formatter; } @Override @@ -171,20 +170,20 @@ public class LongTerms extends InternalTerms { @Override public LongTerms create(List buckets) { - return new LongTerms(this.name, this.order, this.formatter, this.requiredSize, this.shardSize, this.minDocCount, buckets, + return new LongTerms(this.name, this.order, this.format, this.requiredSize, this.shardSize, this.minDocCount, buckets, this.showTermDocCountError, this.docCountError, this.otherDocCount, this.pipelineAggregators(), this.metaData); } @Override public Bucket createBucket(InternalAggregations aggregations, Bucket prototype) { return new Bucket(prototype.term, prototype.docCount, aggregations, prototype.showDocCountError, prototype.docCountError, - prototype.formatter); + prototype.format); } @Override protected LongTerms create(String name, List buckets, long docCountError, long otherDocCount, InternalTerms prototype) { - return new LongTerms(name, prototype.order, ((LongTerms) prototype).formatter, prototype.requiredSize, prototype.shardSize, + return new LongTerms(name, prototype.order, ((LongTerms) prototype).format, prototype.requiredSize, prototype.shardSize, prototype.minDocCount, buckets, prototype.showTermDocCountError, docCountError, otherDocCount, prototype.pipelineAggregators(), prototype.getMetaData()); } @@ -193,7 +192,7 @@ public class LongTerms extends InternalTerms { protected void doReadFrom(StreamInput in) throws IOException { this.docCountError = in.readLong(); this.order = InternalOrder.Streams.readOrder(in); - this.formatter = ValueFormatterStreams.readOptional(in); + this.format = in.readValueFormat(); this.requiredSize = readSize(in); this.shardSize = readSize(in); this.showTermDocCountError = in.readBoolean(); @@ -202,7 +201,7 @@ public class LongTerms extends InternalTerms { int size = in.readVInt(); List buckets = new ArrayList<>(size); for (int i = 0; i < size; i++) { - Bucket bucket = new Bucket(formatter, showTermDocCountError); + Bucket bucket = new Bucket(format, showTermDocCountError); bucket.readFrom(in); buckets.add(bucket); } @@ -214,7 +213,7 @@ public class LongTerms extends InternalTerms { protected void doWriteTo(StreamOutput out) throws IOException { out.writeLong(docCountError); InternalOrder.Streams.writeOrder(order, out); - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(format); writeSize(requiredSize, out); writeSize(shardSize, out); out.writeBoolean(showTermDocCountError); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsAggregator.java index 3b40bdcfcd3..aa4772cf77f 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsAggregator.java @@ -22,6 +22,7 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.SortedNumericDocValues; import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.util.LongHash; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -33,8 +34,6 @@ import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.Arrays; @@ -48,19 +47,19 @@ import java.util.Map; public class LongTermsAggregator extends TermsAggregator { protected final ValuesSource.Numeric valuesSource; - protected final ValueFormatter formatter; + protected final DocValueFormat format; protected final LongHash bucketOrds; private boolean showTermDocCountError; private LongFilter longFilter; - public LongTermsAggregator(String name, AggregatorFactories factories, ValuesSource.Numeric valuesSource, ValueFormat format, + public LongTermsAggregator(String name, AggregatorFactories factories, ValuesSource.Numeric valuesSource, DocValueFormat format, Terms.Order order, BucketCountThresholds bucketCountThresholds, AggregationContext aggregationContext, Aggregator parent, SubAggCollectionMode subAggCollectMode, boolean showTermDocCountError, IncludeExclude.LongFilter longFilter, List pipelineAggregators, Map metaData) throws IOException { super(name, factories, aggregationContext, parent, bucketCountThresholds, order, subAggCollectMode, pipelineAggregators, metaData); this.valuesSource = valuesSource; this.showTermDocCountError = showTermDocCountError; - this.formatter = format.formatter(); + this.format = format; this.longFilter = longFilter; bucketOrds = new LongHash(1, aggregationContext.bigArrays()); } @@ -131,7 +130,7 @@ public class LongTermsAggregator extends TermsAggregator { LongTerms.Bucket spare = null; for (long i = 0; i < bucketOrds.size(); i++) { if (spare == null) { - spare = new LongTerms.Bucket(0, 0, null, showTermDocCountError, 0, formatter); + spare = new LongTerms.Bucket(0, 0, null, showTermDocCountError, 0, format); } spare.term = bucketOrds.get(i); spare.docCount = bucketDocCount(i); @@ -160,14 +159,14 @@ public class LongTermsAggregator extends TermsAggregator { list[i].docCountError = 0; } - return new LongTerms(name, order, formatter, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getShardSize(), + return new LongTerms(name, order, format, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getShardSize(), bucketCountThresholds.getMinDocCount(), Arrays.asList(list), showTermDocCountError, 0, otherDocCount, pipelineAggregators(), metaData()); } @Override public InternalAggregation buildEmptyAggregation() { - return new LongTerms(name, order, formatter, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getShardSize(), + return new LongTerms(name, order, format, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getShardSize(), bucketCountThresholds.getMinDocCount(), Collections. emptyList(), showTermDocCountError, 0, 0, pipelineAggregators(), metaData()); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/InternalNumericMetricsAggregation.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/InternalNumericMetricsAggregation.java index 69ffcc049a0..02412ba81c6 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/InternalNumericMetricsAggregation.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/InternalNumericMetricsAggregation.java @@ -18,8 +18,8 @@ */ package org.elasticsearch.search.aggregations.metrics; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.util.List; import java.util.Map; @@ -29,7 +29,9 @@ import java.util.Map; */ public abstract class InternalNumericMetricsAggregation extends InternalMetricsAggregation { - protected ValueFormatter valueFormatter; + private static final DocValueFormat DEFAULT_FORMAT = DocValueFormat.RAW; + + protected DocValueFormat format = DEFAULT_FORMAT; public static abstract class SingleValue extends InternalNumericMetricsAggregation implements NumericMetricsAggregation.SingleValue { @@ -41,7 +43,7 @@ public abstract class InternalNumericMetricsAggregation extends InternalMetricsA @Override public String getValueAsString() { - return valueFormatter.format(value()); + return format.format(value()); } @Override @@ -68,7 +70,7 @@ public abstract class InternalNumericMetricsAggregation extends InternalMetricsA public abstract double value(String name); public String valueAsString(String name) { - return valueFormatter.format(value(name)); + return format.format(value(name)); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregator.java index 11417b1c615..eb0fc42f9c2 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregator.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.common.util.LongArray; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -32,7 +33,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -47,13 +47,13 @@ public class AvgAggregator extends NumericMetricsAggregator.SingleValue { LongArray counts; DoubleArray sums; - ValueFormatter formatter; + DocValueFormat format; - public AvgAggregator(String name, ValuesSource.Numeric valuesSource, ValueFormatter formatter, AggregationContext context, + public AvgAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat formatter, AggregationContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); this.valuesSource = valuesSource; - this.formatter = formatter; + this.format = formatter; if (valuesSource != null) { final BigArrays bigArrays = context.bigArrays(); counts = bigArrays.newLongArray(1, true); @@ -105,12 +105,12 @@ public class AvgAggregator extends NumericMetricsAggregator.SingleValue { if (valuesSource == null || bucket >= sums.size()) { return buildEmptyAggregation(); } - return new InternalAvg(name, sums.get(bucket), counts.get(bucket), formatter, pipelineAggregators(), metaData()); + return new InternalAvg(name, sums.get(bucket), counts.get(bucket), format, pipelineAggregators(), metaData()); } @Override public InternalAggregation buildEmptyAggregation() { - return new InternalAvg(name, 0.0, 0L, formatter, pipelineAggregators(), metaData()); + return new InternalAvg(name, 0.0, 0L, format, pipelineAggregators(), metaData()); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregatorFactory.java index e3728d09334..5d425f2732c 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/AvgAggregatorFactory.java @@ -44,12 +44,12 @@ public class AvgAggregatorFactory extends ValuesSourceAggregatorFactory pipelineAggregators, Map metaData) throws IOException { - return new AvgAggregator(name, null, config.formatter(), context, parent, pipelineAggregators, metaData); + return new AvgAggregator(name, null, config.format(), context, parent, pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(ValuesSource.Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new AvgAggregator(name, valuesSource, config.formatter(), context, parent, pipelineAggregators, metaData); + return new AvgAggregator(name, valuesSource, config.format(), context, parent, pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/InternalAvg.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/InternalAvg.java index b48952442ef..5148e602aae 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/InternalAvg.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/InternalAvg.java @@ -21,12 +21,11 @@ package org.elasticsearch.search.aggregations.metrics.avg; 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; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -57,12 +56,12 @@ public class InternalAvg extends InternalNumericMetricsAggregation.SingleValue i InternalAvg() {} // for serialization - public InternalAvg(String name, double sum, long count, ValueFormatter formatter, List pipelineAggregators, + public InternalAvg(String name, double sum, long count, DocValueFormat format, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.sum = sum; this.count = count; - this.valueFormatter = formatter; + this.format = format; } @Override @@ -88,19 +87,19 @@ public class InternalAvg extends InternalNumericMetricsAggregation.SingleValue i count += ((InternalAvg) aggregation).count; sum += ((InternalAvg) aggregation).sum; } - return new InternalAvg(getName(), sum, count, valueFormatter, pipelineAggregators(), getMetaData()); + return new InternalAvg(getName(), sum, count, format, pipelineAggregators(), getMetaData()); } @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); sum = in.readDouble(); count = in.readVLong(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeDouble(sum); out.writeVLong(count); } @@ -108,8 +107,8 @@ public class InternalAvg extends InternalNumericMetricsAggregation.SingleValue i @Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { builder.field(CommonFields.VALUE, count != 0 ? getValue() : null); - if (count != 0 && !(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(getValue())); + if (count != 0 && format != DocValueFormat.RAW) { + builder.field(CommonFields.VALUE_AS_STRING, format.format(getValue())); } return builder; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/CardinalityAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/CardinalityAggregator.java index 93fe7fff391..bb297812277 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/CardinalityAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/CardinalityAggregator.java @@ -35,6 +35,7 @@ import org.elasticsearch.common.util.LongArray; import org.elasticsearch.common.util.ObjectArray; import org.elasticsearch.index.fielddata.SortedBinaryDocValues; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -42,7 +43,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -61,15 +61,13 @@ public class CardinalityAggregator extends NumericMetricsAggregator.SingleValue private HyperLogLogPlusPlus counts; private Collector collector; - private ValueFormatter formatter; - public CardinalityAggregator(String name, ValuesSource valuesSource, int precision, ValueFormatter formatter, + public CardinalityAggregator(String name, ValuesSource valuesSource, int precision, AggregationContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); this.valuesSource = valuesSource; this.precision = precision; this.counts = valuesSource == null ? null : new HyperLogLogPlusPlus(precision, context.bigArrays(), 1); - this.formatter = formatter; } @Override @@ -146,12 +144,12 @@ public class CardinalityAggregator extends NumericMetricsAggregator.SingleValue // this Aggregator (and its HLL++ counters) is released. HyperLogLogPlusPlus copy = new HyperLogLogPlusPlus(precision, BigArrays.NON_RECYCLING_INSTANCE, 1); copy.merge(0, counts, owningBucketOrdinal); - return new InternalCardinality(name, copy, formatter, pipelineAggregators(), metaData()); + return new InternalCardinality(name, copy, pipelineAggregators(), metaData()); } @Override public InternalAggregation buildEmptyAggregation() { - return new InternalCardinality(name, null, formatter, pipelineAggregators(), metaData()); + return new InternalCardinality(name, null, pipelineAggregators(), metaData()); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/CardinalityAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/CardinalityAggregatorFactory.java index c4899f1b1ef..ff3e4959adf 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/CardinalityAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/CardinalityAggregatorFactory.java @@ -48,13 +48,13 @@ public class CardinalityAggregatorFactory extends ValuesSourceAggregatorFactory< @Override protected Aggregator createUnmapped(Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { - return new CardinalityAggregator(name, null, precision(parent), config.formatter(), context, parent, pipelineAggregators, metaData); + return new CardinalityAggregator(name, null, precision(parent), context, parent, pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(ValuesSource valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new CardinalityAggregator(name, valuesSource, precision(parent), config.formatter(), context, parent, pipelineAggregators, + return new CardinalityAggregator(name, valuesSource, precision(parent), context, parent, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/InternalCardinality.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/InternalCardinality.java index 69a2daa84b2..f4e9e3acd1f 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/InternalCardinality.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/cardinality/InternalCardinality.java @@ -27,8 +27,6 @@ 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; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -53,11 +51,10 @@ public final class InternalCardinality extends InternalNumericMetricsAggregation private HyperLogLogPlusPlus counts; - InternalCardinality(String name, HyperLogLogPlusPlus counts, ValueFormatter formatter, List pipelineAggregators, + InternalCardinality(String name, HyperLogLogPlusPlus counts, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.counts = counts; - this.valueFormatter = formatter; } private InternalCardinality() { @@ -80,7 +77,7 @@ public final class InternalCardinality extends InternalNumericMetricsAggregation @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); if (in.readBoolean()) { counts = HyperLogLogPlusPlus.readFrom(in, BigArrays.NON_RECYCLING_INSTANCE); } else { @@ -90,7 +87,7 @@ public final class InternalCardinality extends InternalNumericMetricsAggregation @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); if (counts != null) { out.writeBoolean(true); counts.writeTo(0, out); @@ -107,7 +104,7 @@ public final class InternalCardinality extends InternalNumericMetricsAggregation if (cardinality.counts != null) { if (reduced == null) { reduced = new InternalCardinality(name, new HyperLogLogPlusPlus(cardinality.counts.precision(), - BigArrays.NON_RECYCLING_INSTANCE, 1), this.valueFormatter, pipelineAggregators(), getMetaData()); + BigArrays.NON_RECYCLING_INSTANCE, 1), pipelineAggregators(), getMetaData()); } reduced.merge(cardinality); } @@ -129,9 +126,6 @@ public final class InternalCardinality extends InternalNumericMetricsAggregation public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { final long cardinality = getValue(); builder.field(CommonFields.VALUE, cardinality); - if (!(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(cardinality)); - } return builder; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/InternalMax.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/InternalMax.java index d5372a4f121..c72a3a4ed0a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/InternalMax.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/InternalMax.java @@ -21,12 +21,11 @@ package org.elasticsearch.search.aggregations.metrics.max; 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; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -56,10 +55,10 @@ public class InternalMax extends InternalNumericMetricsAggregation.SingleValue i InternalMax() {} // for serialization - public InternalMax(String name, double max, ValueFormatter formatter, List pipelineAggregators, + public InternalMax(String name, double max, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); - this.valueFormatter = formatter; + this.format = formatter; this.max = max; } @@ -84,18 +83,18 @@ public class InternalMax extends InternalNumericMetricsAggregation.SingleValue i for (InternalAggregation aggregation : aggregations) { max = Math.max(max, ((InternalMax) aggregation).max); } - return new InternalMax(name, max, valueFormatter, pipelineAggregators(), getMetaData()); + return new InternalMax(name, max, format, pipelineAggregators(), getMetaData()); } @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); max = in.readDouble(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeDouble(max); } @@ -103,8 +102,8 @@ public class InternalMax extends InternalNumericMetricsAggregation.SingleValue i public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { boolean hasValue = !Double.isInfinite(max); builder.field(CommonFields.VALUE, hasValue ? max : null); - if (hasValue && !(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(max)); + if (hasValue && format != DocValueFormat.RAW) { + builder.field(CommonFields.VALUE_AS_STRING, format.format(max)); } return builder; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregator.java index 2862dc2cad2..73504f9a8f4 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregator.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.index.fielddata.NumericDoubleValues; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import org.elasticsearch.search.MultiValueMode; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -33,7 +34,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -45,11 +45,11 @@ import java.util.Map; public class MaxAggregator extends NumericMetricsAggregator.SingleValue { final ValuesSource.Numeric valuesSource; - final ValueFormatter formatter; + final DocValueFormat formatter; DoubleArray maxes; - public MaxAggregator(String name, ValuesSource.Numeric valuesSource, ValueFormatter formatter, + public MaxAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat formatter, AggregationContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregatorFactory.java index 45acde7398b..2a9bdd9d8dc 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/max/MaxAggregatorFactory.java @@ -44,13 +44,13 @@ public class MaxAggregatorFactory extends ValuesSourceAggregatorFactory pipelineAggregators, Map metaData) throws IOException { - return new MaxAggregator(name, null, config.formatter(), context, parent, pipelineAggregators, metaData); + return new MaxAggregator(name, null, config.format(), context, parent, pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(ValuesSource.Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new MaxAggregator(name, valuesSource, config.formatter(), context, parent, pipelineAggregators, metaData); + return new MaxAggregator(name, valuesSource, config.format(), context, parent, pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/InternalMin.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/InternalMin.java index 728ef438452..8ebd985a6e6 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/InternalMin.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/InternalMin.java @@ -21,12 +21,11 @@ package org.elasticsearch.search.aggregations.metrics.min; 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; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -57,11 +56,11 @@ public class InternalMin extends InternalNumericMetricsAggregation.SingleValue i InternalMin() {} // for serialization - public InternalMin(String name, double min, ValueFormatter formatter, List pipelineAggregators, + public InternalMin(String name, double min, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.min = min; - this.valueFormatter = formatter; + this.format = formatter; } @Override @@ -85,18 +84,18 @@ public class InternalMin extends InternalNumericMetricsAggregation.SingleValue i for (InternalAggregation aggregation : aggregations) { min = Math.min(min, ((InternalMin) aggregation).min); } - return new InternalMin(getName(), min, this.valueFormatter, pipelineAggregators(), getMetaData()); + return new InternalMin(getName(), min, this.format, pipelineAggregators(), getMetaData()); } @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); min = in.readDouble(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeDouble(min); } @@ -104,8 +103,8 @@ public class InternalMin extends InternalNumericMetricsAggregation.SingleValue i public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { boolean hasValue = !Double.isInfinite(min); builder.field(CommonFields.VALUE, hasValue ? min : null); - if (hasValue && !(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(min)); + if (hasValue && format != DocValueFormat.RAW) { + builder.field(CommonFields.VALUE_AS_STRING, format.format(min)); } return builder; } 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 f0a6c8f3a31..a32379b7d10 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 @@ -25,6 +25,7 @@ import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.index.fielddata.NumericDoubleValues; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import org.elasticsearch.search.MultiValueMode; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -33,7 +34,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -45,11 +45,11 @@ import java.util.Map; public class MinAggregator extends NumericMetricsAggregator.SingleValue { final ValuesSource.Numeric valuesSource; - final ValueFormatter formatter; + final DocValueFormat format; DoubleArray mins; - public MinAggregator(String name, ValuesSource.Numeric valuesSource, ValueFormatter formatter, + public MinAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat formatter, AggregationContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); @@ -58,7 +58,7 @@ public class MinAggregator extends NumericMetricsAggregator.SingleValue { mins = context.bigArrays().newDoubleArray(1, false); mins.fill(0, mins.size(), Double.POSITIVE_INFINITY); } - this.formatter = formatter; + this.format = formatter; } @Override @@ -106,12 +106,12 @@ public class MinAggregator extends NumericMetricsAggregator.SingleValue { if (valuesSource == null || bucket >= mins.size()) { return buildEmptyAggregation(); } - return new InternalMin(name, mins.get(bucket), formatter, pipelineAggregators(), metaData()); + return new InternalMin(name, mins.get(bucket), format, pipelineAggregators(), metaData()); } @Override public InternalAggregation buildEmptyAggregation() { - return new InternalMin(name, Double.POSITIVE_INFINITY, formatter, pipelineAggregators(), metaData()); + return new InternalMin(name, Double.POSITIVE_INFINITY, format, pipelineAggregators(), metaData()); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinAggregatorFactory.java index c28a3386c27..1bc01d1e9e3 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/min/MinAggregatorFactory.java @@ -44,12 +44,12 @@ public class MinAggregatorFactory extends ValuesSourceAggregatorFactory pipelineAggregators, Map metaData) throws IOException { - return new MinAggregator(name, null, config.formatter(), context, parent, pipelineAggregators, metaData); + return new MinAggregator(name, null, config.format(), context, parent, pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(ValuesSource.Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new MinAggregator(name, valuesSource, config.formatter(), context, parent, pipelineAggregators, metaData); + return new MinAggregator(name, valuesSource, config.format(), context, parent, pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/AbstractHDRPercentilesAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/AbstractHDRPercentilesAggregator.java index be513a2e766..26ac179a561 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/AbstractHDRPercentilesAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/AbstractHDRPercentilesAggregator.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.util.ArrayUtils; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.ObjectArray; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.LeafBucketCollector; import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; @@ -33,7 +34,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -47,18 +47,18 @@ public abstract class AbstractHDRPercentilesAggregator extends NumericMetricsAgg protected final double[] keys; protected final ValuesSource.Numeric valuesSource; - protected final ValueFormatter formatter; + protected final DocValueFormat format; protected ObjectArray states; protected final int numberOfSignificantValueDigits; protected final boolean keyed; public AbstractHDRPercentilesAggregator(String name, ValuesSource.Numeric valuesSource, AggregationContext context, Aggregator parent, - double[] keys, int numberOfSignificantValueDigits, boolean keyed, ValueFormatter formatter, + double[] keys, int numberOfSignificantValueDigits, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); this.valuesSource = valuesSource; this.keyed = keyed; - this.formatter = formatter; + this.format = formatter; this.states = context.bigArrays().newObjectArray(1); this.keys = keys; this.numberOfSignificantValueDigits = numberOfSignificantValueDigits; diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/AbstractInternalHDRPercentiles.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/AbstractInternalHDRPercentiles.java index ddc0b0f92ad..4168e227101 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/AbstractInternalHDRPercentiles.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/AbstractInternalHDRPercentiles.java @@ -23,11 +23,10 @@ import org.HdrHistogram.DoubleHistogram; 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.InternalAggregation; import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.nio.ByteBuffer; @@ -43,14 +42,14 @@ abstract class AbstractInternalHDRPercentiles extends InternalNumericMetricsAggr AbstractInternalHDRPercentiles() {} // for serialization - public AbstractInternalHDRPercentiles(String name, double[] keys, DoubleHistogram state, boolean keyed, ValueFormatter formatter, + public AbstractInternalHDRPercentiles(String name, double[] keys, DoubleHistogram state, boolean keyed, DocValueFormat format, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.keys = keys; this.state = state; this.keyed = keyed; - this.valueFormatter = formatter; + this.format = format; } @Override @@ -83,7 +82,7 @@ abstract class AbstractInternalHDRPercentiles extends InternalNumericMetricsAggr @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); keys = new double[in.readInt()]; for (int i = 0; i < keys.length; ++i) { keys[i] = in.readDouble(); @@ -103,7 +102,7 @@ abstract class AbstractInternalHDRPercentiles extends InternalNumericMetricsAggr @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeInt(keys.length); for (int i = 0 ; i < keys.length; ++i) { out.writeDouble(keys[i]); @@ -124,8 +123,8 @@ abstract class AbstractInternalHDRPercentiles extends InternalNumericMetricsAggr String key = String.valueOf(keys[i]); double value = value(keys[i]); builder.field(key, value); - if (!(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(key + "_as_string", valueFormatter.format(value)); + if (format != DocValueFormat.RAW) { + builder.field(key + "_as_string", format.format(value)); } } builder.endObject(); @@ -136,8 +135,8 @@ abstract class AbstractInternalHDRPercentiles extends InternalNumericMetricsAggr builder.startObject(); builder.field(CommonFields.KEY, keys[i]); builder.field(CommonFields.VALUE, value); - if (!(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(value)); + if (format != DocValueFormat.RAW) { + builder.field(CommonFields.VALUE_AS_STRING, format.format(value)); } builder.endObject(); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentileRanksAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentileRanksAggregator.java index 603871e7909..faa6039f56c 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentileRanksAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentileRanksAggregator.java @@ -19,12 +19,12 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.hdr; import org.HdrHistogram.DoubleHistogram; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -36,9 +36,9 @@ import java.util.Map; public class HDRPercentileRanksAggregator extends AbstractHDRPercentilesAggregator { public HDRPercentileRanksAggregator(String name, Numeric valuesSource, AggregationContext context, Aggregator parent, - double[] percents, int numberOfSignificantValueDigits, boolean keyed, ValueFormatter formatter, + double[] percents, int numberOfSignificantValueDigits, boolean keyed, DocValueFormat format, List pipelineAggregators, Map metaData) throws IOException { - super(name, valuesSource, context, parent, percents, numberOfSignificantValueDigits, keyed, formatter, pipelineAggregators, + super(name, valuesSource, context, parent, percents, numberOfSignificantValueDigits, keyed, format, pipelineAggregators, metaData); } @@ -48,7 +48,7 @@ public class HDRPercentileRanksAggregator extends AbstractHDRPercentilesAggregat if (state == null) { return buildEmptyAggregation(); } else { - return new InternalHDRPercentileRanks(name, keys, state, keyed, formatter, pipelineAggregators(), metaData()); + return new InternalHDRPercentileRanks(name, keys, state, keyed, format, pipelineAggregators(), metaData()); } } @@ -58,7 +58,7 @@ public class HDRPercentileRanksAggregator extends AbstractHDRPercentilesAggregat state = new DoubleHistogram(numberOfSignificantValueDigits); state.setAutoResize(true); return new InternalHDRPercentileRanks(name, keys, state, - keyed, formatter, pipelineAggregators(), metaData()); + keyed, format, pipelineAggregators(), metaData()); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentileRanksAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentileRanksAggregatorFactory.java index 5ee9a41c70a..7f17220edad 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentileRanksAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentileRanksAggregatorFactory.java @@ -54,14 +54,14 @@ public class HDRPercentileRanksAggregatorFactory protected Aggregator createUnmapped(Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { return new HDRPercentileRanksAggregator(name, null, context, parent, values, numberOfSignificantValueDigits, keyed, - config.formatter(), pipelineAggregators, metaData); + config.format(), pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { return new HDRPercentileRanksAggregator(name, valuesSource, context, parent, values, numberOfSignificantValueDigits, keyed, - config.formatter(), pipelineAggregators, metaData); + config.format(), pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentilesAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentilesAggregator.java index f51769daab8..0b9c2c43d34 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentilesAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentilesAggregator.java @@ -19,12 +19,12 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.hdr; import org.HdrHistogram.DoubleHistogram; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -36,7 +36,7 @@ import java.util.Map; public class HDRPercentilesAggregator extends AbstractHDRPercentilesAggregator { public HDRPercentilesAggregator(String name, Numeric valuesSource, AggregationContext context, Aggregator parent, double[] percents, - int numberOfSignificantValueDigits, boolean keyed, ValueFormatter formatter, + int numberOfSignificantValueDigits, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) throws IOException { super(name, valuesSource, context, parent, percents, numberOfSignificantValueDigits, keyed, formatter, pipelineAggregators, metaData); @@ -48,7 +48,7 @@ public class HDRPercentilesAggregator extends AbstractHDRPercentilesAggregator { if (state == null) { return buildEmptyAggregation(); } else { - return new InternalHDRPercentiles(name, keys, state, keyed, formatter, pipelineAggregators(), metaData()); + return new InternalHDRPercentiles(name, keys, state, keyed, format, pipelineAggregators(), metaData()); } } @@ -69,6 +69,6 @@ public class HDRPercentilesAggregator extends AbstractHDRPercentilesAggregator { state.setAutoResize(true); return new InternalHDRPercentiles(name, keys, state, keyed, - formatter, pipelineAggregators(), metaData()); + format, pipelineAggregators(), metaData()); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentilesAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentilesAggregatorFactory.java index 8d24536f55e..0e1bd1e532d 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentilesAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/HDRPercentilesAggregatorFactory.java @@ -53,14 +53,14 @@ public class HDRPercentilesAggregatorFactory extends ValuesSourceAggregatorFacto protected Aggregator createUnmapped(Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { return new HDRPercentilesAggregator(name, null, context, parent, percents, numberOfSignificantValueDigits, keyed, - config.formatter(), pipelineAggregators, metaData); + config.format(), pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { return new HDRPercentilesAggregator(name, valuesSource, context, parent, percents, numberOfSignificantValueDigits, keyed, - config.formatter(), pipelineAggregators, metaData); + config.format(), pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentileRanks.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentileRanks.java index baa2325a734..6e072c08388 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentileRanks.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentileRanks.java @@ -20,12 +20,12 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.hdr; import org.HdrHistogram.DoubleHistogram; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentile; import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile; import org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.Iterator; @@ -55,7 +55,7 @@ public class InternalHDRPercentileRanks extends AbstractInternalHDRPercentiles i InternalHDRPercentileRanks() { } // for serialization - public InternalHDRPercentileRanks(String name, double[] cdfValues, DoubleHistogram state, boolean keyed, ValueFormatter formatter, + public InternalHDRPercentileRanks(String name, double[] cdfValues, DoubleHistogram state, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, cdfValues, state, keyed, formatter, pipelineAggregators, metaData); } @@ -83,7 +83,7 @@ public class InternalHDRPercentileRanks extends AbstractInternalHDRPercentiles i @Override protected AbstractInternalHDRPercentiles createReduced(String name, double[] keys, DoubleHistogram merged, boolean keyed, List pipelineAggregators, Map metaData) { - return new InternalHDRPercentileRanks(name, keys, merged, keyed, valueFormatter, pipelineAggregators, metaData); + return new InternalHDRPercentileRanks(name, keys, merged, keyed, format, pipelineAggregators, metaData); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentiles.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentiles.java index b7b126f0a6b..b8ecade437e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentiles.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/hdr/InternalHDRPercentiles.java @@ -20,12 +20,12 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.hdr; import org.HdrHistogram.DoubleHistogram; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentile; import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile; import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.Iterator; @@ -55,7 +55,7 @@ public class InternalHDRPercentiles extends AbstractInternalHDRPercentiles imple InternalHDRPercentiles() { } // for serialization - public InternalHDRPercentiles(String name, double[] percents, DoubleHistogram state, boolean keyed, ValueFormatter formatter, + public InternalHDRPercentiles(String name, double[] percents, DoubleHistogram state, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, percents, state, keyed, formatter, pipelineAggregators, metaData); } @@ -86,7 +86,7 @@ public class InternalHDRPercentiles extends AbstractInternalHDRPercentiles imple @Override protected AbstractInternalHDRPercentiles createReduced(String name, double[] keys, DoubleHistogram merged, boolean keyed, List pipelineAggregators, Map metaData) { - return new InternalHDRPercentiles(name, keys, merged, keyed, valueFormatter, pipelineAggregators, metaData); + return new InternalHDRPercentiles(name, keys, merged, keyed, format, pipelineAggregators, metaData); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/AbstractInternalTDigestPercentiles.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/AbstractInternalTDigestPercentiles.java index c357566d4de..6af7cb9b291 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/AbstractInternalTDigestPercentiles.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/AbstractInternalTDigestPercentiles.java @@ -22,11 +22,10 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest; 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.InternalAggregation; import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -40,14 +39,14 @@ abstract class AbstractInternalTDigestPercentiles extends InternalNumericMetrics AbstractInternalTDigestPercentiles() {} // for serialization - public AbstractInternalTDigestPercentiles(String name, double[] keys, TDigestState state, boolean keyed, ValueFormatter formatter, + public AbstractInternalTDigestPercentiles(String name, double[] keys, TDigestState state, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.keys = keys; this.state = state; this.keyed = keyed; - this.valueFormatter = formatter; + this.format = formatter; } @Override @@ -79,7 +78,7 @@ abstract class AbstractInternalTDigestPercentiles extends InternalNumericMetrics @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); keys = new double[in.readInt()]; for (int i = 0; i < keys.length; ++i) { keys[i] = in.readDouble(); @@ -90,7 +89,7 @@ abstract class AbstractInternalTDigestPercentiles extends InternalNumericMetrics @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeInt(keys.length); for (int i = 0 ; i < keys.length; ++i) { out.writeDouble(keys[i]); @@ -107,8 +106,8 @@ abstract class AbstractInternalTDigestPercentiles extends InternalNumericMetrics String key = String.valueOf(keys[i]); double value = value(keys[i]); builder.field(key, value); - if (!(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(key + "_as_string", valueFormatter.format(value)); + if (format != DocValueFormat.RAW) { + builder.field(key + "_as_string", format.format(value)); } } builder.endObject(); @@ -119,8 +118,8 @@ abstract class AbstractInternalTDigestPercentiles extends InternalNumericMetrics builder.startObject(); builder.field(CommonFields.KEY, keys[i]); builder.field(CommonFields.VALUE, value); - if (!(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(value)); + if (format != DocValueFormat.RAW) { + builder.field(CommonFields.VALUE_AS_STRING, format.format(value)); } builder.endObject(); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/AbstractTDigestPercentilesAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/AbstractTDigestPercentilesAggregator.java index 13c0a39c9c9..0676d291a11 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/AbstractTDigestPercentilesAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/AbstractTDigestPercentilesAggregator.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.util.ArrayUtils; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.ObjectArray; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.LeafBucketCollector; import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; @@ -32,7 +33,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -46,13 +46,13 @@ public abstract class AbstractTDigestPercentilesAggregator extends NumericMetric protected final double[] keys; protected final ValuesSource.Numeric valuesSource; - protected final ValueFormatter formatter; + protected final DocValueFormat formatter; protected ObjectArray states; protected final double compression; protected final boolean keyed; public AbstractTDigestPercentilesAggregator(String name, ValuesSource.Numeric valuesSource, AggregationContext context, Aggregator parent, - double[] keys, double compression, boolean keyed, ValueFormatter formatter, + double[] keys, double compression, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); this.valuesSource = valuesSource; diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentileRanks.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentileRanks.java index 61deecb279d..a293d773c83 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentileRanks.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentileRanks.java @@ -19,12 +19,12 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentile; import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile; import org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.Iterator; @@ -53,7 +53,7 @@ public class InternalTDigestPercentileRanks extends AbstractInternalTDigestPerce InternalTDigestPercentileRanks() {} // for serialization - public InternalTDigestPercentileRanks(String name, double[] cdfValues, TDigestState state, boolean keyed, ValueFormatter formatter, + public InternalTDigestPercentileRanks(String name, double[] cdfValues, TDigestState state, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, cdfValues, state, keyed, formatter, pipelineAggregators, metaData); } @@ -81,7 +81,7 @@ public class InternalTDigestPercentileRanks extends AbstractInternalTDigestPerce @Override protected AbstractInternalTDigestPercentiles createReduced(String name, double[] keys, TDigestState merged, boolean keyed, List pipelineAggregators, Map metaData) { - return new InternalTDigestPercentileRanks(name, keys, merged, keyed, valueFormatter, pipelineAggregators, metaData); + return new InternalTDigestPercentileRanks(name, keys, merged, keyed, format, pipelineAggregators, metaData); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentiles.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentiles.java index 878ef559ba4..999c85df0c0 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentiles.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentiles.java @@ -19,12 +19,12 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentile; import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile; import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.Iterator; @@ -54,7 +54,7 @@ public class InternalTDigestPercentiles extends AbstractInternalTDigestPercentil InternalTDigestPercentiles() { } // for serialization - public InternalTDigestPercentiles(String name, double[] percents, TDigestState state, boolean keyed, ValueFormatter formatter, + public InternalTDigestPercentiles(String name, double[] percents, TDigestState state, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, percents, state, keyed, formatter, pipelineAggregators, metaData); } @@ -82,7 +82,7 @@ public class InternalTDigestPercentiles extends AbstractInternalTDigestPercentil @Override protected AbstractInternalTDigestPercentiles createReduced(String name, double[] keys, TDigestState merged, boolean keyed, List pipelineAggregators, Map metaData) { - return new InternalTDigestPercentiles(name, keys, merged, keyed, valueFormatter, pipelineAggregators, metaData); + return new InternalTDigestPercentiles(name, keys, merged, keyed, format, pipelineAggregators, metaData); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentileRanksAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentileRanksAggregator.java index 3db68ca5855..1151e2272a4 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentileRanksAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentileRanksAggregator.java @@ -18,12 +18,12 @@ */ package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -35,7 +35,7 @@ import java.util.Map; public class TDigestPercentileRanksAggregator extends AbstractTDigestPercentilesAggregator { public TDigestPercentileRanksAggregator(String name, Numeric valuesSource, AggregationContext context, Aggregator parent, double[] percents, - double compression, boolean keyed, ValueFormatter formatter, List pipelineAggregators, + double compression, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) throws IOException { super(name, valuesSource, context, parent, percents, compression, keyed, formatter, pipelineAggregators, metaData); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentileRanksAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentileRanksAggregatorFactory.java index f5bf60fa038..a2605fce239 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentileRanksAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentileRanksAggregatorFactory.java @@ -53,14 +53,14 @@ public class TDigestPercentileRanksAggregatorFactory @Override protected Aggregator createUnmapped(Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { - return new TDigestPercentileRanksAggregator(name, null, context, parent, percents, compression, keyed, config.formatter(), + return new TDigestPercentileRanksAggregator(name, null, context, parent, percents, compression, keyed, config.format(), pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new TDigestPercentileRanksAggregator(name, valuesSource, context, parent, percents, compression, keyed, config.formatter(), + return new TDigestPercentileRanksAggregator(name, valuesSource, context, parent, percents, compression, keyed, config.format(), pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentilesAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentilesAggregator.java index 3bf7cff061c..c0063102e07 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentilesAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentilesAggregator.java @@ -18,12 +18,12 @@ */ package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -36,7 +36,7 @@ public class TDigestPercentilesAggregator extends AbstractTDigestPercentilesAggr public TDigestPercentilesAggregator(String name, Numeric valuesSource, AggregationContext context, Aggregator parent, double[] percents, - double compression, boolean keyed, ValueFormatter formatter, List pipelineAggregators, + double compression, boolean keyed, DocValueFormat formatter, List pipelineAggregators, Map metaData) throws IOException { super(name, valuesSource, context, parent, percents, compression, keyed, formatter, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentilesAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentilesAggregatorFactory.java index 5cd3649aaba..4513801fbfb 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentilesAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/TDigestPercentilesAggregatorFactory.java @@ -53,14 +53,14 @@ public class TDigestPercentilesAggregatorFactory @Override protected Aggregator createUnmapped(Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { - return new TDigestPercentilesAggregator(name, null, context, parent, percents, compression, keyed, config.formatter(), + return new TDigestPercentilesAggregator(name, null, context, parent, percents, compression, keyed, config.format(), pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new TDigestPercentilesAggregator(name, valuesSource, context, parent, percents, compression, keyed, config.formatter(), + return new TDigestPercentilesAggregator(name, valuesSource, context, parent, percents, compression, keyed, config.format(), pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/InternalStats.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/InternalStats.java index 9f1402f52e2..ce8a492f3a3 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/InternalStats.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/InternalStats.java @@ -22,12 +22,11 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; +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; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -69,7 +68,7 @@ public class InternalStats extends InternalNumericMetricsAggregation.MultiValue protected InternalStats() {} // for serialization - public InternalStats(String name, long count, double sum, double min, double max, ValueFormatter formatter, + public InternalStats(String name, long count, double sum, double min, double max, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); @@ -77,7 +76,7 @@ public class InternalStats extends InternalNumericMetricsAggregation.MultiValue this.sum = sum; this.min = min; this.max = max; - this.valueFormatter = formatter; + this.format = formatter; } @Override @@ -162,12 +161,12 @@ public class InternalStats extends InternalNumericMetricsAggregation.MultiValue max = Math.max(max, stats.getMax()); sum += stats.getSum(); } - return new InternalStats(name, count, sum, min, max, valueFormatter, pipelineAggregators(), getMetaData()); + return new InternalStats(name, count, sum, min, max, format, pipelineAggregators(), getMetaData()); } @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); count = in.readVLong(); min = in.readDouble(); max = in.readDouble(); @@ -180,7 +179,7 @@ public class InternalStats extends InternalNumericMetricsAggregation.MultiValue @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeVLong(count); out.writeDouble(min); out.writeDouble(max); @@ -210,11 +209,11 @@ public class InternalStats extends InternalNumericMetricsAggregation.MultiValue builder.field(Fields.MAX, count != 0 ? max : null); builder.field(Fields.AVG, count != 0 ? getAvg() : null); builder.field(Fields.SUM, count != 0 ? sum : null); - if (count != 0 && !(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(Fields.MIN_AS_STRING, valueFormatter.format(min)); - builder.field(Fields.MAX_AS_STRING, valueFormatter.format(max)); - builder.field(Fields.AVG_AS_STRING, valueFormatter.format(getAvg())); - builder.field(Fields.SUM_AS_STRING, valueFormatter.format(sum)); + if (count != 0 && format != DocValueFormat.RAW) { + builder.field(Fields.MIN_AS_STRING, format.format(min)); + builder.field(Fields.MAX_AS_STRING, format.format(max)); + builder.field(Fields.AVG_AS_STRING, format.format(getAvg())); + builder.field(Fields.SUM_AS_STRING, format.format(sum)); } otherStatsToXCotent(builder, params); return builder; diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/StatsAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/StatsAggregator.java index 3a497d70df0..374cbcaf0e6 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/StatsAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/StatsAggregator.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.common.util.LongArray; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -32,7 +33,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -44,7 +44,7 @@ import java.util.Map; public class StatsAggregator extends NumericMetricsAggregator.MultiValue { final ValuesSource.Numeric valuesSource; - final ValueFormatter formatter; + final DocValueFormat format; LongArray counts; DoubleArray sums; @@ -52,7 +52,7 @@ public class StatsAggregator extends NumericMetricsAggregator.MultiValue { DoubleArray maxes; - public StatsAggregator(String name, ValuesSource.Numeric valuesSource, ValueFormatter formatter, + public StatsAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat format, AggregationContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { @@ -67,7 +67,7 @@ public class StatsAggregator extends NumericMetricsAggregator.MultiValue { maxes = bigArrays.newDoubleArray(1, false); maxes.fill(0, maxes.size(), Double.NEGATIVE_INFINITY); } - this.formatter = formatter; + this.format = format; } @Override @@ -156,12 +156,12 @@ public class StatsAggregator extends NumericMetricsAggregator.MultiValue { return buildEmptyAggregation(); } return new InternalStats(name, counts.get(bucket), sums.get(bucket), mins.get(bucket), - maxes.get(bucket), formatter, pipelineAggregators(), metaData()); + maxes.get(bucket), format, pipelineAggregators(), metaData()); } @Override public InternalAggregation buildEmptyAggregation() { - return new InternalStats(name, 0, 0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, formatter, pipelineAggregators(), metaData()); + return new InternalStats(name, 0, 0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, format, pipelineAggregators(), metaData()); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/StatsAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/StatsAggregatorFactory.java index 4564cdaacef..a4f78f53d07 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/StatsAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/StatsAggregatorFactory.java @@ -44,12 +44,12 @@ public class StatsAggregatorFactory extends ValuesSourceAggregatorFactory pipelineAggregators, Map metaData) throws IOException { - return new StatsAggregator(name, null, config.formatter(), context, parent, pipelineAggregators, metaData); + return new StatsAggregator(name, null, config.format(), context, parent, pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(ValuesSource.Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new StatsAggregator(name, valuesSource, config.formatter(), context, parent, pipelineAggregators, metaData); + return new StatsAggregator(name, valuesSource, config.format(), context, parent, pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ExtendedStatsAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ExtendedStatsAggregator.java index c9413e15d2a..7715d4b713e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ExtendedStatsAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ExtendedStatsAggregator.java @@ -25,16 +25,15 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.common.util.LongArray; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator; -import org.elasticsearch.search.aggregations.metrics.stats.InternalStats; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -48,7 +47,7 @@ public class ExtendedStatsAggregator extends NumericMetricsAggregator.MultiValue public static final ParseField SIGMA_FIELD = new ParseField("sigma"); final ValuesSource.Numeric valuesSource; - final ValueFormatter formatter; + final DocValueFormat format; final double sigma; LongArray counts; @@ -57,13 +56,13 @@ public class ExtendedStatsAggregator extends NumericMetricsAggregator.MultiValue DoubleArray maxes; DoubleArray sumOfSqrs; - public ExtendedStatsAggregator(String name, ValuesSource.Numeric valuesSource, ValueFormatter formatter, + public ExtendedStatsAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat formatter, AggregationContext context, Aggregator parent, double sigma, List pipelineAggregators, Map metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); this.valuesSource = valuesSource; - this.formatter = formatter; + this.format = formatter; this.sigma = sigma; if (valuesSource != null) { final BigArrays bigArrays = context.bigArrays(); @@ -187,13 +186,13 @@ public class ExtendedStatsAggregator extends NumericMetricsAggregator.MultiValue return buildEmptyAggregation(); } return new InternalExtendedStats(name, counts.get(bucket), sums.get(bucket), - mins.get(bucket), maxes.get(bucket), sumOfSqrs.get(bucket), sigma, formatter, + mins.get(bucket), maxes.get(bucket), sumOfSqrs.get(bucket), sigma, format, pipelineAggregators(), metaData()); } @Override public InternalAggregation buildEmptyAggregation() { - return new InternalExtendedStats(name, 0, 0d, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0d, sigma, formatter, pipelineAggregators(), + return new InternalExtendedStats(name, 0, 0d, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0d, sigma, format, pipelineAggregators(), metaData()); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ExtendedStatsAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ExtendedStatsAggregatorFactory.java index 22f49e3ee64..82b0862482e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ExtendedStatsAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ExtendedStatsAggregatorFactory.java @@ -48,12 +48,12 @@ public class ExtendedStatsAggregatorFactory extends ValuesSourceAggregatorFactor @Override protected Aggregator createUnmapped(Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { - return new ExtendedStatsAggregator(name, null, config.formatter(), context, parent, sigma, pipelineAggregators, metaData); + return new ExtendedStatsAggregator(name, null, config.format(), context, parent, sigma, pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(ValuesSource.Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new ExtendedStatsAggregator(name, valuesSource, config.formatter(), context, parent, sigma, pipelineAggregators, metaData); + return new ExtendedStatsAggregator(name, valuesSource, config.format(), context, parent, sigma, pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/InternalExtendedStats.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/InternalExtendedStats.java index fe83e045952..d2cb2fb8a51 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/InternalExtendedStats.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/InternalExtendedStats.java @@ -23,11 +23,11 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.metrics.stats.InternalStats; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -68,7 +68,7 @@ public class InternalExtendedStats extends InternalStats implements ExtendedStat protected InternalExtendedStats() {} // for serialization public InternalExtendedStats(String name, long count, double sum, double min, double max, double sumOfSqrs, double sigma, - ValueFormatter formatter, List pipelineAggregators, Map metaData) { + DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, count, sum, min, max, formatter, pipelineAggregators, metaData); this.sumOfSqrs = sumOfSqrs; this.sigma = sigma; @@ -155,7 +155,7 @@ public class InternalExtendedStats extends InternalStats implements ExtendedStat } final InternalStats stats = super.doReduce(aggregations, reduceContext); return new InternalExtendedStats(name, stats.getCount(), stats.getSum(), stats.getMin(), stats.getMax(), sumOfSqrs, sigma, - valueFormatter, pipelineAggregators(), getMetaData()); + format, pipelineAggregators(), getMetaData()); } @Override @@ -195,9 +195,9 @@ public class InternalExtendedStats extends InternalStats implements ExtendedStat .field(Fields.LOWER, count != 0 ? getStdDeviationBound(Bounds.LOWER) : null) .endObject(); - if (count != 0 && !(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(Fields.SUM_OF_SQRS_AS_STRING, valueFormatter.format(sumOfSqrs)); - builder.field(Fields.VARIANCE_AS_STRING, valueFormatter.format(getVariance())); + if (count != 0 && format != DocValueFormat.RAW) { + builder.field(Fields.SUM_OF_SQRS_AS_STRING, format.format(sumOfSqrs)); + builder.field(Fields.VARIANCE_AS_STRING, format.format(getVariance())); builder.field(Fields.STD_DEVIATION_AS_STRING, getStdDeviationAsString()); builder.startObject(Fields.STD_DEVIATION_BOUNDS_AS_STRING) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/InternalSum.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/InternalSum.java index decb47479f4..ee6851c94cb 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/InternalSum.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/InternalSum.java @@ -21,12 +21,11 @@ package org.elasticsearch.search.aggregations.metrics.sum; 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; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -56,11 +55,11 @@ public class InternalSum extends InternalNumericMetricsAggregation.SingleValue i InternalSum() {} // for serialization - InternalSum(String name, double sum, ValueFormatter formatter, List pipelineAggregators, + InternalSum(String name, double sum, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.sum = sum; - this.valueFormatter = formatter; + this.format = formatter; } @Override @@ -84,26 +83,26 @@ public class InternalSum extends InternalNumericMetricsAggregation.SingleValue i for (InternalAggregation aggregation : aggregations) { sum += ((InternalSum) aggregation).sum; } - return new InternalSum(name, sum, valueFormatter, pipelineAggregators(), getMetaData()); + return new InternalSum(name, sum, format, pipelineAggregators(), getMetaData()); } @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); sum = in.readDouble(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeDouble(sum); } @Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { builder.field(CommonFields.VALUE, sum); - if (!(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(sum)); + if (format != DocValueFormat.RAW) { + builder.field(CommonFields.VALUE_AS_STRING, format.format(sum)); } return builder; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregator.java index 79f6ad4e564..0d6b5de4a80 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregator.java @@ -23,6 +23,7 @@ import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -31,7 +32,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -43,15 +43,15 @@ import java.util.Map; public class SumAggregator extends NumericMetricsAggregator.SingleValue { final ValuesSource.Numeric valuesSource; - final ValueFormatter formatter; + final DocValueFormat format; DoubleArray sums; - public SumAggregator(String name, ValuesSource.Numeric valuesSource, ValueFormatter formatter, AggregationContext context, + public SumAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat formatter, AggregationContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { super(name, context, parent, pipelineAggregators, metaData); this.valuesSource = valuesSource; - this.formatter = formatter; + this.format = formatter; if (valuesSource != null) { sums = context.bigArrays().newDoubleArray(1, true); } @@ -98,12 +98,12 @@ public class SumAggregator extends NumericMetricsAggregator.SingleValue { if (valuesSource == null || bucket >= sums.size()) { return buildEmptyAggregation(); } - return new InternalSum(name, sums.get(bucket), formatter, pipelineAggregators(), metaData()); + return new InternalSum(name, sums.get(bucket), format, pipelineAggregators(), metaData()); } @Override public InternalAggregation buildEmptyAggregation() { - return new InternalSum(name, 0.0, formatter, pipelineAggregators(), metaData()); + return new InternalSum(name, 0.0, format, pipelineAggregators(), metaData()); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregatorFactory.java index 7570c76b272..07184849c5e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/sum/SumAggregatorFactory.java @@ -44,12 +44,12 @@ public class SumAggregatorFactory extends ValuesSourceAggregatorFactory pipelineAggregators, Map metaData) throws IOException { - return new SumAggregator(name, null, config.formatter(), context, parent, pipelineAggregators, metaData); + return new SumAggregator(name, null, config.format(), context, parent, pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(ValuesSource.Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new SumAggregator(name, valuesSource, config.formatter(), context, parent, pipelineAggregators, metaData); + return new SumAggregator(name, valuesSource, config.format(), context, parent, pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/InternalValueCount.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/InternalValueCount.java index fd97972471e..807c7ed78e2 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/InternalValueCount.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/InternalValueCount.java @@ -25,8 +25,6 @@ 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; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -56,11 +54,10 @@ public class InternalValueCount extends InternalNumericMetricsAggregation.Single InternalValueCount() {} // for serialization - public InternalValueCount(String name, long value, ValueFormatter formatter, List pipelineAggregators, + public InternalValueCount(String name, long value, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.value = value; - this.valueFormatter = formatter; } @Override @@ -84,27 +81,22 @@ public class InternalValueCount extends InternalNumericMetricsAggregation.Single for (InternalAggregation aggregation : aggregations) { valueCount += ((InternalValueCount) aggregation).value; } - return new InternalValueCount(name, valueCount, valueFormatter, pipelineAggregators(), getMetaData()); + return new InternalValueCount(name, valueCount, pipelineAggregators(), getMetaData()); } @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); value = in.readVLong(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); out.writeVLong(value); } @Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { builder.field(CommonFields.VALUE, value); - if (!(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(value)); - } return builder; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregator.java index a25f7dea70e..4dbfb54e8da 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregator.java @@ -31,7 +31,6 @@ 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.ValuesSource; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -46,18 +45,16 @@ import java.util.Map; public class ValueCountAggregator extends NumericMetricsAggregator.SingleValue { final ValuesSource valuesSource; - final ValueFormatter formatter; // a count per bucket LongArray counts; - public ValueCountAggregator(String name, ValuesSource valuesSource, ValueFormatter formatter, + public ValueCountAggregator(String name, ValuesSource valuesSource, AggregationContext aggregationContext, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { super(name, aggregationContext, parent, pipelineAggregators, metaData); this.valuesSource = valuesSource; - this.formatter = formatter; if (valuesSource != null) { counts = context.bigArrays().newLongArray(1, true); } @@ -93,12 +90,12 @@ public class ValueCountAggregator extends NumericMetricsAggregator.SingleValue { if (valuesSource == null || bucket >= counts.size()) { return buildEmptyAggregation(); } - return new InternalValueCount(name, counts.get(bucket), formatter, pipelineAggregators(), metaData()); + return new InternalValueCount(name, counts.get(bucket), pipelineAggregators(), metaData()); } @Override public InternalAggregation buildEmptyAggregation() { - return new InternalValueCount(name, 0L, formatter, pipelineAggregators(), metaData()); + return new InternalValueCount(name, 0L, pipelineAggregators(), metaData()); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregatorFactory.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregatorFactory.java index 73481e18105..4569f3421d0 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregatorFactory.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregatorFactory.java @@ -43,12 +43,12 @@ public class ValueCountAggregatorFactory extends ValuesSourceAggregatorFactory pipelineAggregators, Map metaData) throws IOException { - return new ValueCountAggregator(name, null, config.formatter(), context, parent, pipelineAggregators, metaData); + return new ValueCountAggregator(name, null, context, parent, pipelineAggregators, metaData); } @Override protected Aggregator doCreateInternal(ValuesSource valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - return new ValueCountAggregator(name, valuesSource, config.formatter(), context, parent, pipelineAggregators, metaData); + return new ValueCountAggregator(name, valuesSource, context, parent, pipelineAggregators, metaData); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValue.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValue.java index bd1fa8dc5d9..9f6bec782c0 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValue.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/InternalSimpleValue.java @@ -22,12 +22,11 @@ package org.elasticsearch.search.aggregations.pipeline; 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.metrics.max.InternalMax; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.List; @@ -55,10 +54,10 @@ public class InternalSimpleValue extends InternalNumericMetricsAggregation.Singl protected InternalSimpleValue() { } // for serialization - public InternalSimpleValue(String name, double value, ValueFormatter formatter, List pipelineAggregators, + public InternalSimpleValue(String name, double value, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); - this.valueFormatter = formatter; + this.format = formatter; this.value = value; } @@ -83,13 +82,13 @@ public class InternalSimpleValue extends InternalNumericMetricsAggregation.Singl @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); value = in.readDouble(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeDouble(value); } @@ -97,8 +96,8 @@ public class InternalSimpleValue extends InternalNumericMetricsAggregation.Singl public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { boolean hasValue = !(Double.isInfinite(value) || Double.isNaN(value)); builder.field(CommonFields.VALUE, hasValue ? value : null); - if (hasValue && !(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(value)); + if (hasValue && format != DocValueFormat.RAW) { + builder.field(CommonFields.VALUE_AS_STRING, format.format(value)); } return builder; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/BucketMetricsPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/BucketMetricsPipelineAggregator.java index e79e4e2aa26..87a6aa22e64 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/BucketMetricsPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/BucketMetricsPipelineAggregator.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -32,8 +33,6 @@ import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator; import org.elasticsearch.search.aggregations.support.AggregationPath; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.Collections; @@ -46,18 +45,18 @@ import java.util.Map; */ public abstract class BucketMetricsPipelineAggregator extends SiblingPipelineAggregator { - protected ValueFormatter formatter; + protected DocValueFormat format; protected GapPolicy gapPolicy; public BucketMetricsPipelineAggregator() { super(); } - protected BucketMetricsPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, ValueFormatter formatter, + protected BucketMetricsPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, DocValueFormat format, Map metaData) { super(name, bucketsPaths, metaData); this.gapPolicy = gapPolicy; - this.formatter = formatter; + this.format = format; } @Override @@ -113,13 +112,13 @@ public abstract class BucketMetricsPipelineAggregator extends SiblingPipelineAgg @Override public void doReadFrom(StreamInput in) throws IOException { - formatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); gapPolicy = GapPolicy.readFrom(in); } @Override public void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(format); gapPolicy.writeTo(out); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/BucketMetricsPipelineAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/BucketMetricsPipelineAggregatorBuilder.java index c5d290302f5..c28ebed7a7a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/BucketMetricsPipelineAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/BucketMetricsPipelineAggregatorBuilder.java @@ -22,12 +22,11 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics; 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.AggregatorFactory; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -60,11 +59,11 @@ public abstract class BucketMetricsPipelineAggregatorBuilder pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.keys = keys; this.value = value; - this.valueFormatter = formatter; + this.format = formatter; } @Override @@ -100,14 +99,14 @@ public class InternalBucketMetricValue extends InternalNumericMetricsAggregation @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); value = in.readDouble(); keys = in.readStringArray(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeDouble(value); out.writeStringArray(keys); } @@ -116,8 +115,8 @@ public class InternalBucketMetricValue extends InternalNumericMetricsAggregation public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { boolean hasValue = !Double.isInfinite(value); builder.field(CommonFields.VALUE, hasValue ? value : null); - if (hasValue && !(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field(CommonFields.VALUE_AS_STRING, valueFormatter.format(value)); + if (hasValue && format != DocValueFormat.RAW) { + builder.field(CommonFields.VALUE_AS_STRING, format.format(value)); } builder.startArray("keys"); for (String key : keys) { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/avg/AvgBucketPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/avg/AvgBucketPipelineAggregator.java index 7210bdb2106..74ae7325051 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/avg/AvgBucketPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/avg/AvgBucketPipelineAggregator.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.avg; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; @@ -27,7 +28,6 @@ import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsPipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -56,9 +56,9 @@ public class AvgBucketPipelineAggregator extends BucketMetricsPipelineAggregator private AvgBucketPipelineAggregator() { } - protected AvgBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, ValueFormatter formatter, + protected AvgBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, DocValueFormat format, Map metaData) { - super(name, bucketsPaths, gapPolicy, formatter, metaData); + super(name, bucketsPaths, gapPolicy, format, metaData); } @Override @@ -81,7 +81,7 @@ public class AvgBucketPipelineAggregator extends BucketMetricsPipelineAggregator @Override protected InternalAggregation buildAggregation(List pipelineAggregators, Map metadata) { double avgValue = count == 0 ? Double.NaN : (sum / count); - return new InternalSimpleValue(name(), avgValue, formatter, pipelineAggregators, metadata); + return new InternalSimpleValue(name(), avgValue, format, pipelineAggregators, metadata); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/max/MaxBucketPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/max/MaxBucketPipelineAggregator.java index e4cd5569ec2..fa10de557ae 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/max/MaxBucketPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/max/MaxBucketPipelineAggregator.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.max; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; @@ -27,7 +28,6 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsPipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.ArrayList; @@ -58,7 +58,7 @@ public class MaxBucketPipelineAggregator extends BucketMetricsPipelineAggregator private MaxBucketPipelineAggregator() { } - protected MaxBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, ValueFormatter formatter, + protected MaxBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, DocValueFormat formatter, Map metaData) { super(name, bucketsPaths, gapPolicy, formatter, metaData); } @@ -88,7 +88,7 @@ public class MaxBucketPipelineAggregator extends BucketMetricsPipelineAggregator @Override protected InternalAggregation buildAggregation(List pipelineAggregators, Map metadata) { String[] keys = maxBucketKeys.toArray(new String[maxBucketKeys.size()]); - return new InternalBucketMetricValue(name(), keys, maxValue, formatter, Collections.emptyList(), metaData()); + return new InternalBucketMetricValue(name(), keys, maxValue, format, Collections.emptyList(), metaData()); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/min/MinBucketPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/min/MinBucketPipelineAggregator.java index 108ee0d2a7d..37e4a89f7c1 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/min/MinBucketPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/min/MinBucketPipelineAggregator.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.min; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; @@ -27,7 +28,6 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsPipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.ArrayList; @@ -58,7 +58,7 @@ public class MinBucketPipelineAggregator extends BucketMetricsPipelineAggregator private MinBucketPipelineAggregator() { } - protected MinBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, ValueFormatter formatter, + protected MinBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, DocValueFormat formatter, Map metaData) { super(name, bucketsPaths, gapPolicy, formatter, metaData); } @@ -89,7 +89,7 @@ public class MinBucketPipelineAggregator extends BucketMetricsPipelineAggregator protected InternalAggregation buildAggregation(java.util.List pipelineAggregators, java.util.Map metadata) { String[] keys = minBucketKeys.toArray(new String[minBucketKeys.size()]); - return new InternalBucketMetricValue(name(), keys, minValue, formatter, Collections.emptyList(), metaData()); + return new InternalBucketMetricValue(name(), keys, minValue, format, Collections.emptyList(), metaData()); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucket.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucket.java index 6a11c6fafd7..00b4b5ba90a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucket.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/InternalPercentilesBucket.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile; 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; @@ -29,8 +30,6 @@ import org.elasticsearch.search.aggregations.metrics.max.InternalMax; import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentile; import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.Arrays; @@ -62,10 +61,10 @@ public class InternalPercentilesBucket extends InternalNumericMetricsAggregation } // for serialization public InternalPercentilesBucket(String name, double[] percents, double[] percentiles, - ValueFormatter formatter, List pipelineAggregators, + DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); - this.valueFormatter = formatter; + this.format = formatter; this.percentiles = percentiles; this.percents = percents; } @@ -82,7 +81,7 @@ public class InternalPercentilesBucket extends InternalNumericMetricsAggregation @Override public String percentileAsString(double percent) { - return valueFormatter.format(percentile(percent)); + return format.format(percentile(percent)); } @Override @@ -107,14 +106,14 @@ public class InternalPercentilesBucket extends InternalNumericMetricsAggregation @Override protected void doReadFrom(StreamInput in) throws IOException { - valueFormatter = ValueFormatterStreams.readOptional(in); + format = in.readValueFormat(); percentiles = in.readDoubleArray(); percents = in.readDoubleArray(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(valueFormatter, out); + out.writeValueFormat(format); out.writeDoubleArray(percentiles); out.writeDoubleArray(percents); } @@ -127,7 +126,7 @@ public class InternalPercentilesBucket extends InternalNumericMetricsAggregation boolean hasValue = !(Double.isInfinite(value) || Double.isNaN(value)); String key = String.valueOf(percent); builder.field(key, hasValue ? value : null); - if (hasValue && !(valueFormatter instanceof ValueFormatter.Raw)) { + if (hasValue && format != DocValueFormat.RAW) { builder.field(key + "_as_string", percentileAsString(percent)); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/PercentilesBucketPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/PercentilesBucketPipelineAggregator.java index 1a4aa339d0d..d4b7d0526ea 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/PercentilesBucketPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/percentile/PercentilesBucketPipelineAggregator.java @@ -22,13 +22,13 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsPipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.ArrayList; @@ -62,7 +62,7 @@ public class PercentilesBucketPipelineAggregator extends BucketMetricsPipelineAg } protected PercentilesBucketPipelineAggregator(String name, double[] percents, String[] bucketsPaths, GapPolicy gapPolicy, - ValueFormatter formatter, Map metaData) { + DocValueFormat formatter, Map metaData) { super(name, bucketsPaths, gapPolicy, formatter, metaData); this.percents = percents; } @@ -103,7 +103,7 @@ public class PercentilesBucketPipelineAggregator extends BucketMetricsPipelineAg // todo need postCollection() to clean up temp sorted data? - return new InternalPercentilesBucket(name(), percents, percentiles, formatter, pipelineAggregators, metadata); + return new InternalPercentilesBucket(name(), percents, percentiles, format, pipelineAggregators, metadata); } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/InternalStatsBucket.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/InternalStatsBucket.java index fd326f50d50..84f8b265756 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/InternalStatsBucket.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/InternalStatsBucket.java @@ -20,11 +20,11 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.metrics.stats.InternalStats; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -47,7 +47,7 @@ public class InternalStatsBucket extends InternalStats implements StatsBucket { AggregationStreams.registerStream(STREAM, TYPE.stream()); } - public InternalStatsBucket(String name, long count, double sum, double min, double max, ValueFormatter formatter, + public InternalStatsBucket(String name, long count, double sum, double min, double max, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, count, sum, min, max, formatter, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/StatsBucketPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/StatsBucketPipelineAggregator.java index 41559640eac..8be8a8462be 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/StatsBucketPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/StatsBucketPipelineAggregator.java @@ -20,13 +20,13 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsPipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -55,7 +55,7 @@ public class StatsBucketPipelineAggregator extends BucketMetricsPipelineAggregat private double min = Double.POSITIVE_INFINITY; private double max = Double.NEGATIVE_INFINITY; - protected StatsBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, ValueFormatter formatter, + protected StatsBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, DocValueFormat formatter, Map metaData) { super(name, bucketsPaths, gapPolicy, formatter, metaData); } @@ -87,7 +87,7 @@ public class StatsBucketPipelineAggregator extends BucketMetricsPipelineAggregat @Override protected InternalAggregation buildAggregation(List pipelineAggregators, Map metadata) { - return new InternalStatsBucket(name(), count, sum, min, max, formatter, pipelineAggregators, metadata); + return new InternalStatsBucket(name(), count, sum, min, max, format, pipelineAggregators, metadata); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/ExtendedStatsBucketPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/ExtendedStatsBucketPipelineAggregator.java index 3f308465e37..69e8427143f 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/ExtendedStatsBucketPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/ExtendedStatsBucketPipelineAggregator.java @@ -20,13 +20,13 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsPipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -58,7 +58,7 @@ public class ExtendedStatsBucketPipelineAggregator extends BucketMetricsPipeline private double sigma; protected ExtendedStatsBucketPipelineAggregator(String name, String[] bucketsPaths, double sigma, GapPolicy gapPolicy, - ValueFormatter formatter, Map metaData) { + DocValueFormat formatter, Map metaData) { super(name, bucketsPaths, gapPolicy, formatter, metaData); this.sigma = sigma; } @@ -92,7 +92,7 @@ public class ExtendedStatsBucketPipelineAggregator extends BucketMetricsPipeline @Override protected InternalAggregation buildAggregation(List pipelineAggregators, Map metadata) { - return new InternalExtendedStatsBucket(name(), count, sum, min, max, sumOfSqrs, sigma, formatter, pipelineAggregators, metadata); + return new InternalExtendedStatsBucket(name(), count, sum, min, max, sumOfSqrs, sigma, format, pipelineAggregators, metadata); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/InternalExtendedStatsBucket.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/InternalExtendedStatsBucket.java index 469b184bcc7..548e6443299 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/InternalExtendedStatsBucket.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/InternalExtendedStatsBucket.java @@ -20,11 +20,11 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.metrics.stats.extended.InternalExtendedStats; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -48,7 +48,7 @@ public class InternalExtendedStatsBucket extends InternalExtendedStats implement } InternalExtendedStatsBucket(String name, long count, double sum, double min, double max, double sumOfSqrs, double sigma, - ValueFormatter formatter, List pipelineAggregators, + DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, count, sum, min, max, sumOfSqrs, sigma, formatter, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/sum/SumBucketPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/sum/SumBucketPipelineAggregator.java index b16cec211ee..edd912ee8ff 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/sum/SumBucketPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/sum/SumBucketPipelineAggregator.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics.sum; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; @@ -27,7 +28,6 @@ import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsPipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -55,7 +55,7 @@ public class SumBucketPipelineAggregator extends BucketMetricsPipelineAggregator private SumBucketPipelineAggregator() { } - protected SumBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, ValueFormatter formatter, + protected SumBucketPipelineAggregator(String name, String[] bucketsPaths, GapPolicy gapPolicy, DocValueFormat formatter, Map metaData) { super(name, bucketsPaths, gapPolicy, formatter, metaData); } @@ -77,7 +77,7 @@ public class SumBucketPipelineAggregator extends BucketMetricsPipelineAggregator @Override protected InternalAggregation buildAggregation(List pipelineAggregators, Map metadata) { - return new InternalSimpleValue(name(), sum, formatter, pipelineAggregators, metadata); + return new InternalSimpleValue(name(), sum, format, pipelineAggregators, metadata); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java index 2c1919f35eb..83cf5cbbf83 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java @@ -25,6 +25,7 @@ import org.elasticsearch.script.CompiledScript; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptContext; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; @@ -36,8 +37,6 @@ import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.ArrayList; @@ -64,7 +63,7 @@ public class BucketScriptPipelineAggregator extends PipelineAggregator { PipelineAggregatorStreams.registerStream(STREAM, TYPE.stream()); } - private ValueFormatter formatter; + private DocValueFormat formatter; private GapPolicy gapPolicy; private Script script; @@ -74,7 +73,7 @@ public class BucketScriptPipelineAggregator extends PipelineAggregator { public BucketScriptPipelineAggregator() { } - public BucketScriptPipelineAggregator(String name, Map bucketsPathsMap, Script script, ValueFormatter formatter, + public BucketScriptPipelineAggregator(String name, Map bucketsPathsMap, Script script, DocValueFormat formatter, GapPolicy gapPolicy, Map metadata) { super(name, bucketsPathsMap.values().toArray(new String[bucketsPathsMap.size()]), metadata); this.bucketsPathsMap = bucketsPathsMap; @@ -140,7 +139,7 @@ public class BucketScriptPipelineAggregator extends PipelineAggregator { @Override protected void doWriteTo(StreamOutput out) throws IOException { script.writeTo(out); - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(formatter); gapPolicy.writeTo(out); out.writeGenericValue(bucketsPathsMap); } @@ -149,7 +148,7 @@ public class BucketScriptPipelineAggregator extends PipelineAggregator { @Override protected void doReadFrom(StreamInput in) throws IOException { script = Script.readScript(in); - formatter = ValueFormatterStreams.readOptional(in); + formatter = in.readValueFormat(); gapPolicy = GapPolicy.readFrom(in); bucketsPathsMap = (Map) in.readGenericValue(); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregatorBuilder.java index 6d3e9e2ddd8..cdc17f19bb1 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregatorBuilder.java @@ -27,8 +27,7 @@ import org.elasticsearch.script.Script.ScriptField; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; +import org.elasticsearch.search.DocValueFormat; import java.io.IOException; import java.util.Collections; @@ -85,11 +84,11 @@ public class BucketScriptPipelineAggregatorBuilder extends PipelineAggregatorBui return format; } - protected ValueFormatter formatter() { + protected DocValueFormat formatter() { if (format != null) { - return ValueFormat.Patternable.Number.format(format).formatter(); + return new DocValueFormat.Decimal(format); } else { - return ValueFormatter.RAW; + return DocValueFormat.RAW; } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregator.java index 46727f165fd..e7f68338a1d 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregator.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.pipeline.cumulativesum; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; import org.elasticsearch.search.aggregations.InternalAggregation.Type; @@ -30,8 +31,6 @@ import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import java.io.IOException; import java.util.ArrayList; @@ -56,12 +55,12 @@ public class CumulativeSumPipelineAggregator extends PipelineAggregator { PipelineAggregatorStreams.registerStream(STREAM, TYPE.stream()); } - private ValueFormatter formatter; + private DocValueFormat formatter; public CumulativeSumPipelineAggregator() { } - public CumulativeSumPipelineAggregator(String name, String[] bucketsPaths, ValueFormatter formatter, + public CumulativeSumPipelineAggregator(String name, String[] bucketsPaths, DocValueFormat formatter, Map metadata) { super(name, bucketsPaths, metadata); this.formatter = formatter; @@ -96,11 +95,11 @@ public class CumulativeSumPipelineAggregator extends PipelineAggregator { @Override public void doReadFrom(StreamInput in) throws IOException { - formatter = ValueFormatterStreams.readOptional(in); + formatter = in.readValueFormat(); } @Override public void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(formatter); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregatorBuilder.java index ae50998d290..af772cc943a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregatorBuilder.java @@ -22,13 +22,12 @@ package org.elasticsearch.search.aggregations.pipeline.cumulativesum; 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.AggregatorFactory; import org.elasticsearch.search.aggregations.bucket.histogram.AbstractHistogramAggregatorFactory; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsParser; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -67,11 +66,11 @@ public class CumulativeSumPipelineAggregatorBuilder extends PipelineAggregatorBu return format; } - protected ValueFormatter formatter() { + protected DocValueFormat formatter() { if (format != null) { - return ValueFormat.Patternable.Number.format(format).formatter(); + return new DocValueFormat.Decimal(format); } else { - return ValueFormatter.RAW; + return DocValueFormat.RAW; } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/DerivativePipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/DerivativePipelineAggregator.java index cea99cf868e..ff0b1d97d0d 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/DerivativePipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/DerivativePipelineAggregator.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.pipeline.derivative; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; @@ -30,8 +31,6 @@ import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import org.joda.time.DateTime; import java.io.IOException; @@ -60,14 +59,14 @@ public class DerivativePipelineAggregator extends PipelineAggregator { PipelineAggregatorStreams.registerStream(STREAM, TYPE.stream()); } - private ValueFormatter formatter; + private DocValueFormat formatter; private GapPolicy gapPolicy; private Double xAxisUnits; public DerivativePipelineAggregator() { } - public DerivativePipelineAggregator(String name, String[] bucketsPaths, ValueFormatter formatter, GapPolicy gapPolicy, Long xAxisUnits, + public DerivativePipelineAggregator(String name, String[] bucketsPaths, DocValueFormat formatter, GapPolicy gapPolicy, Long xAxisUnits, Map metadata) { super(name, bucketsPaths, metadata); this.formatter = formatter; @@ -128,7 +127,7 @@ public class DerivativePipelineAggregator extends PipelineAggregator { @Override public void doReadFrom(StreamInput in) throws IOException { - formatter = ValueFormatterStreams.readOptional(in); + formatter = in.readValueFormat(); gapPolicy = GapPolicy.readFrom(in); if (in.readBoolean()) { xAxisUnits = in.readDouble(); @@ -140,7 +139,7 @@ public class DerivativePipelineAggregator extends PipelineAggregator { @Override public void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(formatter); gapPolicy.writeTo(out); boolean hasXAxisUnitsValue = xAxisUnits != null; out.writeBoolean(hasXAxisUnitsValue); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/DerivativePipelineAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/DerivativePipelineAggregatorBuilder.java index dd7de1e19c3..ffcc91c51dc 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/DerivativePipelineAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/DerivativePipelineAggregatorBuilder.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.rounding.DateTimeUnit; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.bucket.histogram.AbstractHistogramAggregatorFactory; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregatorFactory; @@ -31,8 +32,6 @@ import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInter import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -101,11 +100,11 @@ public class DerivativePipelineAggregatorBuilder extends PipelineAggregatorBuild @Override protected PipelineAggregator createInternal(Map metaData) throws IOException { - ValueFormatter formatter; + DocValueFormat formatter; if (format != null) { - formatter = ValueFormat.Patternable.Number.format(format).formatter(); + formatter = new DocValueFormat.Decimal(format); } else { - formatter = ValueFormatter.RAW; + formatter = DocValueFormat.RAW; } Long xAxisUnits = null; if (units != null) { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivative.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivative.java index f36dfbe7d94..24579d65a30 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivative.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/derivative/InternalDerivative.java @@ -22,10 +22,10 @@ package org.elasticsearch.search.aggregations.pipeline.derivative; 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.pipeline.InternalSimpleValue; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -53,7 +53,7 @@ public class InternalDerivative extends InternalSimpleValue implements Derivativ InternalDerivative() { } - public InternalDerivative(String name, double value, double normalizationFactor, ValueFormatter formatter, List pipelineAggregators, + public InternalDerivative(String name, double value, double normalizationFactor, DocValueFormat formatter, List pipelineAggregators, Map metaData) { super(name, value, formatter, pipelineAggregators, metaData); this.normalizationFactor = normalizationFactor; @@ -101,8 +101,8 @@ public class InternalDerivative extends InternalSimpleValue implements Derivativ if (normalizationFactor > 0) { boolean hasValue = !(Double.isInfinite(normalizedValue()) || Double.isNaN(normalizedValue())); builder.field("normalized_value", hasValue ? normalizedValue() : null); - if (hasValue && !(valueFormatter instanceof ValueFormatter.Raw)) { - builder.field("normalized_value_as_string", valueFormatter.format(normalizedValue())); + if (hasValue && format != DocValueFormat.RAW) { + builder.field("normalized_value_as_string", format.format(normalizedValue())); } } return builder; diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/movavg/MovAvgPipelineAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/movavg/MovAvgPipelineAggregator.java index d5d9d040d73..08c7ee64680 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/movavg/MovAvgPipelineAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/movavg/MovAvgPipelineAggregator.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.aggregations.pipeline.movavg; import org.elasticsearch.common.collect.EvictingQueue; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; @@ -34,8 +35,6 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams; import org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel; import org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModelStreams; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; -import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams; import org.joda.time.DateTime; import java.io.IOException; @@ -65,7 +64,7 @@ public class MovAvgPipelineAggregator extends PipelineAggregator { PipelineAggregatorStreams.registerStream(STREAM, TYPE.stream()); } - private ValueFormatter formatter; + private DocValueFormat formatter; private GapPolicy gapPolicy; private int window; private MovAvgModel model; @@ -75,7 +74,7 @@ public class MovAvgPipelineAggregator extends PipelineAggregator { public MovAvgPipelineAggregator() { } - public MovAvgPipelineAggregator(String name, String[] bucketsPaths, ValueFormatter formatter, GapPolicy gapPolicy, + public MovAvgPipelineAggregator(String name, String[] bucketsPaths, DocValueFormat formatter, GapPolicy gapPolicy, int window, int predict, MovAvgModel model, boolean minimize, Map metadata) { super(name, bucketsPaths, metadata); this.formatter = formatter; @@ -152,7 +151,7 @@ public class MovAvgPipelineAggregator extends PipelineAggregator { if (buckets.size() > 0 && predict > 0) { boolean keyed; - ValueFormatter formatter; + DocValueFormat formatter; keyed = buckets.get(0).getKeyed(); formatter = buckets.get(0).getFormatter(); @@ -251,7 +250,7 @@ public class MovAvgPipelineAggregator extends PipelineAggregator { @Override public void doReadFrom(StreamInput in) throws IOException { - formatter = ValueFormatterStreams.readOptional(in); + formatter = in.readValueFormat(); gapPolicy = GapPolicy.readFrom(in); window = in.readVInt(); predict = in.readVInt(); @@ -262,7 +261,7 @@ public class MovAvgPipelineAggregator extends PipelineAggregator { @Override public void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(formatter); gapPolicy.writeTo(out); out.writeVInt(window); out.writeVInt(predict); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/movavg/MovAvgPipelineAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/movavg/MovAvgPipelineAggregatorBuilder.java index 8f6d116c5f1..2decd08954e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/movavg/MovAvgPipelineAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/movavg/MovAvgPipelineAggregatorBuilder.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.aggregations.pipeline.movavg; 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.AggregatorFactory; import org.elasticsearch.search.aggregations.bucket.histogram.AbstractHistogramAggregatorFactory; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; @@ -31,8 +32,6 @@ import org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel; import org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModelBuilder; import org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModelStreams; import org.elasticsearch.search.aggregations.pipeline.movavg.models.SimpleModel; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.List; @@ -94,11 +93,11 @@ public class MovAvgPipelineAggregatorBuilder extends PipelineAggregatorBuilder metadata) { super(name, bucketsPaths, metadata); this.formatter = formatter; @@ -129,14 +128,14 @@ public class SerialDiffPipelineAggregator extends PipelineAggregator { @Override public void doReadFrom(StreamInput in) throws IOException { - formatter = ValueFormatterStreams.readOptional(in); + formatter = in.readValueFormat(); gapPolicy = GapPolicy.readFrom(in); lag = in.readVInt(); } @Override public void doWriteTo(StreamOutput out) throws IOException { - ValueFormatterStreams.writeOptional(formatter, out); + out.writeValueFormat(formatter); gapPolicy.writeTo(out); out.writeVInt(lag); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/serialdiff/SerialDiffPipelineAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/serialdiff/SerialDiffPipelineAggregatorBuilder.java index ad7b83442e2..5e154921b75 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/serialdiff/SerialDiffPipelineAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/serialdiff/SerialDiffPipelineAggregatorBuilder.java @@ -24,9 +24,8 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import java.io.IOException; import java.util.Map; @@ -102,11 +101,11 @@ public class SerialDiffPipelineAggregatorBuilder extends PipelineAggregatorBuild return gapPolicy; } - protected ValueFormatter formatter() { + protected DocValueFormat formatter() { if (format != null) { - return ValueFormat.Patternable.Number.format(format).formatter(); + return new DocValueFormat.Decimal(format); } else { - return ValueFormatter.RAW; + return DocValueFormat.RAW; } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java b/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java index 14e84818d50..1ee72d2b572 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java @@ -29,10 +29,10 @@ import org.elasticsearch.index.fielddata.IndexGeoPointFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.fielddata.IndexOrdinalsFieldData; import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData; -import org.elasticsearch.index.mapper.core.DateFieldMapper; import org.elasticsearch.search.SearchParseException; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.internal.SearchContext; +import org.joda.time.DateTimeZone; import java.io.IOException; @@ -99,19 +99,11 @@ public class AggregationContext { if (config.missing instanceof Number) { missing = (Number) config.missing; } else { - if (config.fieldContext != null && config.fieldContext.fieldType() instanceof DateFieldMapper.DateFieldType) { - final DateFieldMapper.DateFieldType fieldType = (DateFieldMapper.DateFieldType) config.fieldContext.fieldType(); - try { - missing = fieldType.dateTimeFormatter().parser().parseDateTime(config.missing.toString()).getMillis(); - } catch (IllegalArgumentException e) { - throw new SearchParseException(context, "Expected a date value in [missing] but got [" + config.missing + "]", null, e); - } + if (config.fieldContext != null && config.fieldContext.fieldType() != null) { + missing = config.fieldContext.fieldType().docValueFormat(null, DateTimeZone.UTC) + .parseDouble(config.missing.toString(), false, context.nowCallable()); } else { - try { - missing = Double.parseDouble(config.missing.toString()); - } catch (NumberFormatException e) { - throw new SearchParseException(context, "Expected a numeric value in [missing] but got [" + config.missing + "]", null, e); - } + missing = Double.parseDouble(config.missing.toString()); } } return (VS) MissingValues.replaceMissing((ValuesSource.Numeric) vs, missing); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java b/core/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java index 688e447cf58..13ed5e6435d 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java @@ -25,7 +25,9 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexGeoPointFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; +import org.elasticsearch.index.mapper.core.DateFieldMapper; +import org.elasticsearch.search.DocValueFormat; +import org.joda.time.DateTimeZone; import java.io.IOException; @@ -34,20 +36,17 @@ import java.io.IOException; */ public enum ValueType implements Writeable { - @Deprecated - ANY((byte) 0, "any", "any", ValuesSourceType.ANY, IndexFieldData.class, ValueFormat.RAW), STRING((byte) 1, "string", "string", ValuesSourceType.BYTES, - IndexFieldData.class, - ValueFormat.RAW), + IndexFieldData.class, DocValueFormat.RAW), LONG((byte) 2, "byte|short|integer|long", "long", ValuesSourceType.NUMERIC, - IndexNumericFieldData.class, ValueFormat.RAW) { + IndexNumericFieldData.class, DocValueFormat.RAW) { @Override public boolean isNumeric() { return true; } }, - DOUBLE((byte) 3, "float|double", "double", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, ValueFormat.RAW) { + DOUBLE((byte) 3, "float|double", "double", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, DocValueFormat.RAW) { @Override public boolean isNumeric() { return true; @@ -58,31 +57,32 @@ public enum ValueType implements Writeable { return true; } }, - NUMBER((byte) 4, "number", "number", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, ValueFormat.RAW) { + NUMBER((byte) 4, "number", "number", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, DocValueFormat.RAW) { @Override public boolean isNumeric() { return true; } }, - DATE((byte) 5, "date", "date", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, ValueFormat.DateTime.DEFAULT) { + DATE((byte) 5, "date", "date", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, + new DocValueFormat.DateTime(DateFieldMapper.Defaults.DATE_TIME_FORMATTER, DateTimeZone.UTC)) { @Override public boolean isNumeric() { return true; } }, - IP((byte) 6, "ip", "ip", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, ValueFormat.IPv4) { + IP((byte) 6, "ip", "ip", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, DocValueFormat.IP) { @Override public boolean isNumeric() { return true; } }, - NUMERIC((byte) 7, "numeric", "numeric", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, ValueFormat.RAW) { + NUMERIC((byte) 7, "numeric", "numeric", ValuesSourceType.NUMERIC, IndexNumericFieldData.class, DocValueFormat.RAW) { @Override public boolean isNumeric() { return true; } }, - GEOPOINT((byte) 8, "geo_point", "geo_point", ValuesSourceType.GEOPOINT, IndexGeoPointFieldData.class, ValueFormat.RAW) { + GEOPOINT((byte) 8, "geo_point", "geo_point", ValuesSourceType.GEOPOINT, IndexGeoPointFieldData.class, DocValueFormat.GEOHASH) { @Override public boolean isGeoPoint() { return true; @@ -92,12 +92,12 @@ public enum ValueType implements Writeable { final String description; final ValuesSourceType valuesSourceType; final Class fieldDataType; - final ValueFormat defaultFormat; + final DocValueFormat defaultFormat; private final byte id; private String preferredName; private ValueType(byte id, String description, String preferredName, ValuesSourceType valuesSourceType, Class fieldDataType, - ValueFormat defaultFormat) { + DocValueFormat defaultFormat) { this.id = id; this.description = description; this.preferredName = preferredName; @@ -131,7 +131,7 @@ public enum ValueType implements Writeable { return !isA(valueType); } - public ValueFormat defaultFormat() { + public DocValueFormat defaultFormat() { return defaultFormat; } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregatorBuilder.java index fc06ed94f80..ea9f84dc9b7 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregatorBuilder.java @@ -26,20 +26,16 @@ import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexGeoPointFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.core.BooleanFieldMapper; -import org.elasticsearch.index.mapper.core.DateFieldMapper; -import org.elasticsearch.index.mapper.core.NumberFieldMapper; -import org.elasticsearch.index.mapper.ip.IpFieldMapper; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.SearchScript; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationInitializationException; import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; import org.elasticsearch.search.aggregations.AggregatorBuilder; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.InternalAggregation.Type; -import org.elasticsearch.search.aggregations.support.format.ValueFormat; import org.elasticsearch.search.internal.SearchContext; import org.joda.time.DateTimeZone; @@ -73,7 +69,7 @@ public abstract class ValuesSourceAggregatorBuilder config; protected ValuesSourceAggregatorBuilder(String name, Type type, ValuesSourceType valuesSourceType, ValueType targetValueType) { @@ -279,7 +275,7 @@ public abstract class ValuesSourceAggregatorBuilder { ValueType scriptValueType; boolean unmapped = false; String formatPattern; - ValueFormat format = ValueFormat.RAW; + DocValueFormat format = DocValueFormat.RAW; Object missing; DateTimeZone timeZone; @@ -78,15 +76,7 @@ public class ValuesSourceConfig { return this; } - public ValueFormat format() { + public DocValueFormat format() { return format; } - - public ValueFormatter formatter() { - return format.formatter(); - } - - public ValueParser parser() { - return format.parser(); - } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormat.java b/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormat.java deleted file mode 100644 index 9a3be56517c..00000000000 --- a/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormat.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.support.format; - -import org.elasticsearch.index.mapper.core.DateFieldMapper; -import org.joda.time.DateTimeZone; - -/** - * - */ -public class ValueFormat { - - public static final ValueFormat RAW = new ValueFormat(ValueFormatter.RAW, ValueParser.RAW); - public static final ValueFormat IPv4 = new ValueFormat(ValueFormatter.IPv4, ValueParser.IPv4); - public static final ValueFormat BOOLEAN = new ValueFormat(ValueFormatter.BOOLEAN, ValueParser.BOOLEAN); - - private final ValueFormatter formatter; - private final ValueParser parser; - - public ValueFormat(ValueFormatter formatter, ValueParser parser) { - assert formatter != null && parser != null; - this.formatter = formatter; - this.parser = parser; - } - - public ValueFormatter formatter() { - return formatter; - } - - public ValueParser parser() { - return parser; - } - - public abstract static class Patternable> extends ValueFormat { - - private final String pattern; - - public Patternable(String pattern, ValueFormatter formatter, ValueParser parser) { - super(formatter, parser); - this.pattern = pattern; - } - - public String pattern() { - return pattern; - } - - public abstract VF create(String pattern); - } - - public static class DateTime extends Patternable { - - public static final DateTime DEFAULT = new DateTime(DateFieldMapper.Defaults.DATE_TIME_FORMATTER.format(), ValueFormatter.DateTime.DEFAULT, ValueParser.DateMath.DEFAULT); - - public static DateTime format(String format, DateTimeZone timezone) { - return new DateTime(format, new ValueFormatter.DateTime(format, timezone), new ValueParser.DateMath(format, timezone)); - } - - public static DateTime mapper(DateFieldMapper.DateFieldType fieldType, DateTimeZone timezone) { - return new DateTime(fieldType.dateTimeFormatter().format(), ValueFormatter.DateTime.mapper(fieldType, timezone), ValueParser.DateMath.mapper(fieldType, timezone)); - } - - private DateTime(String pattern, ValueFormatter formatter, ValueParser parser) { - super(pattern, formatter, parser); - } - - @Override - public DateTime create(String pattern) { - return format(pattern, DateTimeZone.UTC); - } - } - - public static class Number extends Patternable { - - public static Number format(String format) { - return new Number(format, new ValueFormatter.Number.Pattern(format), new ValueParser.Number.Pattern(format)); - } - - public Number(String pattern, ValueFormatter formatter, ValueParser parser) { - super(pattern, formatter, parser); - } - - @Override - public Number create(String pattern) { - return format(pattern); - } - } - -} diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormatter.java b/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormatter.java deleted file mode 100644 index 555256b6810..00000000000 --- a/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormatter.java +++ /dev/null @@ -1,299 +0,0 @@ -/* - * 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.support.format; - -import org.apache.lucene.spatial.util.GeoHashUtils; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Streamable; -import org.elasticsearch.common.joda.FormatDateTimeFormatter; -import org.elasticsearch.common.joda.Joda; -import org.elasticsearch.index.mapper.core.DateFieldMapper; -import org.elasticsearch.index.mapper.ip.IpFieldMapper; -import org.joda.time.DateTimeZone; - -import java.io.IOException; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A strategy for formatting time represented as millis long value to string - */ -public interface ValueFormatter extends Streamable { - - public final static ValueFormatter RAW = new Raw(); - public final static ValueFormatter IPv4 = new IPv4Formatter(); - public final static ValueFormatter GEOHASH = new GeoHash(); - public final static ValueFormatter BOOLEAN = new BooleanFormatter(); - - /** - * Uniquely identifies this formatter (used for efficient serialization) - * - * @return The id of this formatter - */ - byte id(); - - /** - * Formats the given millis time value (since the epoch) to string. - * - * @param value The long value to format. - * @return The formatted value as string. - */ - String format(long value); - - /** - * @param value double The double value to format. - * @return The formatted value as string - */ - String format(double value); - - - static class Raw implements ValueFormatter { - - static final byte ID = 1; - - @Override - public String format(long value) { - return String.valueOf(value); - } - - @Override - public String format(double value) { - return String.valueOf(value); - } - - @Override - public byte id() { - return ID; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - } - } - - /** - * A time formatter which is based on date/time format. - */ - public static class DateTime implements ValueFormatter { - - public static final ValueFormatter DEFAULT = new ValueFormatter.DateTime(DateFieldMapper.Defaults.DATE_TIME_FORMATTER); - private DateTimeZone timeZone = DateTimeZone.UTC; - - public static DateTime mapper(DateFieldMapper.DateFieldType fieldType, DateTimeZone timezone) { - return new DateTime(fieldType.dateTimeFormatter(), timezone); - } - - static final byte ID = 2; - - FormatDateTimeFormatter formatter; - - DateTime() {} // for serialization - - public DateTime(String format) { - this.formatter = Joda.forPattern(format); - } - - public DateTime(FormatDateTimeFormatter formatter) { - this.formatter = formatter; - } - - public DateTime(String format, DateTimeZone timezone) { - this.formatter = Joda.forPattern(format); - this.timeZone = timezone != null ? timezone : DateTimeZone.UTC; - } - - public DateTime(FormatDateTimeFormatter formatter, DateTimeZone timezone) { - this.formatter = formatter; - this.timeZone = timezone != null ? timezone : DateTimeZone.UTC; - } - - @Override - public String format(long time) { - return formatter.printer().withZone(timeZone).print(time); - } - - @Override - public String format(double value) { - return format((long) value); - } - - @Override - public byte id() { - return ID; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - formatter = Joda.forPattern(in.readString()); - timeZone = DateTimeZone.forID(in.readString()); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeString(formatter.format()); - out.writeString(timeZone.getID()); - } - } - - public static abstract class Number implements ValueFormatter { - - NumberFormat format; - - Number() {} // for serialization - - Number(NumberFormat format) { - this.format = format; - } - - @Override - public String format(long value) { - return format.format(value); - } - - @Override - public String format(double value) { - return format.format(value); - } - - public static class Pattern extends Number { - - private static final DecimalFormatSymbols SYMBOLS = new DecimalFormatSymbols(Locale.ROOT); - - static final byte ID = 4; - - String pattern; - - Pattern() {} // for serialization - - public Pattern(String pattern) { - super(new DecimalFormat(pattern, SYMBOLS)); - this.pattern = pattern; - } - - @Override - public byte id() { - return ID; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - pattern = in.readString(); - format = new DecimalFormat(pattern, SYMBOLS); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeString(pattern); - } - } - } - - static class IPv4Formatter implements ValueFormatter { - - static final byte ID = 6; - - @Override - public byte id() { - return ID; - } - - @Override - public String format(long value) { - return IpFieldMapper.longToIp(value); - } - - @Override - public String format(double value) { - return format((long) value); - } - - @Override - public void readFrom(StreamInput in) throws IOException { - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - } - } - - static class GeoHash implements ValueFormatter { - - static final byte ID = 8; - - @Override - public byte id() { - return ID; - } - - @Override - public String format(long value) { - return GeoHashUtils.stringEncode(value); - } - - @Override - public String format(double value) { - return format((long) value); - } - - @Override - public void readFrom(StreamInput in) throws IOException { - - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - - } - } - - static class BooleanFormatter implements ValueFormatter { - - static final byte ID = 10; - - @Override - public byte id() { - return ID; - } - - @Override - public String format(long value) { - return Boolean.valueOf(value != 0).toString(); - } - - @Override - public String format(double value) { - return Boolean.valueOf(value != 0).toString(); - } - - @Override - public void readFrom(StreamInput in) throws IOException { - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - } - } -} diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormatterStreams.java b/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormatterStreams.java deleted file mode 100644 index 97699c37b21..00000000000 --- a/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueFormatterStreams.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.support.format; - -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; - -import java.io.IOException; - -/** - * - */ -public class ValueFormatterStreams { - - public static ValueFormatter read(StreamInput in) throws IOException { - byte id = in.readByte(); - ValueFormatter formatter = null; - switch (id) { - case ValueFormatter.Raw.ID: return ValueFormatter.RAW; - case ValueFormatter.IPv4Formatter.ID: return ValueFormatter.IPv4; - case ValueFormatter.DateTime.ID: formatter = new ValueFormatter.DateTime(); break; - case ValueFormatter.Number.Pattern.ID: formatter = new ValueFormatter.Number.Pattern(); break; - case ValueFormatter.GeoHash.ID: formatter = ValueFormatter.GEOHASH; break; - case ValueFormatter.BooleanFormatter.ID: formatter = ValueFormatter.BOOLEAN; break; - default: throw new IllegalArgumentException("Unknown value formatter with id [" + id + "]"); - } - formatter.readFrom(in); - return formatter; - } - - public static ValueFormatter readOptional(StreamInput in) throws IOException { - if (!in.readBoolean()) { - return null; - } - return read(in); - } - - public static void write(ValueFormatter formatter, StreamOutput out) throws IOException { - out.writeByte(formatter.id()); - formatter.writeTo(out); - } - - public static void writeOptional(ValueFormatter formatter, StreamOutput out) throws IOException { - out.writeBoolean(formatter != null); - if (formatter != null) { - write(formatter, out); - } - } -} diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueParser.java deleted file mode 100644 index acd88f70201..00000000000 --- a/core/src/main/java/org/elasticsearch/search/aggregations/support/format/ValueParser.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * 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.support.format; - -import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.joda.DateMathParser; -import org.elasticsearch.common.joda.FormatDateTimeFormatter; -import org.elasticsearch.common.joda.Joda; -import org.elasticsearch.index.mapper.core.DateFieldMapper; -import org.elasticsearch.index.mapper.ip.IpFieldMapper; -import org.elasticsearch.search.aggregations.AggregationExecutionException; -import org.elasticsearch.search.internal.SearchContext; -import org.joda.time.DateTimeZone; - -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.text.NumberFormat; -import java.text.ParseException; -import java.util.Locale; -import java.util.concurrent.Callable; - -/** - * - */ -public interface ValueParser { - - static final ValueParser IPv4 = new IPv4(); - static final ValueParser RAW = new Raw(); - static final ValueParser BOOLEAN = new Boolean(); - - long parseLong(String value, SearchContext searchContext); - - double parseDouble(String value, SearchContext searchContext); - - - /** - * Knows how to parse datatime values based on date/time format - */ - static class DateTime implements ValueParser { - - public static final DateTime DEFAULT = new DateTime(DateFieldMapper.Defaults.DATE_TIME_FORMATTER); - - private FormatDateTimeFormatter formatter; - - public DateTime(String format) { - this(Joda.forPattern(format)); - } - - public DateTime(FormatDateTimeFormatter formatter) { - this.formatter = formatter; - } - - @Override - public long parseLong(String value, SearchContext searchContext) { - return formatter.parser().parseMillis(value); - } - - @Override - public double parseDouble(String value, SearchContext searchContext) { - return parseLong(value, searchContext); - } - } - - /** - * Knows how to parse datatime values based on elasticsearch's date math expression - */ - static class DateMath implements ValueParser { - - public static final DateMath DEFAULT = new ValueParser.DateMath(new DateMathParser(DateFieldMapper.Defaults.DATE_TIME_FORMATTER), DateTimeZone.UTC); - - private DateMathParser parser; - - private DateTimeZone timezone = DateTimeZone.UTC; - - public DateMath(String format, DateTimeZone timezone) { - this(new DateMathParser(Joda.forPattern(format)), timezone); - } - - public DateMath(DateMathParser parser, @Nullable DateTimeZone timeZone) { - this.parser = parser; - if (timeZone != null) { - this.timezone = timeZone; - } - } - - @Override - public long parseLong(String value, final SearchContext searchContext) { - final Callable now = new Callable() { - @Override - public Long call() throws Exception { - return searchContext.nowInMillis(); - } - }; - return parser.parse(value, now, false, timezone); - } - - @Override - public double parseDouble(String value, SearchContext searchContext) { - return parseLong(value, searchContext); - } - - public static DateMath mapper(DateFieldMapper.DateFieldType fieldType, @Nullable DateTimeZone timezone) { - return new DateMath(new DateMathParser(fieldType.dateTimeFormatter()), timezone); - } - } - - /** - * Knows how to parse IPv4 formats - */ - static class IPv4 implements ValueParser { - - private IPv4() { - } - - @Override - public long parseLong(String value, SearchContext searchContext) { - return IpFieldMapper.ipToLong(value); - } - - @Override - public double parseDouble(String value, SearchContext searchContext) { - return parseLong(value, searchContext); - } - } - - static class Raw implements ValueParser { - - private Raw() { - } - - @Override - public long parseLong(String value, SearchContext searchContext) { - return Long.parseLong(value); - } - - @Override - public double parseDouble(String value, SearchContext searchContext) { - return Double.parseDouble(value); - } - } - - public static abstract class Number implements ValueParser { - - NumberFormat format; - - Number(NumberFormat format) { - this.format = format; - } - - public static class Pattern extends Number { - - private static final DecimalFormatSymbols SYMBOLS = new DecimalFormatSymbols(Locale.ROOT); - - public Pattern(String pattern) { - super(new DecimalFormat(pattern, SYMBOLS)); - } - - @Override - public long parseLong(String value, SearchContext searchContext) { - try { - return format.parse(value).longValue(); - } catch (ParseException nfe) { - throw new AggregationExecutionException("Invalid number format [" + ((DecimalFormat) format).toPattern() + "]"); - } - } - - @Override - public double parseDouble(String value, SearchContext searchContext) { - try { - return format.parse(value).doubleValue(); - } catch (ParseException nfe) { - throw new AggregationExecutionException("Invalid number format [" + ((DecimalFormat) format).toPattern() + "]"); - } - } - } - } - - static class Boolean implements ValueParser { - - private Boolean() { - } - - @Override - public long parseLong(String value, SearchContext searchContext) { - return java.lang.Boolean.parseBoolean(value) ? 1 : 0; - } - - @Override - public double parseDouble(String value, SearchContext searchContext) { - return parseLong(value, searchContext); - } - } - -} diff --git a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java index f9d0b6b9283..3c71d9e2599 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -65,6 +65,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicBoolean; public abstract class SearchContext implements Releasable { @@ -145,6 +146,15 @@ public abstract class SearchContext implements Releasable { return nowInMillisImpl(); } + public final Callable nowCallable() { + return new Callable() { + @Override + public Long call() throws Exception { + return nowInMillis(); + } + }; + }; + public final boolean nowInMillisUsed() { return nowInMillisUsed; } diff --git a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index 0275ff1200a..1ac67cdf468 100644 --- a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java +++ b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -22,9 +22,13 @@ package org.elasticsearch.common.io.stream; import org.apache.lucene.util.Constants; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.geo.GeoPoint; +import org.elasticsearch.common.joda.FormatDateTimeFormatter; +import org.elasticsearch.common.joda.Joda; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.test.ESTestCase; +import org.joda.time.DateTimeZone; import java.io.IOException; import java.util.Objects; @@ -520,4 +524,5 @@ public class BytesStreamsTests extends ESTestCase { assertEquals(point, geoPoint); } } + } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/BooleanFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/BooleanFieldTypeTests.java index 0800e4ae3d9..ae29b96981a 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/BooleanFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/BooleanFieldTypeTests.java @@ -32,4 +32,10 @@ public class BooleanFieldTypeTests extends FieldTypeTestCase { public void setupProperties() { setDummyNullValue(true); } + + public void testValueFormat() { + MappedFieldType ft = createDefaultFieldType(); + assertEquals("false", ft.docValueFormat(null, null).format(0)); + assertEquals("true", ft.docValueFormat(null, null).format(1)); + } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java index 089533023d8..96ebf5b7603 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/core/DateFieldTypeTests.java @@ -28,7 +28,6 @@ import org.apache.lucene.index.MultiReader; import org.apache.lucene.store.Directory; import org.apache.lucene.util.IOUtils; import org.elasticsearch.common.joda.DateMathParser; -import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.Joda; import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; @@ -124,4 +123,21 @@ public class DateFieldTypeTests extends FieldTypeTestCase { doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, alternateFormat); IOUtils.close(reader, w, dir); } + + public void testValueFormat() { + MappedFieldType ft = createDefaultFieldType(); + long instant = DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime("2015-10-12T14:10:55").getMillis(); + assertEquals("2015-10-12T14:10:55.000Z", + ft.docValueFormat(null, DateTimeZone.UTC).format(instant)); + assertEquals("2015-10-12T15:10:55.000+01:00", + ft.docValueFormat(null, DateTimeZone.forOffsetHours(1)).format(instant)); + assertEquals("2015", + createDefaultFieldType().docValueFormat("YYYY", DateTimeZone.UTC).format(instant)); + assertEquals(instant, + ft.docValueFormat(null, DateTimeZone.UTC).parseLong("2015-10-12T14:10:55", false, null)); + assertEquals(instant, + ft.docValueFormat(null, DateTimeZone.UTC).parseLong("2015-10-12T14:10:55", true, null)); + assertEquals(DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime("2015-10-13").getMillis() - 1, + ft.docValueFormat(null, DateTimeZone.UTC).parseLong("2015-10-12||/d", true, null)); + } } diff --git a/core/src/test/java/org/elasticsearch/search/DocValueFormatTests.java b/core/src/test/java/org/elasticsearch/search/DocValueFormatTests.java new file mode 100644 index 00000000000..218ccc4f20c --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/DocValueFormatTests.java @@ -0,0 +1,79 @@ +/* + * 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; + +import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.joda.Joda; +import org.elasticsearch.test.ESTestCase; +import org.joda.time.DateTimeZone; + +public class DocValueFormatTests extends ESTestCase { + + public void testSerialization() throws Exception { + NamedWriteableRegistry registry = new NamedWriteableRegistry(); + registry.register(DocValueFormat.class, DocValueFormat.BOOLEAN.getWriteableName(), in -> DocValueFormat.BOOLEAN); + registry.register(DocValueFormat.class, DocValueFormat.DateTime.NAME, DocValueFormat.DateTime::new); + registry.register(DocValueFormat.class, DocValueFormat.Decimal.NAME, DocValueFormat.Decimal::new); + registry.register(DocValueFormat.class, DocValueFormat.GEOHASH.getWriteableName(), in -> DocValueFormat.GEOHASH); + registry.register(DocValueFormat.class, DocValueFormat.IP.getWriteableName(), in -> DocValueFormat.IP); + registry.register(DocValueFormat.class, DocValueFormat.RAW.getWriteableName(), in -> DocValueFormat.RAW); + + BytesStreamOutput out = new BytesStreamOutput(); + out.writeValueFormat(DocValueFormat.BOOLEAN); + StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes()), registry); + assertSame(DocValueFormat.BOOLEAN, in.readValueFormat()); + + DocValueFormat.Decimal decimalFormat = new DocValueFormat.Decimal("###.##"); + out = new BytesStreamOutput(); + out.writeValueFormat(decimalFormat); + in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes()), registry); + DocValueFormat vf = in.readValueFormat(); + assertEquals(DocValueFormat.Decimal.class, vf.getClass()); + assertEquals("###.##", ((DocValueFormat.Decimal) vf).pattern); + + DocValueFormat.DateTime dateFormat = new DocValueFormat.DateTime(Joda.forPattern("epoch_second"), DateTimeZone.forOffsetHours(1)); + out = new BytesStreamOutput(); + out.writeValueFormat(dateFormat); + in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes()), registry); + vf = in.readValueFormat(); + assertEquals(DocValueFormat.DateTime.class, vf.getClass()); + assertEquals("epoch_second", ((DocValueFormat.DateTime) vf).formatter.format()); + assertEquals(DateTimeZone.forOffsetHours(1), ((DocValueFormat.DateTime) vf).timeZone); + + out = new BytesStreamOutput(); + out.writeValueFormat(DocValueFormat.GEOHASH); + in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes()), registry); + assertSame(DocValueFormat.GEOHASH, in.readValueFormat()); + + out = new BytesStreamOutput(); + out.writeValueFormat(DocValueFormat.IP); + in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes()), registry); + assertSame(DocValueFormat.IP, in.readValueFormat()); + + out = new BytesStreamOutput(); + out.writeValueFormat(DocValueFormat.RAW); + in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes()), registry); + assertSame(DocValueFormat.RAW, in.readValueFormat()); + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java index c9cd332ec33..d245cd7bcc6 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java @@ -281,7 +281,7 @@ public class SignificantTermsSignificanceScoreIT extends ESIntegTestCase { classes.toXContent(responseBuilder, null); String result = null; if (type.equals("long")) { - result = "\"class\"{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":4,\"sig_terms\":{\"doc_count\":4,\"buckets\":[{\"key\":0,\"key_as_string\":\"0\",\"doc_count\":4,\"score\":0.39999999999999997,\"bg_count\":5}]}},{\"key\":\"1\",\"doc_count\":3,\"sig_terms\":{\"doc_count\":3,\"buckets\":[{\"key\":1,\"key_as_string\":\"1\",\"doc_count\":3,\"score\":0.75,\"bg_count\":4}]}}]}"; + result = "\"class\"{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":4,\"sig_terms\":{\"doc_count\":4,\"buckets\":[{\"key\":0,\"doc_count\":4,\"score\":0.39999999999999997,\"bg_count\":5}]}},{\"key\":\"1\",\"doc_count\":3,\"sig_terms\":{\"doc_count\":3,\"buckets\":[{\"key\":1,\"doc_count\":3,\"score\":0.75,\"bg_count\":4}]}}]}"; } else { result = "\"class\"{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"0\",\"doc_count\":4,\"sig_terms\":{\"doc_count\":4,\"buckets\":[{\"key\":\"0\",\"doc_count\":4,\"score\":0.39999999999999997,\"bg_count\":5}]}},{\"key\":\"1\",\"doc_count\":3,\"sig_terms\":{\"doc_count\":3,\"buckets\":[{\"key\":\"1\",\"doc_count\":3,\"score\":0.75,\"bg_count\":4}]}}]}"; } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java index 16066c30d10..66efd9a72ff 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java @@ -23,7 +23,10 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.Version; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.io.stream.InputStreamStreamInput; +import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.OutputStreamStreamOutput; +import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; @@ -32,7 +35,9 @@ import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.indices.query.IndicesQueriesRegistry; +import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare; @@ -44,7 +49,6 @@ import org.elasticsearch.search.aggregations.bucket.significant.heuristics.Signi import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParserMapper; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import org.elasticsearch.search.aggregations.support.format.ValueFormatter; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.TestSearchContext; @@ -102,7 +106,10 @@ public class SignificanceHeuristicTests extends ESTestCase { // read ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray()); - InputStreamStreamInput in = new InputStreamStreamInput(inBuffer); + StreamInput in = new InputStreamStreamInput(inBuffer); + NamedWriteableRegistry registry = new NamedWriteableRegistry(); + new SearchModule(Settings.EMPTY, registry); // populates the registry through side effects + in = new NamedWriteableAwareStreamInput(in, registry); in.setVersion(version); sigTerms[1].readFrom(in); @@ -121,11 +128,10 @@ public class SignificanceHeuristicTests extends ESTestCase { ArrayList buckets = new ArrayList<>(); if (randomBoolean()) { buckets.add(new SignificantLongTerms.Bucket(1, 2, 3, 4, 123, InternalAggregations.EMPTY, null)); - sTerms[0] = new SignificantLongTerms(10, 20, "some_name", null, 1, 1, heuristic, buckets, + sTerms[0] = new SignificantLongTerms(10, 20, "some_name", DocValueFormat.RAW, 1, 1, heuristic, buckets, Collections.emptyList(), null); sTerms[1] = new SignificantLongTerms(); } else { - BytesRef term = new BytesRef("someterm"); buckets.add(new SignificantStringTerms.Bucket(term, 1, 2, 3, 4, InternalAggregations.EMPTY)); sTerms[0] = new SignificantStringTerms(10, 20, "some_name", 1, 1, heuristic, buckets, Collections.emptyList(), @@ -182,7 +188,7 @@ public class SignificanceHeuristicTests extends ESTestCase { if (type.equals("string")) { return new SignificantStringTerms(subsetSize, supersetSize, "sig_terms", 2, -1, significanceHeuristic, buckets, new ArrayList(), new HashMap()); } else { - return new SignificantLongTerms(subsetSize, supersetSize, "sig_terms", ValueFormatter.RAW, 2, -1, significanceHeuristic, buckets, new ArrayList(), new HashMap()); + return new SignificantLongTerms(subsetSize, supersetSize, "sig_terms", DocValueFormat.RAW, 2, -1, significanceHeuristic, buckets, new ArrayList(), new HashMap()); } } @@ -190,7 +196,7 @@ public class SignificanceHeuristicTests extends ESTestCase { if (type.equals("string")) { return new SignificantStringTerms.Bucket(new BytesRef(Long.toString(label).getBytes(StandardCharsets.UTF_8)), subsetDF, subsetSize, supersetDF, supersetSize, InternalAggregations.EMPTY); } else { - return new SignificantLongTerms.Bucket(subsetDF, subsetSize, supersetDF, supersetSize, label, InternalAggregations.EMPTY, ValueFormatter.RAW); + return new SignificantLongTerms.Bucket(subsetDF, subsetSize, supersetDF, supersetSize, label, InternalAggregations.EMPTY, DocValueFormat.RAW); } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java b/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java index 7adf6d2b4b9..eaeaed75985 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java +++ b/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java @@ -56,9 +56,11 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.plugins.PluginInfo; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.suggest.Suggest; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.test.rest.client.http.HttpResponse; @@ -645,7 +647,9 @@ public class ElasticsearchAssertions { } public static void assertVersionSerializable(Version version, Streamable streamable) { - assertVersionSerializable(version, streamable, null); + NamedWriteableRegistry registry = new NamedWriteableRegistry(); + new SearchModule(Settings.EMPTY, registry); // populates the registry through side effects + assertVersionSerializable(version, streamable, registry); } public static void assertVersionSerializable(Version version, Streamable streamable, NamedWriteableRegistry namedWriteableRegistry) {