Skip to content

Commit a1bd389

Browse files
CR: Add separate context for scriptheuristic
1 parent 0f13633 commit a1bd389

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.script;
21+
22+
import java.util.Map;
23+
24+
/**
25+
* A script used in script heuristics.
26+
*/
27+
public abstract class ScriptHeuristicScript {
28+
29+
public static final String[] PARAMETERS = { "params" };
30+
31+
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("script_heuristic", Factory.class);
32+
33+
public abstract double execute(Map<String, Object> params);
34+
35+
public interface Factory {
36+
ScriptHeuristicScript newInstance();
37+
}
38+
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.elasticsearch.common.xcontent.XContentParser;
2929
import org.elasticsearch.index.query.QueryShardContext;
3030
import org.elasticsearch.index.query.QueryShardException;
31-
import org.elasticsearch.script.BucketAggregationScript;
3231
import org.elasticsearch.script.Script;
32+
import org.elasticsearch.script.ScriptHeuristicScript;
3333
import org.elasticsearch.search.aggregations.InternalAggregation;
3434
import org.elasticsearch.search.internal.SearchContext;
3535

@@ -50,10 +50,10 @@ static class ExecutableScriptHeuristic extends ScriptHeuristic {
5050
private final LongAccessor supersetSizeHolder;
5151
private final LongAccessor subsetDfHolder;
5252
private final LongAccessor supersetDfHolder;
53-
private final BucketAggregationScript executableScript;
53+
private final ScriptHeuristicScript executableScript;
5454
private final Map<String, Object> params = new HashMap<>();
5555

56-
ExecutableScriptHeuristic(Script script, BucketAggregationScript executableScript) {
56+
ExecutableScriptHeuristic(Script script, ScriptHeuristicScript executableScript) {
5757
super(script);
5858
subsetSizeHolder = new LongAccessor();
5959
supersetSizeHolder = new LongAccessor();
@@ -95,14 +95,14 @@ public void writeTo(StreamOutput out) throws IOException {
9595

9696
@Override
9797
public SignificanceHeuristic rewrite(InternalAggregation.ReduceContext context) {
98-
BucketAggregationScript.Factory factory = context.scriptService().compile(script, BucketAggregationScript.CONTEXT);
98+
ScriptHeuristicScript.Factory factory = context.scriptService().compile(script, ScriptHeuristicScript.CONTEXT);
9999
return new ExecutableScriptHeuristic(script, factory.newInstance());
100100
}
101101

102102
@Override
103103
public SignificanceHeuristic rewrite(SearchContext context) {
104104
QueryShardContext shardContext = context.getQueryShardContext();
105-
BucketAggregationScript.Factory compiledScript = shardContext.getScriptService().compile(script, BucketAggregationScript.CONTEXT);
105+
ScriptHeuristicScript.Factory compiledScript = shardContext.getScriptService().compile(script, ScriptHeuristicScript.CONTEXT);
106106
return new ExecutableScriptHeuristic(script, compiledScript.newInstance());
107107
}
108108

0 commit comments

Comments
 (0)