2018-06-25 04:01:33 -07:00
|
|
|
/*
|
|
|
|
* 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 org.apache.lucene.index.LeafReaderContext;
|
2018-09-10 20:51:55 +01:00
|
|
|
import org.apache.lucene.search.Scorable;
|
2018-06-25 04:01:33 -07:00
|
|
|
import org.elasticsearch.ElasticsearchException;
|
|
|
|
import org.elasticsearch.index.fielddata.ScriptDocValues;
|
|
|
|
import org.elasticsearch.search.lookup.LeafSearchLookup;
|
|
|
|
import org.elasticsearch.search.lookup.SearchLookup;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-10-23 18:07:53 -07:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2018-06-25 04:01:33 -07:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class ScriptedMetricAggContexts {
|
2018-10-23 18:07:53 -07:00
|
|
|
|
|
|
|
public abstract static class InitScript {
|
2018-06-25 04:01:33 -07:00
|
|
|
private final Map<String, Object> params;
|
2018-08-28 01:27:43 -07:00
|
|
|
private final Map<String, Object> state;
|
2018-06-25 04:01:33 -07:00
|
|
|
|
2018-10-23 18:07:53 -07:00
|
|
|
public InitScript(Map<String, Object> params, Map<String, Object> state) {
|
2018-06-25 04:01:33 -07:00
|
|
|
this.params = params;
|
|
|
|
this.state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> getParams() {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getState() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void execute();
|
|
|
|
|
2019-12-06 15:08:05 -07:00
|
|
|
public interface Factory extends ScriptFactory {
|
2018-08-28 01:27:43 -07:00
|
|
|
InitScript newInstance(Map<String, Object> params, Map<String, Object> state);
|
2018-06-25 04:01:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] PARAMETERS = {};
|
|
|
|
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs_init", Factory.class);
|
|
|
|
}
|
|
|
|
|
2018-10-23 18:07:53 -07:00
|
|
|
public abstract static class MapScript {
|
|
|
|
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 a scripted metric agg map script " +
|
|
|
|
"is deprecated in favor of directly accessing [doc]."
|
|
|
|
);
|
|
|
|
deprecations.put(
|
|
|
|
"_doc",
|
|
|
|
"Accessing variable [doc] via [params._doc] from within a scripted metric agg map script " +
|
|
|
|
"is deprecated in favor of directly accessing [doc]."
|
|
|
|
);
|
|
|
|
deprecations.put(
|
|
|
|
"_agg",
|
|
|
|
"Accessing variable [_agg] via [params._agg] from within a scripted metric agg map script " +
|
|
|
|
"is deprecated in favor of using [state]."
|
|
|
|
);
|
|
|
|
DEPRECATIONS = Collections.unmodifiableMap(deprecations);
|
|
|
|
}
|
|
|
|
|
|
|
|
private final Map<String, Object> params;
|
|
|
|
private final Map<String, Object> state;
|
2018-06-25 04:01:33 -07:00
|
|
|
private final LeafSearchLookup leafLookup;
|
2018-09-10 20:51:55 +01:00
|
|
|
private Scorable scorer;
|
2018-06-25 04:01:33 -07:00
|
|
|
|
2018-08-28 01:27:43 -07:00
|
|
|
public MapScript(Map<String, Object> params, Map<String, Object> state, SearchLookup lookup, LeafReaderContext leafContext) {
|
2018-10-23 18:07:53 -07:00
|
|
|
this.state = state;
|
2018-06-25 04:01:33 -07:00
|
|
|
this.leafLookup = leafContext == null ? null : lookup.getLeafSearchLookup(leafContext);
|
2018-10-23 18:07:53 -07:00
|
|
|
if (leafLookup != null) {
|
|
|
|
params = new HashMap<>(params); // copy params so we aren't modifying input
|
|
|
|
params.putAll(leafLookup.asMap()); // add lookup vars
|
2019-01-18 09:13:49 -08:00
|
|
|
params = new DeprecationMap(params, DEPRECATIONS, "map-script"); // wrap with deprecations
|
2018-10-23 18:07:53 -07:00
|
|
|
}
|
|
|
|
this.params = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> getParams() {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> getState() {
|
|
|
|
return state;
|
2018-06-25 04:01:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the doc as a map (instead of LeafDocLookup) in order to abide by type whitelisting rules for
|
|
|
|
// Painless scripts.
|
|
|
|
public Map<String, ScriptDocValues<?>> getDoc() {
|
|
|
|
return leafLookup == null ? null : leafLookup.doc();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDocument(int docId) {
|
|
|
|
if (leafLookup != null) {
|
|
|
|
leafLookup.setDocument(docId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 20:51:55 +01:00
|
|
|
public void setScorer(Scorable scorer) {
|
2018-06-25 04:01:33 -07:00
|
|
|
this.scorer = scorer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_score() is named this way so that it's picked up by Painless as '_score'
|
|
|
|
public double get_score() {
|
|
|
|
if (scorer == null) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return scorer.score();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new ElasticsearchException("Couldn't look up score", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void execute();
|
|
|
|
|
|
|
|
public interface LeafFactory {
|
|
|
|
MapScript newInstance(LeafReaderContext ctx);
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:08:05 -07:00
|
|
|
public interface Factory extends ScriptFactory {
|
2018-08-28 01:27:43 -07:00
|
|
|
LeafFactory newFactory(Map<String, Object> params, Map<String, Object> state, SearchLookup lookup);
|
2018-06-25 04:01:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] PARAMETERS = new String[] {};
|
|
|
|
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs_map", Factory.class);
|
|
|
|
}
|
|
|
|
|
2018-10-23 18:07:53 -07:00
|
|
|
public abstract static class CombineScript {
|
|
|
|
private final Map<String, Object> params;
|
|
|
|
private final Map<String, Object> state;
|
|
|
|
|
2018-08-28 01:27:43 -07:00
|
|
|
public CombineScript(Map<String, Object> params, Map<String, Object> state) {
|
2018-10-23 18:07:53 -07:00
|
|
|
this.params = params;
|
|
|
|
this.state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> getParams() {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> getState() {
|
|
|
|
return state;
|
2018-06-25 04:01:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract Object execute();
|
|
|
|
|
2019-12-06 15:08:05 -07:00
|
|
|
public interface Factory extends ScriptFactory {
|
2018-08-28 01:27:43 -07:00
|
|
|
CombineScript newInstance(Map<String, Object> params, Map<String, Object> state);
|
2018-06-25 04:01:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] PARAMETERS = {};
|
|
|
|
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs_combine", Factory.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract static class ReduceScript {
|
|
|
|
private final Map<String, Object> params;
|
|
|
|
private final List<Object> states;
|
|
|
|
|
|
|
|
public ReduceScript(Map<String, Object> params, List<Object> states) {
|
|
|
|
this.params = params;
|
|
|
|
this.states = states;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> getParams() {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Object> getStates() {
|
|
|
|
return states;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract Object execute();
|
|
|
|
|
2019-12-06 15:08:05 -07:00
|
|
|
public interface Factory extends ScriptFactory {
|
2018-06-25 04:01:33 -07:00
|
|
|
ReduceScript newInstance(Map<String, Object> params, List<Object> states);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] PARAMETERS = {};
|
|
|
|
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs_reduce", Factory.class);
|
|
|
|
}
|
|
|
|
}
|