Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion modules/aggregations/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ apply plugin: 'elasticsearch.internal-cluster-test'
esplugin {
description 'Adds "built in" aggregations to Elasticsearch.'
classname 'org.elasticsearch.aggregations.AggregationsPlugin'
extendedPlugins = ['lang-painless']
}

restResources {
restApi {
include '_common', 'indices', 'cluster', 'index', 'search', 'nodes', 'bulk'
include '_common', 'indices', 'cluster', 'index', 'search', 'nodes', 'bulk', 'scripts_painless_execute'
}
restTests {
// Pulls in all aggregation tests from core AND the forwards v7's core for forwards compatibility
Expand All @@ -38,3 +39,11 @@ tasks.named("yamlRestTestV7CompatTransform").configure { task ->
artifacts {
restTests(new File(projectDir, "src/yamlRestTest/resources/rest-api-spec/test"))
}

testClusters.configureEach {
module ':modules:lang-painless'
}

dependencies {
compileOnly(project(':modules:lang-painless:spi'))
}
5 changes: 5 additions & 0 deletions modules/aggregations/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

module org.elasticsearch.aggs {
requires org.elasticsearch.base;
requires org.elasticsearch.painless.spi;
requires org.elasticsearch.server;
requires org.elasticsearch.xcontent;
requires org.apache.lucene.core;

exports org.elasticsearch.aggregations.bucket.histogram;
exports org.elasticsearch.aggregations.bucket.adjacency;
exports org.elasticsearch.aggregations.pipeline;

opens org.elasticsearch.aggregations to org.elasticsearch.painless.spi; // whitelist resource access

provides org.elasticsearch.painless.spi.PainlessExtension with org.elasticsearch.aggregations.AggregationsPainlessExtension;
}
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
Expand Up @@ -12,13 +12,16 @@
import org.elasticsearch.aggregations.bucket.adjacency.InternalAdjacencyMatrix;
import org.elasticsearch.aggregations.bucket.histogram.AutoDateHistogramAggregationBuilder;
import org.elasticsearch.aggregations.bucket.histogram.InternalAutoDateHistogram;
import org.elasticsearch.aggregations.pipeline.MovFnPipelineAggregationBuilder;
import org.elasticsearch.aggregations.pipeline.MovingFunctionScript;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.plugins.SearchPlugin;
import org.elasticsearch.script.ScriptContext;

import java.util.List;

public class AggregationsPlugin extends Plugin implements SearchPlugin {

public class AggregationsPlugin extends Plugin implements SearchPlugin, ScriptPlugin {
@Override
public List<AggregationSpec> getAggregations() {
return List.of(
Expand All @@ -35,4 +38,20 @@ public List<AggregationSpec> getAggregations() {
.setAggregatorRegistrar(AutoDateHistogramAggregationBuilder::registerAggregators)
);
}

@Override
public List<PipelineAggregationSpec> getPipelineAggregations() {
return List.of(
new PipelineAggregationSpec(
MovFnPipelineAggregationBuilder.NAME,
MovFnPipelineAggregationBuilder::new,
MovFnPipelineAggregationBuilder.PARSER
)
);
}

@Override
public List<ScriptContext<?>> getContexts() {
return List.of(MovingFunctionScript.CONTEXT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
* Side Public License, v 1.
*/

package org.elasticsearch.search.aggregations.pipeline;
package org.elasticsearch.aggregations.pipeline;
Copy link
Member

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?


import org.elasticsearch.Version;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

package org.elasticsearch.search.aggregations.pipeline;
package org.elasticsearch.aggregations.pipeline;

import org.elasticsearch.script.Script;
import org.elasticsearch.search.DocValueFormat;
Expand All @@ -16,6 +16,9 @@
import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory;
import org.elasticsearch.search.aggregations.pipeline.BucketHelpers;
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

package org.elasticsearch.search.aggregations.pipeline;
package org.elasticsearch.aggregations.pipeline;

import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptFactory;
Expand Down
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
Expand Up @@ -6,7 +6,7 @@
# Side Public License, v 1.
#

# This file contains a whitelist for the Moving Function pipeline aggregator in core
# This file contains a allowlist for the Moving Function pipeline aggregator

class org.elasticsearch.search.aggregations.pipeline.MovingFunctions {
double max(double[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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);
}
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required because we MockScriptEngine can no longer provide a default implementation. So far as I can tell that implementation was only used for this test so I'm kind of happy to isolate it here anyway.

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 {
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@

package org.elasticsearch.aggregations.pipeline;

import org.elasticsearch.aggregations.AggregationsPlugin;
import org.elasticsearch.plugins.SearchPlugin;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.BasePipelineAggregationTestCase;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.MovFnPipelineAggregationBuilder;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

public class MovFnPipelineAggregationBuilderSerializationTests extends BasePipelineAggregationTestCase<MovFnPipelineAggregationBuilder> {
@Override
protected List<SearchPlugin> plugins() {
return List.of(new AggregationsPlugin());
}

@Override
protected MovFnPipelineAggregationBuilder createTestAggregatorFactory() {
MovFnPipelineAggregationBuilder builder = new MovFnPipelineAggregationBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static double calculateMetric(double[] values, ValuesSourceAggregationBui
return 0.0;
}

static AggregationBuilder getRandomSequentiallyOrderedParentAgg() throws IOException {
public static AggregationBuilder getRandomSequentiallyOrderedParentAgg() throws IOException {
@SuppressWarnings("unchecked")
Function<String, AggregationBuilder> builder = randomFrom(
HistogramAggregationBuilder::new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,50 @@ bad path:
buckets_path: "missing"
window: 2
script: "MovingFunctions.min(values)"

---
"Bad window":
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I combined the two test files we have for moving_fn


- 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" }
3 changes: 2 additions & 1 deletion modules/lang-painless/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of a shame.

Copy link
Member

Choose a reason for hiding this comment

The 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(',')
}

Expand Down
Loading