-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCRIPTING: Move Aggregation Script Context to its own class (#33820)
* SCRIPTING: Move Aggregation Script Context to its own class
- Loading branch information
1 parent
5115262
commit ebca273
Showing
18 changed files
with
384 additions
and
76 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...ession/src/main/java/org/elasticsearch/script/expression/ExpressionAggregationScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.expression; | ||
|
||
import java.io.IOException; | ||
import org.apache.lucene.expressions.Bindings; | ||
import org.apache.lucene.expressions.Expression; | ||
import org.apache.lucene.expressions.SimpleBindings; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.DoubleValues; | ||
import org.apache.lucene.search.DoubleValuesSource; | ||
import org.elasticsearch.script.AggregationScript; | ||
import org.elasticsearch.script.GeneralScriptException; | ||
|
||
/** | ||
* A bridge to evaluate an {@link Expression} against {@link Bindings} in the context | ||
* of a {@link AggregationScript}. | ||
*/ | ||
class ExpressionAggregationScript implements AggregationScript.LeafFactory { | ||
|
||
final Expression exprScript; | ||
final SimpleBindings bindings; | ||
final DoubleValuesSource source; | ||
final ReplaceableConstDoubleValueSource specialValue; // _value | ||
|
||
ExpressionAggregationScript(Expression e, SimpleBindings b, ReplaceableConstDoubleValueSource v) { | ||
exprScript = e; | ||
bindings = b; | ||
source = exprScript.getDoubleValuesSource(bindings); | ||
specialValue = v; | ||
} | ||
|
||
@Override | ||
public AggregationScript newInstance(final LeafReaderContext leaf) throws IOException { | ||
return new AggregationScript() { | ||
// Fake the scorer until setScorer is called. | ||
DoubleValues values = source.getValues(leaf, null); | ||
|
||
@Override | ||
public Object execute() { | ||
try { | ||
return values.doubleValue(); | ||
} catch (Exception exception) { | ||
throw new GeneralScriptException("Error evaluating " + exprScript, exception); | ||
} | ||
} | ||
|
||
@Override | ||
public void setDocument(int d) { | ||
try { | ||
values.advanceExact(d); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Can't advance to doc using " + exprScript, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void setNextAggregationValue(Object value) { | ||
// _value isn't used in script if specialValue == null | ||
if (specialValue != null) { | ||
if (value instanceof Number) { | ||
specialValue.setValue(((Number)value).doubleValue()); | ||
} else { | ||
throw new GeneralScriptException("Cannot use expression with text variable using " + exprScript); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean needs_score() { | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
server/src/main/java/org/elasticsearch/script/AggregationScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* 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.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.Scorable; | ||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.common.lucene.ScorerAware; | ||
import org.elasticsearch.index.fielddata.ScriptDocValues; | ||
import org.elasticsearch.search.lookup.LeafSearchLookup; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
public abstract class AggregationScript implements ScorerAware { | ||
|
||
public static final String[] PARAMETERS = {}; | ||
|
||
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs", Factory.class); | ||
|
||
private static final Map<String, String> DEPRECATIONS; | ||
|
||
static { | ||
Map<String, String> deprecations = new HashMap<>(); | ||
deprecations.put( | ||
"doc", | ||
"Accessing variable [doc] via [params.doc] from within an aggregation-script " + | ||
"is deprecated in favor of directly accessing [doc]." | ||
); | ||
deprecations.put( | ||
"_doc", | ||
"Accessing variable [doc] via [params._doc] from within an aggregation-script " + | ||
"is deprecated in favor of directly accessing [doc]." | ||
); | ||
DEPRECATIONS = Collections.unmodifiableMap(deprecations); | ||
} | ||
|
||
/** | ||
* The generic runtime parameters for the script. | ||
*/ | ||
private final Map<String, Object> params; | ||
|
||
/** | ||
* A leaf lookup for the bound segment this script will operate on. | ||
*/ | ||
private final LeafSearchLookup leafLookup; | ||
|
||
/** | ||
* A scorer that will return the score for the current document when the script is run. | ||
*/ | ||
protected Scorable scorer; | ||
|
||
private Object value; | ||
|
||
public AggregationScript(Map<String, Object> params, SearchLookup lookup, LeafReaderContext leafContext) { | ||
this.params = new ParameterMap(new HashMap<>(params), DEPRECATIONS); | ||
this.leafLookup = lookup.getLeafSearchLookup(leafContext); | ||
this.params.putAll(leafLookup.asMap()); | ||
} | ||
|
||
protected AggregationScript() { | ||
params = null; | ||
leafLookup = null; | ||
} | ||
|
||
/** | ||
* Return the parameters for this script. | ||
*/ | ||
public Map<String, Object> getParams() { | ||
return params; | ||
} | ||
|
||
/** | ||
* The doc lookup for the Lucene segment this script was created for. | ||
*/ | ||
public Map<String, ScriptDocValues<?>> getDoc() { | ||
return leafLookup.doc(); | ||
} | ||
|
||
/** | ||
* Set the current document to run the script on next. | ||
*/ | ||
public void setDocument(int docid) { | ||
leafLookup.setDocument(docid); | ||
} | ||
|
||
@Override | ||
public void setScorer(Scorable scorer) { | ||
this.scorer = scorer; | ||
} | ||
|
||
/** | ||
* Sets per-document aggregation {@code _value}. | ||
* <p> | ||
* The default implementation just calls {@code setNextVar("_value", value)} but | ||
* some engines might want to handle this differently for better performance. | ||
* <p> | ||
* @param value per-document value, typically a String, Long, or Double | ||
*/ | ||
public void setNextAggregationValue(Object value) { | ||
this.value = value; | ||
} | ||
|
||
public Number get_score() { | ||
try { | ||
return scorer == null ? 0.0 : scorer.score(); | ||
} catch (IOException e) { | ||
throw new ElasticsearchException("couldn't lookup score", e); | ||
} | ||
} | ||
|
||
public Object get_value() { | ||
return value; | ||
} | ||
|
||
/** | ||
* Return the result as a long. This is used by aggregation scripts over long fields. | ||
*/ | ||
public long runAsLong() { | ||
return ((Number) execute()).longValue(); | ||
} | ||
|
||
public double runAsDouble() { | ||
return ((Number) execute()).doubleValue(); | ||
} | ||
|
||
public abstract Object execute(); | ||
|
||
/** | ||
* A factory to construct {@link AggregationScript} instances. | ||
*/ | ||
public interface LeafFactory { | ||
AggregationScript newInstance(LeafReaderContext ctx) throws IOException; | ||
|
||
/** | ||
* Return {@code true} if the script needs {@code _score} calculated, or {@code false} otherwise. | ||
*/ | ||
boolean needs_score(); | ||
} | ||
|
||
/** | ||
* A factory to construct stateful {@link AggregationScript} factories for a specific index. | ||
*/ | ||
public interface Factory { | ||
LeafFactory newFactory(Map<String, Object> params, SearchLookup lookup); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.