Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> new BucketAggregationSelectorScript(parameters) {
@Override
public boolean execute() {
return factory.newInstance(getParams()).execute().doubleValue() == 1.0;
return ((Number)factory.newInstance(getParams()).execute()).doubleValue() == 1.0;
}
};
return context.factoryClazz.cast(wrappedFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Map<String, Object> getParams() {
return params;
}

public abstract Number execute();
public abstract Object execute();

public interface Factory {
BucketAggregationScript newInstance(Map<String, Object> params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,21 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext
if (skipBucket) {
newBuckets.add(bucket);
} else {
Number returned = factory.newInstance(vars).execute();
Object returned = factory.newInstance(vars).execute();
if (returned == null) {
newBuckets.add(bucket);
} else {
final List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map(
(p) -> (InternalAggregation) p).collect(Collectors.toList());

InternalSimpleValue simpleValue = new InternalSimpleValue(name(), returned.doubleValue(),
formatter, new ArrayList<>(), metaData());
aggs.add(simpleValue);
InternalAggregation value;
if (returned instanceof Number) {
value = new InternalSimpleValue(name(), ((Number)returned).doubleValue(),
formatter, new ArrayList<>(), metaData());
} else {
value = new InternalStringValue(name(), returned.toString(), new ArrayList<>(), metaData());
}
aggs.add(value);
InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs),
bucket);
newBuckets.add(newBucket);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.elasticsearch.search.aggregations.pipeline;

import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.InternalAggregation;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class InternalStringValue extends InternalAggregation {
public static final String NAME = "string_value";

private final String text;

protected InternalStringValue(String name, String text, List<PipelineAggregator> pipelineAggregators, Map<String,
Object> metaData) {
super(name, pipelineAggregators, metaData);
this.text = text;
}

@Override
protected void doWriteTo(StreamOutput out) throws IOException {
throw new UnsupportedOperationException("not supported yet");
}

@Override
public InternalAggregation doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
throw new UnsupportedOperationException("Not supported");
}

@Override
public Object getProperty(List<String> path) {
if (path.isEmpty()) {
return this;
} else if (path.size() == 1 && "value".equals(path.get(0))) {
return text();
} else {
throw new IllegalArgumentException("path not supported for [" + getName() + "]: " + path);
}
}

@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
boolean hasValue = text != null;
builder.field(CommonFields.VALUE.getPreferredName(), hasValue ? text : null);
if (hasValue) {
builder.field(CommonFields.VALUE_AS_STRING.getPreferredName(), text);
}
return builder;
}

@Override
public String getWriteableName() {
return NAME;
}

public String text() {
return text;
}
}