|
23 | 23 | import org.elasticsearch.action.search.SearchResponse;
|
24 | 24 | import org.elasticsearch.common.bytes.BytesArray;
|
25 | 25 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
| 26 | +import org.elasticsearch.common.xcontent.XContentFactory; |
26 | 27 | import org.elasticsearch.common.xcontent.XContentType;
|
27 | 28 | import org.elasticsearch.plugins.Plugin;
|
28 | 29 | import org.elasticsearch.script.MockScriptPlugin;
|
@@ -117,6 +118,11 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
|
117 | 118 | return value0 + value1 + value2;
|
118 | 119 | });
|
119 | 120 |
|
| 121 | + scripts.put("single_input", vars -> { |
| 122 | + double value = (double) vars.get("_value"); |
| 123 | + return value; |
| 124 | + }); |
| 125 | + |
120 | 126 | scripts.put("return null", vars -> null);
|
121 | 127 |
|
122 | 128 | return scripts;
|
@@ -628,4 +634,159 @@ public void testPartiallyUnmapped() throws Exception {
|
628 | 634 | }
|
629 | 635 | }
|
630 | 636 | }
|
| 637 | + |
| 638 | + public void testSingleBucketPathAgg() throws Exception { |
| 639 | + XContentBuilder content = XContentFactory.jsonBuilder() |
| 640 | + .startObject() |
| 641 | + .field("buckets_path", "field2Sum") |
| 642 | + .startObject("script") |
| 643 | + .field("source", "single_input") |
| 644 | + .field("lang", CustomScriptPlugin.NAME) |
| 645 | + .endObject() |
| 646 | + .endObject(); |
| 647 | + BucketScriptPipelineAggregationBuilder bucketScriptAgg = |
| 648 | + BucketScriptPipelineAggregationBuilder.parse("seriesArithmetic", createParser(content)); |
| 649 | + |
| 650 | + SearchResponse response = client() |
| 651 | + .prepareSearch("idx", "idx_unmapped") |
| 652 | + .addAggregation( |
| 653 | + histogram("histo") |
| 654 | + .field(FIELD_1_NAME) |
| 655 | + .interval(interval) |
| 656 | + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) |
| 657 | + .subAggregation(bucketScriptAgg)).get(); |
| 658 | + |
| 659 | + assertSearchResponse(response); |
| 660 | + |
| 661 | + Histogram histo = response.getAggregations().get("histo"); |
| 662 | + assertThat(histo, notNullValue()); |
| 663 | + assertThat(histo.getName(), equalTo("histo")); |
| 664 | + List<? extends Histogram.Bucket> buckets = histo.getBuckets(); |
| 665 | + |
| 666 | + for (int i = 0; i < buckets.size(); ++i) { |
| 667 | + Histogram.Bucket bucket = buckets.get(i); |
| 668 | + if (bucket.getDocCount() == 0) { |
| 669 | + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); |
| 670 | + assertThat(seriesArithmetic, nullValue()); |
| 671 | + } else { |
| 672 | + Sum field2Sum = bucket.getAggregations().get("field2Sum"); |
| 673 | + assertThat(field2Sum, notNullValue()); |
| 674 | + double field2SumValue = field2Sum.getValue(); |
| 675 | + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); |
| 676 | + assertThat(seriesArithmetic, notNullValue()); |
| 677 | + double seriesArithmeticValue = seriesArithmetic.value(); |
| 678 | + assertThat(seriesArithmeticValue, equalTo(field2SumValue)); |
| 679 | + } |
| 680 | + } |
| 681 | + } |
| 682 | + |
| 683 | + public void testArrayBucketPathAgg() throws Exception { |
| 684 | + XContentBuilder content = XContentFactory.jsonBuilder() |
| 685 | + .startObject() |
| 686 | + .array("buckets_path", "field2Sum", "field3Sum", "field4Sum") |
| 687 | + .startObject("script") |
| 688 | + .field("source", "_value0 + _value1 + _value2") |
| 689 | + .field("lang", CustomScriptPlugin.NAME) |
| 690 | + .endObject() |
| 691 | + .endObject(); |
| 692 | + BucketScriptPipelineAggregationBuilder bucketScriptAgg = |
| 693 | + BucketScriptPipelineAggregationBuilder.parse("seriesArithmetic", createParser(content)); |
| 694 | + |
| 695 | + SearchResponse response = client() |
| 696 | + .prepareSearch("idx", "idx_unmapped") |
| 697 | + .addAggregation( |
| 698 | + histogram("histo") |
| 699 | + .field(FIELD_1_NAME) |
| 700 | + .interval(interval) |
| 701 | + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) |
| 702 | + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) |
| 703 | + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) |
| 704 | + .subAggregation(bucketScriptAgg)).get(); |
| 705 | + |
| 706 | + assertSearchResponse(response); |
| 707 | + |
| 708 | + Histogram histo = response.getAggregations().get("histo"); |
| 709 | + assertThat(histo, notNullValue()); |
| 710 | + assertThat(histo.getName(), equalTo("histo")); |
| 711 | + List<? extends Histogram.Bucket> buckets = histo.getBuckets(); |
| 712 | + |
| 713 | + for (int i = 0; i < buckets.size(); ++i) { |
| 714 | + Histogram.Bucket bucket = buckets.get(i); |
| 715 | + if (bucket.getDocCount() == 0) { |
| 716 | + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); |
| 717 | + assertThat(seriesArithmetic, nullValue()); |
| 718 | + } else { |
| 719 | + Sum field2Sum = bucket.getAggregations().get("field2Sum"); |
| 720 | + assertThat(field2Sum, notNullValue()); |
| 721 | + double field2SumValue = field2Sum.getValue(); |
| 722 | + Sum field3Sum = bucket.getAggregations().get("field3Sum"); |
| 723 | + assertThat(field3Sum, notNullValue()); |
| 724 | + double field3SumValue = field3Sum.getValue(); |
| 725 | + Sum field4Sum = bucket.getAggregations().get("field4Sum"); |
| 726 | + assertThat(field4Sum, notNullValue()); |
| 727 | + double field4SumValue = field4Sum.getValue(); |
| 728 | + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); |
| 729 | + assertThat(seriesArithmetic, notNullValue()); |
| 730 | + double seriesArithmeticValue = seriesArithmetic.value(); |
| 731 | + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue)); |
| 732 | + } |
| 733 | + } |
| 734 | + } |
| 735 | + |
| 736 | + public void testObjectBucketPathAgg() throws Exception { |
| 737 | + XContentBuilder content = XContentFactory.jsonBuilder() |
| 738 | + .startObject() |
| 739 | + .startObject("buckets_path") |
| 740 | + .field("_value0", "field2Sum") |
| 741 | + .field("_value1", "field3Sum") |
| 742 | + .field("_value2", "field4Sum") |
| 743 | + .endObject() |
| 744 | + .startObject("script") |
| 745 | + .field("source", "_value0 + _value1 + _value2") |
| 746 | + .field("lang", CustomScriptPlugin.NAME) |
| 747 | + .endObject() |
| 748 | + .endObject(); |
| 749 | + BucketScriptPipelineAggregationBuilder bucketScriptAgg = |
| 750 | + BucketScriptPipelineAggregationBuilder.parse("seriesArithmetic", createParser(content)); |
| 751 | + |
| 752 | + SearchResponse response = client() |
| 753 | + .prepareSearch("idx", "idx_unmapped") |
| 754 | + .addAggregation( |
| 755 | + histogram("histo") |
| 756 | + .field(FIELD_1_NAME) |
| 757 | + .interval(interval) |
| 758 | + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) |
| 759 | + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) |
| 760 | + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) |
| 761 | + .subAggregation(bucketScriptAgg)).get(); |
| 762 | + |
| 763 | + assertSearchResponse(response); |
| 764 | + |
| 765 | + Histogram histo = response.getAggregations().get("histo"); |
| 766 | + assertThat(histo, notNullValue()); |
| 767 | + assertThat(histo.getName(), equalTo("histo")); |
| 768 | + List<? extends Histogram.Bucket> buckets = histo.getBuckets(); |
| 769 | + |
| 770 | + for (int i = 0; i < buckets.size(); ++i) { |
| 771 | + Histogram.Bucket bucket = buckets.get(i); |
| 772 | + if (bucket.getDocCount() == 0) { |
| 773 | + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); |
| 774 | + assertThat(seriesArithmetic, nullValue()); |
| 775 | + } else { |
| 776 | + Sum field2Sum = bucket.getAggregations().get("field2Sum"); |
| 777 | + assertThat(field2Sum, notNullValue()); |
| 778 | + double field2SumValue = field2Sum.getValue(); |
| 779 | + Sum field3Sum = bucket.getAggregations().get("field3Sum"); |
| 780 | + assertThat(field3Sum, notNullValue()); |
| 781 | + double field3SumValue = field3Sum.getValue(); |
| 782 | + Sum field4Sum = bucket.getAggregations().get("field4Sum"); |
| 783 | + assertThat(field4Sum, notNullValue()); |
| 784 | + double field4SumValue = field4Sum.getValue(); |
| 785 | + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); |
| 786 | + assertThat(seriesArithmetic, notNullValue()); |
| 787 | + double seriesArithmeticValue = seriesArithmetic.value(); |
| 788 | + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue)); |
| 789 | + } |
| 790 | + } |
| 791 | + } |
631 | 792 | }
|
0 commit comments