-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
SCRIPTING: Move Aggregation Scripts to their own context #32068
Changes from 18 commits
2ee4b46
5088757
f924c07
de9b238
2ac53ab
0386ec8
574780d
63963f6
ba8fd94
678d388
145e11f
05e6559
bc27466
0f13633
a1bd389
af67738
b0a4487
2662c87
f31660c
d506e97
5a55338
956480e
b095f3d
5bf4573
6509e85
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 @@ | ||
/* | ||
* 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.script; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* A script used in bucket aggregations that returns a {@code double} value. | ||
*/ | ||
public abstract class BucketAggregationScript { | ||
|
||
public static final String[] PARAMETERS = { "params" }; | ||
|
||
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("bucket_aggregation", Factory.class); | ||
|
||
public abstract double execute(Map<String, Object> params); | ||
|
||
public interface Factory { | ||
BucketAggregationScript newInstance(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.script; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* A script used in bucket aggregations that returns a {@code boolean} value. | ||
*/ | ||
public abstract class BucketAggregationSelectorScript { | ||
|
||
public static final String[] PARAMETERS = { "params" }; | ||
|
||
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggregation_selector", Factory.class); | ||
|
||
public abstract boolean execute(Map<String, Object> params); | ||
|
||
public interface Factory { | ||
BucketAggregationSelectorScript newInstance(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.script; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* A script used in script heuristics. | ||
*/ | ||
public abstract class ScriptHeuristicScript { | ||
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 name is confusing having "script" twice. Can we call it 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.
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. It's long, but probably better in this case too not have any possible confusion with regular scoring scripts. |
||
|
||
public static final String[] PARAMETERS = { "params" }; | ||
|
||
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("script_heuristic", Factory.class); | ||
|
||
public abstract double execute(Map<String, Object> params); | ||
|
||
public interface Factory { | ||
ScriptHeuristicScript newInstance(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,12 +28,14 @@ | |
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.index.query.QueryShardContext; | ||
import org.elasticsearch.index.query.QueryShardException; | ||
import org.elasticsearch.script.ExecutableScript; | ||
import org.elasticsearch.script.Script; | ||
import org.elasticsearch.script.ScriptHeuristicScript; | ||
import org.elasticsearch.search.aggregations.InternalAggregation; | ||
import org.elasticsearch.search.internal.SearchContext; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class ScriptHeuristic extends SignificanceHeuristic { | ||
|
@@ -48,19 +50,21 @@ static class ExecutableScriptHeuristic extends ScriptHeuristic { | |
private final LongAccessor supersetSizeHolder; | ||
private final LongAccessor subsetDfHolder; | ||
private final LongAccessor supersetDfHolder; | ||
private final ExecutableScript executableScript; | ||
private final ScriptHeuristicScript executableScript; | ||
private final Map<String, Object> params = new HashMap<>(); | ||
|
||
ExecutableScriptHeuristic(Script script, ExecutableScript executableScript){ | ||
ExecutableScriptHeuristic(Script script, ScriptHeuristicScript executableScript) { | ||
super(script); | ||
subsetSizeHolder = new LongAccessor(); | ||
supersetSizeHolder = new LongAccessor(); | ||
subsetDfHolder = new LongAccessor(); | ||
supersetDfHolder = new LongAccessor(); | ||
this.executableScript = executableScript; | ||
executableScript.setNextVar("_subset_freq", subsetDfHolder); | ||
executableScript.setNextVar("_subset_size", subsetSizeHolder); | ||
executableScript.setNextVar("_superset_freq", supersetDfHolder); | ||
executableScript.setNextVar("_superset_size", supersetSizeHolder); | ||
params.putAll(script.getParams()); | ||
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 think this should be its own context. Putting these into params would be a breaking change, and also not utilize the intent of having contexts (different variables for different uses). 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. @rjernst but currently these are documented as Also under the hood 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. You are right, I was confused because of what I saw in ScriptImpl for painless (naming implying it was in variables for the script, but that is actually the params). I still think this needs to be its own context. We can eventually move these to direct arguments of the execute method (again, so params can be read-only in the future). 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. 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. They can be added as direct arguments in a separate PR. There should also be deprecation messages along with that so we can remove inserting them into params. |
||
params.put("_subset_freq", subsetDfHolder); | ||
params.put("_subset_size", subsetSizeHolder); | ||
params.put("_superset_freq", supersetDfHolder); | ||
params.put("_superset_size", supersetSizeHolder); | ||
} | ||
|
||
@Override | ||
|
@@ -69,7 +73,7 @@ public double getScore(long subsetFreq, long subsetSize, long supersetFreq, long | |
supersetSizeHolder.value = supersetSize; | ||
subsetDfHolder.value = subsetFreq; | ||
supersetDfHolder.value = supersetFreq; | ||
return ((Number) executableScript.run()).doubleValue(); | ||
return executableScript.execute(params); | ||
} | ||
} | ||
|
||
|
@@ -91,15 +95,15 @@ public void writeTo(StreamOutput out) throws IOException { | |
|
||
@Override | ||
public SignificanceHeuristic rewrite(InternalAggregation.ReduceContext context) { | ||
ExecutableScript.Factory factory = context.scriptService().compile(script, ExecutableScript.AGGS_CONTEXT); | ||
return new ExecutableScriptHeuristic(script, factory.newInstance(script.getParams())); | ||
ScriptHeuristicScript.Factory factory = context.scriptService().compile(script, ScriptHeuristicScript.CONTEXT); | ||
return new ExecutableScriptHeuristic(script, factory.newInstance()); | ||
} | ||
|
||
@Override | ||
public SignificanceHeuristic rewrite(SearchContext context) { | ||
QueryShardContext shardContext = context.getQueryShardContext(); | ||
ExecutableScript.Factory compiledScript = shardContext.getScriptService().compile(script, ExecutableScript.AGGS_CONTEXT); | ||
return new ExecutableScriptHeuristic(script, compiledScript.newInstance(script.getParams())); | ||
ScriptHeuristicScript.Factory compiledScript = shardContext.getScriptService().compile(script, ScriptHeuristicScript.CONTEXT); | ||
return new ExecutableScriptHeuristic(script, compiledScript.newInstance()); | ||
} | ||
|
||
|
||
|
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.
In expressions, we want these types of decisions made up front, not at execution time. Take a look at how params are passed to the factory in other script examples, and exposed as
getParams()
on the script class. We want to avoid costly things like hash lookups per document for something that won't change (eg the params that exist).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.
@rjernst but these
params
aren't reallyparams
like in the other scripts. They aren't constant but change in a loop here https://github.com/elastic/elasticsearch/pull/32068/files#diff-15179b62bf7bf2f829791af279ea380aR90.Previously those were part of a params field passed to the factory which made the script stateful and I guess led to this todo https://github.com/elastic/elasticsearch/pull/32068/files#diff-15179b62bf7bf2f829791af279ea380aL98 <= that I resolved here?
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.
Well, that isn't really resolving that comment. While only one script object is created with your change, the performance is the same because we are still doing the binding at execution time for each document. I would rather keep that original code with that comment, deprecate passing things in via params (by adding the parameters directly as arguments), and then fix the TODO in master.
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.
@rjernst ah now I see. It still looks to me like the change here get's us to a better spot for existing scripts that still use the
params
here since:org.elasticsearch.script.expression.ExpressionExecutableScript#ExpressionExecutableScript
Wouldn't it make more sense to keep this to improve the backwards compatible case and add a todo to avoid the redundant binding for the unchanged vars (and creating the map over and over) instead?
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.
Or put differently :)
If we don't move to passing the
params
as theexecute
argument, then we'll be forced to recreate the script over so long as we support passing variables to scripts viaparams
won't we? => better move to more efficiently updating this map (and avoiding looping over it in expressions ideally) than adding theparams
field back and being forced to recreate the script?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.
Then again, the correct solution will have
params
in this still -> will revert to usingparams
as on the script tomorrow :)