|
@@ -23,6 +23,7 @@ import org.elasticsearch.action.index.IndexRequestBuilder;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.common.bytes.BytesArray;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
+import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.plugins.Plugin;
|
|
|
import org.elasticsearch.script.MockScriptPlugin;
|
|
@@ -117,6 +118,11 @@ public class BucketScriptIT extends ESIntegTestCase {
|
|
|
return value0 + value1 + value2;
|
|
|
});
|
|
|
|
|
|
+ scripts.put("single_input", vars -> {
|
|
|
+ double value = (double) vars.get("_value");
|
|
|
+ return value;
|
|
|
+ });
|
|
|
+
|
|
|
scripts.put("return null", vars -> null);
|
|
|
|
|
|
return scripts;
|
|
@@ -628,4 +634,159 @@ public class BucketScriptIT extends ESIntegTestCase {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void testSingleBucketPathAgg() throws Exception {
|
|
|
+ XContentBuilder content = XContentFactory.jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .field("buckets_path", "field2Sum")
|
|
|
+ .startObject("script")
|
|
|
+ .field("source", "single_input")
|
|
|
+ .field("lang", CustomScriptPlugin.NAME)
|
|
|
+ .endObject()
|
|
|
+ .endObject();
|
|
|
+ BucketScriptPipelineAggregationBuilder bucketScriptAgg =
|
|
|
+ BucketScriptPipelineAggregationBuilder.parse("seriesArithmetic", createParser(content));
|
|
|
+
|
|
|
+ SearchResponse response = client()
|
|
|
+ .prepareSearch("idx", "idx_unmapped")
|
|
|
+ .addAggregation(
|
|
|
+ histogram("histo")
|
|
|
+ .field(FIELD_1_NAME)
|
|
|
+ .interval(interval)
|
|
|
+ .subAggregation(sum("field2Sum").field(FIELD_2_NAME))
|
|
|
+ .subAggregation(bucketScriptAgg)).get();
|
|
|
+
|
|
|
+ assertSearchResponse(response);
|
|
|
+
|
|
|
+ Histogram histo = response.getAggregations().get("histo");
|
|
|
+ assertThat(histo, notNullValue());
|
|
|
+ assertThat(histo.getName(), equalTo("histo"));
|
|
|
+ List<? extends Histogram.Bucket> buckets = histo.getBuckets();
|
|
|
+
|
|
|
+ for (int i = 0; i < buckets.size(); ++i) {
|
|
|
+ Histogram.Bucket bucket = buckets.get(i);
|
|
|
+ if (bucket.getDocCount() == 0) {
|
|
|
+ SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
|
|
|
+ assertThat(seriesArithmetic, nullValue());
|
|
|
+ } else {
|
|
|
+ Sum field2Sum = bucket.getAggregations().get("field2Sum");
|
|
|
+ assertThat(field2Sum, notNullValue());
|
|
|
+ double field2SumValue = field2Sum.getValue();
|
|
|
+ SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
|
|
|
+ assertThat(seriesArithmetic, notNullValue());
|
|
|
+ double seriesArithmeticValue = seriesArithmetic.value();
|
|
|
+ assertThat(seriesArithmeticValue, equalTo(field2SumValue));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testArrayBucketPathAgg() throws Exception {
|
|
|
+ XContentBuilder content = XContentFactory.jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .array("buckets_path", "field2Sum", "field3Sum", "field4Sum")
|
|
|
+ .startObject("script")
|
|
|
+ .field("source", "_value0 + _value1 + _value2")
|
|
|
+ .field("lang", CustomScriptPlugin.NAME)
|
|
|
+ .endObject()
|
|
|
+ .endObject();
|
|
|
+ BucketScriptPipelineAggregationBuilder bucketScriptAgg =
|
|
|
+ BucketScriptPipelineAggregationBuilder.parse("seriesArithmetic", createParser(content));
|
|
|
+
|
|
|
+ SearchResponse response = client()
|
|
|
+ .prepareSearch("idx", "idx_unmapped")
|
|
|
+ .addAggregation(
|
|
|
+ histogram("histo")
|
|
|
+ .field(FIELD_1_NAME)
|
|
|
+ .interval(interval)
|
|
|
+ .subAggregation(sum("field2Sum").field(FIELD_2_NAME))
|
|
|
+ .subAggregation(sum("field3Sum").field(FIELD_3_NAME))
|
|
|
+ .subAggregation(sum("field4Sum").field(FIELD_4_NAME))
|
|
|
+ .subAggregation(bucketScriptAgg)).get();
|
|
|
+
|
|
|
+ assertSearchResponse(response);
|
|
|
+
|
|
|
+ Histogram histo = response.getAggregations().get("histo");
|
|
|
+ assertThat(histo, notNullValue());
|
|
|
+ assertThat(histo.getName(), equalTo("histo"));
|
|
|
+ List<? extends Histogram.Bucket> buckets = histo.getBuckets();
|
|
|
+
|
|
|
+ for (int i = 0; i < buckets.size(); ++i) {
|
|
|
+ Histogram.Bucket bucket = buckets.get(i);
|
|
|
+ if (bucket.getDocCount() == 0) {
|
|
|
+ SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
|
|
|
+ assertThat(seriesArithmetic, nullValue());
|
|
|
+ } else {
|
|
|
+ Sum field2Sum = bucket.getAggregations().get("field2Sum");
|
|
|
+ assertThat(field2Sum, notNullValue());
|
|
|
+ double field2SumValue = field2Sum.getValue();
|
|
|
+ Sum field3Sum = bucket.getAggregations().get("field3Sum");
|
|
|
+ assertThat(field3Sum, notNullValue());
|
|
|
+ double field3SumValue = field3Sum.getValue();
|
|
|
+ Sum field4Sum = bucket.getAggregations().get("field4Sum");
|
|
|
+ assertThat(field4Sum, notNullValue());
|
|
|
+ double field4SumValue = field4Sum.getValue();
|
|
|
+ SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
|
|
|
+ assertThat(seriesArithmetic, notNullValue());
|
|
|
+ double seriesArithmeticValue = seriesArithmetic.value();
|
|
|
+ assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testObjectBucketPathAgg() throws Exception {
|
|
|
+ XContentBuilder content = XContentFactory.jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .startObject("buckets_path")
|
|
|
+ .field("_value0", "field2Sum")
|
|
|
+ .field("_value1", "field3Sum")
|
|
|
+ .field("_value2", "field4Sum")
|
|
|
+ .endObject()
|
|
|
+ .startObject("script")
|
|
|
+ .field("source", "_value0 + _value1 + _value2")
|
|
|
+ .field("lang", CustomScriptPlugin.NAME)
|
|
|
+ .endObject()
|
|
|
+ .endObject();
|
|
|
+ BucketScriptPipelineAggregationBuilder bucketScriptAgg =
|
|
|
+ BucketScriptPipelineAggregationBuilder.parse("seriesArithmetic", createParser(content));
|
|
|
+
|
|
|
+ SearchResponse response = client()
|
|
|
+ .prepareSearch("idx", "idx_unmapped")
|
|
|
+ .addAggregation(
|
|
|
+ histogram("histo")
|
|
|
+ .field(FIELD_1_NAME)
|
|
|
+ .interval(interval)
|
|
|
+ .subAggregation(sum("field2Sum").field(FIELD_2_NAME))
|
|
|
+ .subAggregation(sum("field3Sum").field(FIELD_3_NAME))
|
|
|
+ .subAggregation(sum("field4Sum").field(FIELD_4_NAME))
|
|
|
+ .subAggregation(bucketScriptAgg)).get();
|
|
|
+
|
|
|
+ assertSearchResponse(response);
|
|
|
+
|
|
|
+ Histogram histo = response.getAggregations().get("histo");
|
|
|
+ assertThat(histo, notNullValue());
|
|
|
+ assertThat(histo.getName(), equalTo("histo"));
|
|
|
+ List<? extends Histogram.Bucket> buckets = histo.getBuckets();
|
|
|
+
|
|
|
+ for (int i = 0; i < buckets.size(); ++i) {
|
|
|
+ Histogram.Bucket bucket = buckets.get(i);
|
|
|
+ if (bucket.getDocCount() == 0) {
|
|
|
+ SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
|
|
|
+ assertThat(seriesArithmetic, nullValue());
|
|
|
+ } else {
|
|
|
+ Sum field2Sum = bucket.getAggregations().get("field2Sum");
|
|
|
+ assertThat(field2Sum, notNullValue());
|
|
|
+ double field2SumValue = field2Sum.getValue();
|
|
|
+ Sum field3Sum = bucket.getAggregations().get("field3Sum");
|
|
|
+ assertThat(field3Sum, notNullValue());
|
|
|
+ double field3SumValue = field3Sum.getValue();
|
|
|
+ Sum field4Sum = bucket.getAggregations().get("field4Sum");
|
|
|
+ assertThat(field4Sum, notNullValue());
|
|
|
+ double field4SumValue = field4Sum.getValue();
|
|
|
+ SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
|
|
|
+ assertThat(seriesArithmetic, notNullValue());
|
|
|
+ double seriesArithmeticValue = seriesArithmetic.value();
|
|
|
+ assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|