Skip to content

Commit

Permalink
[Rest Api Compatibility] MovAvgPipelineAggregation (#82828)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed Jan 20, 2022
1 parent c2b9d73 commit 69b79dd
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
setup:
- skip:
version: "9.0.0 - "
reason: "compatible from 8.x to 7.x"
features:
- "headers"

---
moving_avg agg throws exception:
- do:
catch: "/Moving Average aggregation usage is not supported. Use the \\[moving_fn\\] aggregation instead./"
search:
rest_total_hits_as_int: true
body:
aggs:
the_histo:
date_histogram:
field: "date"
calendar_interval: "1d"
aggs:
the_avg:
avg:
field: "value_field"
the_movavg:
moving_avg:
buckets_path: "the_avg"
headers:
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
10 changes: 10 additions & 0 deletions server/src/main/java/org/elasticsearch/search/SearchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
import org.elasticsearch.search.aggregations.pipeline.InternalStatsBucket;
import org.elasticsearch.search.aggregations.pipeline.MaxBucketPipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.MinBucketPipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.MovAvgPipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.MovFnPipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.PercentilesBucketPipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.SerialDiffPipelineAggregationBuilder;
Expand Down Expand Up @@ -783,6 +784,15 @@ private void registerPipelineAggregations(List<SearchPlugin> plugins) {
MovFnPipelineAggregationBuilder.PARSER
)
);
if (RestApiVersion.minimumSupported() == RestApiVersion.V_7) {
registerPipelineAggregation(
new PipelineAggregationSpec(
MovAvgPipelineAggregationBuilder.NAME_V7,
MovAvgPipelineAggregationBuilder::new,
MovAvgPipelineAggregationBuilder.PARSER
)
);
}

registerFromPlugin(plugins, SearchPlugin::getPipelineAggregations, this::registerPipelineAggregation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.search.aggregations.pipeline;

import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.index.query.CommonTermsQueryBuilder;
import org.elasticsearch.xcontent.ContextParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Map;

/**
* The actual moving_avg aggregation was removed as a breaking change in 8.0. This class exists to provide a friendlier error message
* if somebody attempts to use the moving_avg aggregation via the compatible-with=7 mechanism.
*
* We can remove this class entirely when v7 rest api compatibility is dropped.
*
* @deprecated Only for 7.x rest compat
*/
@Deprecated
public class MovAvgPipelineAggregationBuilder extends AbstractPipelineAggregationBuilder<MovAvgPipelineAggregationBuilder> {
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(CommonTermsQueryBuilder.class);
public static final String MOVING_AVG_AGG_DEPRECATION_MSG = "Moving Average aggregation usage is not supported. "
+ "Use the [moving_fn] aggregation instead.";

public static ParseField NAME_V7 = new ParseField("moving_avg").withAllDeprecated(MOVING_AVG_AGG_DEPRECATION_MSG)
.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7));

public static final ContextParser<String, MovAvgPipelineAggregationBuilder> PARSER = (parser, name) -> {
deprecationLogger.compatibleCritical("moving_avg_aggregation", MOVING_AVG_AGG_DEPRECATION_MSG);
throw new ParsingException(parser.getTokenLocation(), MOVING_AVG_AGG_DEPRECATION_MSG);
};

public MovAvgPipelineAggregationBuilder(StreamInput in) throws IOException {
super(in, NAME_V7.getPreferredName());
throw new UnsupportedOperationException("moving_avg is not meant to be used.");
}

@Override
protected void doWriteTo(StreamOutput out) throws IOException {
throw new UnsupportedOperationException("moving_avg is not meant to be used.");
}

@Override
protected PipelineAggregator createInternal(Map<String, Object> metadata) {
throw new UnsupportedOperationException("moving_avg is not meant to be used.");
}

@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
throw new UnsupportedOperationException("moving_avg is not meant to be used.");
}

@Override
protected void validate(ValidationContext context) {
throw new UnsupportedOperationException("moving_avg is not meant to be used.");
}

@Override
public final String getWriteableName() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.DerivativePipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.InternalDerivative;
import org.elasticsearch.search.aggregations.pipeline.MovAvgPipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
Expand Down Expand Up @@ -478,6 +479,8 @@ public CheckedBiConsumer<ShardSearchRequest, StreamOutput, IOException> getReque
private static final String[] REST_COMPATIBLE_QUERIES = new String[] {
TypeQueryV7Builder.NAME_V7.getPreferredName(),
CommonTermsQueryBuilder.NAME_V7.getPreferredName() };
private static final String[] REST_COMPATIBLE_AGGREGATIONS = new String[] {
MovAvgPipelineAggregationBuilder.NAME_V7.getPreferredName() };

/**
* Dummy test {@link AggregationBuilder} used to test registering aggregation builders.
Expand Down Expand Up @@ -765,7 +768,7 @@ public List<SearchPlugin.QuerySpec<?>> getQueries() {
.filter(e -> RestApiVersion.current().matches(e.restApiCompatibility))
.collect(toSet()),
// -1 because of the registered in the test
hasSize(searchModule.getNamedXContents().size() - REST_COMPATIBLE_QUERIES.length - 1)
hasSize(searchModule.getNamedXContents().size() - REST_COMPATIBLE_QUERIES.length - REST_COMPATIBLE_AGGREGATIONS.length - 1)
);

final List<NamedXContentRegistry.Entry> compatEntry = searchModule.getNamedXContents()
Expand Down

0 comments on commit 69b79dd

Please sign in to comment.