-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Move mov_fn agg to module #90836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move mov_fn agg to module #90836
Changes from all commits
ccfe2a4
5091656
ac49040
74086fe
7d9e8c1
442ec9b
781079c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * 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.aggregations; | ||
|
|
||
| import org.elasticsearch.aggregations.pipeline.MovingFunctionScript; | ||
| import org.elasticsearch.painless.spi.PainlessExtension; | ||
| import org.elasticsearch.painless.spi.PainlessTestScript; | ||
| import org.elasticsearch.painless.spi.Whitelist; | ||
| import org.elasticsearch.painless.spi.WhitelistLoader; | ||
| import org.elasticsearch.script.ScriptContext; | ||
| import org.elasticsearch.search.aggregations.pipeline.MovingFunctions; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Extends the painless whitelist for the {@link MovingFunctionScript} to include {@link MovingFunctions}. | ||
| */ | ||
| public class AggregationsPainlessExtension implements PainlessExtension { | ||
| private static final Whitelist MOVING_FUNCTION_ALLOWLIST = WhitelistLoader.loadFromResourceFiles( | ||
| AggregationsPainlessExtension.class, | ||
| "moving_function_whitelist.txt" | ||
| ); | ||
|
|
||
| @Override | ||
| public Map<ScriptContext<?>, List<Whitelist>> getContextWhitelists() { | ||
| return Map.ofEntries( | ||
| Map.entry(MovingFunctionScript.CONTEXT, List.of(MOVING_FUNCTION_ALLOWLIST)), | ||
| Map.entry(PainlessTestScript.CONTEXT, List.of(MOVING_FUNCTION_ALLOWLIST)) | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # | ||
| # 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. | ||
| # | ||
|
|
||
| org.elasticsearch.aggregations.AggregationsPainlessExtension |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.aggregations.pipeline; | ||
| package org.elasticsearch.aggregations.pipeline; | ||
|
|
||
| import org.apache.lucene.document.Document; | ||
| import org.apache.lucene.document.LongPoint; | ||
|
|
@@ -24,10 +24,9 @@ | |
| import org.elasticsearch.index.mapper.DateFieldMapper; | ||
| import org.elasticsearch.index.mapper.MappedFieldType; | ||
| import org.elasticsearch.index.mapper.NumberFieldMapper; | ||
| import org.elasticsearch.script.MockScriptEngine; | ||
| import org.elasticsearch.script.Script; | ||
| import org.elasticsearch.script.ScriptContext; | ||
| import org.elasticsearch.script.ScriptEngine; | ||
| import org.elasticsearch.script.ScriptModule; | ||
| import org.elasticsearch.script.ScriptService; | ||
| import org.elasticsearch.script.ScriptType; | ||
| import org.elasticsearch.search.aggregations.AggregatorTestCase; | ||
|
|
@@ -36,17 +35,20 @@ | |
| import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; | ||
| import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram; | ||
| import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder; | ||
| import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue; | ||
| import org.elasticsearch.search.aggregations.pipeline.MovingFunctions; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| public class MovFnAggrgatorTests extends AggregatorTestCase { | ||
| public class MovFnAggregatorTests extends AggregatorTestCase { | ||
|
|
||
| private static final String DATE_FIELD = "date"; | ||
| private static final String INSTANT_FIELD = "instant"; | ||
|
|
@@ -69,14 +71,44 @@ public class MovFnAggrgatorTests extends AggregatorTestCase { | |
|
|
||
| @Override | ||
| protected ScriptService getMockScriptService() { | ||
| MockScriptEngine scriptEngine = new MockScriptEngine( | ||
| MockScriptEngine.NAME, | ||
| Collections.singletonMap("test", script -> MovingFunctions.max((double[]) script.get("_values"))), | ||
| Collections.emptyMap() | ||
| ); | ||
| ScriptEngine scriptEngine = new ScriptEngine() { | ||
| @Override | ||
| public String getType() { | ||
| return "test"; | ||
| } | ||
|
|
||
| @Override | ||
| public <FactoryType> FactoryType compile( | ||
| String name, | ||
| String code, | ||
| ScriptContext<FactoryType> context, | ||
| Map<String, String> params | ||
| ) { | ||
| if (getSupportedContexts().contains(context) == false) { | ||
| return null; | ||
| } | ||
| MovingFunctionScript.Factory factory = () -> new MovingFunctionScript() { | ||
| @Override | ||
| public double execute(Map<String, Object> params, double[] values) { | ||
| return MovingFunctions.max(values); | ||
| } | ||
| }; | ||
| return context.factoryClazz.cast(factory); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<ScriptContext<?>> getSupportedContexts() { | ||
| return Set.of(MovingFunctionScript.CONTEXT); | ||
| } | ||
| }; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is required because we |
||
| Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine); | ||
|
|
||
| return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS, () -> 1L); | ||
| return new ScriptService( | ||
| Settings.EMPTY, | ||
| engines, | ||
| Map.of(MovingFunctionScript.CONTEXT.name, MovingFunctionScript.CONTEXT), | ||
| () -> 1L | ||
| ); | ||
| } | ||
|
|
||
| public void testMatchAllDocs() throws IOException { | ||
|
|
@@ -94,7 +126,7 @@ public void testWideWindow() throws IOException { | |
| } | ||
|
|
||
| private void check(int shift, int window, List<Double> expected) throws IOException { | ||
| Script script = new Script(ScriptType.INLINE, MockScriptEngine.NAME, "test", Collections.emptyMap()); | ||
| Script script = new Script(ScriptType.INLINE, "test", "test", Collections.emptyMap()); | ||
| MovFnPipelineAggregationBuilder builder = new MovFnPipelineAggregationBuilder("mov_fn", "avg", script, window); | ||
| builder.setShift(shift); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -765,3 +765,50 @@ bad path: | |
| buckets_path: "missing" | ||
| window: 2 | ||
| script: "MovingFunctions.min(values)" | ||
|
|
||
| --- | ||
| "Bad window": | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I combined the two test files we have for |
||
|
|
||
| - skip: | ||
| version: " - 7.1.99" | ||
| reason: "calendar_interval added in 7.2" | ||
|
|
||
| - do: | ||
| catch: /\[window\] must be a positive, non-zero integer\./ | ||
| search: | ||
| rest_total_hits_as_int: true | ||
| body: | ||
| size: 0 | ||
| aggs: | ||
| the_histo: | ||
| date_histogram: | ||
| field: "date" | ||
| calendar_interval: "1d" | ||
| aggs: | ||
| the_avg: | ||
| avg: | ||
| field: "value_field" | ||
| the_mov_fn: | ||
| moving_fn: | ||
| buckets_path: "the_avg" | ||
| window: -1 | ||
| script: "MovingFunctions.max(values)" | ||
|
|
||
| --- | ||
| "Not under date_histo": | ||
|
|
||
| - do: | ||
| catch: /moving_fn aggregation \[the_mov_fn\] must have a histogram, date_histogram or auto_date_histogram as parent but doesn't have a parent/ | ||
| search: | ||
| rest_total_hits_as_int: true | ||
| body: | ||
| size: 0 | ||
| aggs: | ||
| the_avg: | ||
| avg: | ||
| field: "value_field" | ||
| the_mov_fn: | ||
| moving_fn: | ||
| buckets_path: "the_avg" | ||
| window: 1 | ||
| script: "MovingFunctions.max(values)" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| MovingFunctions are available in the default context: | ||
| - do: | ||
| scripts_painless_execute: | ||
| body: | ||
| script: | ||
| source: "MovingFunctions.max(new double[] {1.0, 2.0, -1234})" | ||
| - match: { result: "2.0" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,7 +96,8 @@ tasks.named("yamlRestTestV7CompatTest").configure { | |
| 'painless/40_fields_api/script fields api for dates', | ||
| 'painless/70_execute_painless_scripts/Execute with double field context (multi-value, fields api)', | ||
| 'painless/40_fields_api/filter script fields api', | ||
| 'painless/40_fields_api/script score fields api' | ||
| 'painless/40_fields_api/script score fields api', | ||
| 'painless/70_mov_fn_agg/*' // Agg moved to a module. | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kind of a shame.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, because the integration test cluster started here doesn't have the analytics module and therefor the moving average pipeline aggregation isn't available when running these bwc yaml tests. We won't have this test coverage in the aggregations module, since the tests didn't exist in this module in earlier versions (in fact the entire module doesn't exist). We can maybe think about adding the aggregations module to the painless test cluster? Then we don't lose test coverage? |
||
| ].join(',') | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a package level java docs for pipeline package?