org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetricAggregationBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* 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.search.aggregations.metrics.scripted;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.InternalAggregation.Type;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
public class ScriptedMetricAggregationBuilder extends AbstractAggregationBuilder {
public static final String NAME = "scripted_metric";
private static final Type TYPE = new Type(NAME);
private static final ParseField INIT_SCRIPT_FIELD = new ParseField("init_script");
private static final ParseField MAP_SCRIPT_FIELD = new ParseField("map_script");
private static final ParseField COMBINE_SCRIPT_FIELD = new ParseField("combine_script");
private static final ParseField REDUCE_SCRIPT_FIELD = new ParseField("reduce_script");
private static final ParseField PARAMS_FIELD = new ParseField("params");
private Script initScript;
private Script mapScript;
private Script combineScript;
private Script reduceScript;
private Map params;
public ScriptedMetricAggregationBuilder(String name) {
super(name, TYPE);
}
/**
* Read from a stream.
*/
public ScriptedMetricAggregationBuilder(StreamInput in) throws IOException {
super(in, TYPE);
initScript = in.readOptionalWriteable(Script::new);
mapScript = in.readOptionalWriteable(Script::new);
combineScript = in.readOptionalWriteable(Script::new);
reduceScript = in.readOptionalWriteable(Script::new);
if (in.readBoolean()) {
params = in.readMap();
}
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalWriteable(initScript);
out.writeOptionalWriteable(mapScript);
out.writeOptionalWriteable(combineScript);
out.writeOptionalWriteable(reduceScript);
boolean hasParams = params != null;
out.writeBoolean(hasParams);
if (hasParams) {
out.writeMap(params);
}
}
/**
* Set the init script.
*/
public ScriptedMetricAggregationBuilder initScript(Script initScript) {
if (initScript == null) {
throw new IllegalArgumentException("[initScript] must not be null: [" + name + "]");
}
this.initScript = initScript;
return this;
}
/**
* Get the init script.
*/
public Script initScript() {
return initScript;
}
/**
* Set the map script.
*/
public ScriptedMetricAggregationBuilder mapScript(Script mapScript) {
if (mapScript == null) {
throw new IllegalArgumentException("[mapScript] must not be null: [" + name + "]");
}
this.mapScript = mapScript;
return this;
}
/**
* Get the map script.
*/
public Script mapScript() {
return mapScript;
}
/**
* Set the combine script.
*/
public ScriptedMetricAggregationBuilder combineScript(Script combineScript) {
if (combineScript == null) {
throw new IllegalArgumentException("[combineScript] must not be null: [" + name + "]");
}
this.combineScript = combineScript;
return this;
}
/**
* Get the combine script.
*/
public Script combineScript() {
return combineScript;
}
/**
* Set the reduce script.
*/
public ScriptedMetricAggregationBuilder reduceScript(Script reduceScript) {
if (reduceScript == null) {
throw new IllegalArgumentException("[reduceScript] must not be null: [" + name + "]");
}
this.reduceScript = reduceScript;
return this;
}
/**
* Get the reduce script.
*/
public Script reduceScript() {
return reduceScript;
}
/**
* Set parameters that will be available in the init,
* map and combine phases.
*/
public ScriptedMetricAggregationBuilder params(Map params) {
if (params == null) {
throw new IllegalArgumentException("[params] must not be null: [" + name + "]");
}
this.params = params;
return this;
}
/**
* Get parameters that will be available in the init,
* map and combine phases.
*/
public Map params() {
return params;
}
@Override
protected ScriptedMetricAggregatorFactory doBuild(AggregationContext context, AggregatorFactory parent,
Builder subfactoriesBuilder) throws IOException {
QueryShardContext queryShardContext = context.searchContext().getQueryShardContext();
Function