25
25
import static org .hamcrest .Matchers .greaterThan ;
26
26
import static org .hamcrest .Matchers .lessThan ;
27
27
import static org .hamcrest .Matchers .sameInstance ;
28
+ import static org .mockito .Mockito .mock ;
29
+ import static org .mockito .Mockito .when ;
28
30
29
31
public class DelayedBucketTests extends ESTestCase {
30
32
public void testToString () {
@@ -40,6 +42,23 @@ public void testReduced() {
40
42
assertThat (b .reduced (reduce , context ), sameInstance (b .reduced (reduce , context )));
41
43
assertThat (b .reduced (reduce , context ).getKeyAsString (), equalTo ("test" ));
42
44
assertThat (b .reduced (reduce , context ).getDocCount (), equalTo (3L ));
45
+ // it only accounts for sub-buckets
46
+ assertEquals (0 , buckets .get ());
47
+ }
48
+
49
+ public void testReducedSubAggregation () {
50
+ AtomicInteger buckets = new AtomicInteger ();
51
+ AggregationReduceContext context = new AggregationReduceContext .ForFinal (null , null , () -> false , null , buckets ::addAndGet );
52
+ BiFunction <List <InternalBucket >, AggregationReduceContext , InternalBucket > reduce = mockReduce (context );
53
+ DelayedBucket <InternalBucket > b = new DelayedBucket <>(
54
+ List .of (bucket ("test" , 1 , mockMultiBucketAgg ()), bucket ("test" , 2 , mockMultiBucketAgg ()))
55
+ );
56
+
57
+ assertThat (b .getDocCount (), equalTo (3L ));
58
+ assertThat (b .reduced (reduce , context ), sameInstance (b .reduced (reduce , context )));
59
+ assertThat (b .reduced (reduce , context ).getKeyAsString (), equalTo ("test" ));
60
+ assertThat (b .reduced (reduce , context ).getDocCount (), equalTo (3L ));
61
+ // it only accounts for sub-buckets
43
62
assertEquals (1 , buckets .get ());
44
63
}
45
64
@@ -76,6 +95,19 @@ public void testNonCompetitiveReduced() {
76
95
BiFunction <List <InternalBucket >, AggregationReduceContext , InternalBucket > reduce = mockReduce (context );
77
96
DelayedBucket <InternalBucket > b = new DelayedBucket <>(List .of (bucket ("test" , 1 )));
78
97
b .reduced (reduce , context );
98
+ // only account for sub-aggregations
99
+ assertEquals (0 , buckets .get ());
100
+ b .nonCompetitive (context );
101
+ assertEquals (0 , buckets .get ());
102
+ }
103
+
104
+ public void testNonCompetitiveReducedSubAggregation () {
105
+ AtomicInteger buckets = new AtomicInteger ();
106
+ AggregationReduceContext context = new AggregationReduceContext .ForFinal (null , null , () -> false , null , buckets ::addAndGet );
107
+ BiFunction <List <InternalBucket >, AggregationReduceContext , InternalBucket > reduce = mockReduce (context );
108
+ DelayedBucket <InternalBucket > b = new DelayedBucket <>(List .of (bucket ("test" , 1 , mockMultiBucketAgg ())));
109
+ b .reduced (reduce , context );
110
+ // only account for sub-aggregations
79
111
assertEquals (1 , buckets .get ());
80
112
b .nonCompetitive (context );
81
113
assertEquals (0 , buckets .get ());
@@ -85,10 +117,25 @@ private static InternalBucket bucket(String key, long docCount) {
85
117
return new StringTerms .Bucket (new BytesRef (key ), docCount , InternalAggregations .EMPTY , false , 0 , DocValueFormat .RAW );
86
118
}
87
119
120
+ private static InternalBucket bucket (String key , long docCount , InternalAggregations subAggregations ) {
121
+ return new StringTerms .Bucket (new BytesRef (key ), docCount , subAggregations , false , 0 , DocValueFormat .RAW );
122
+ }
123
+
88
124
static BiFunction <List <InternalBucket >, AggregationReduceContext , InternalBucket > mockReduce (AggregationReduceContext context ) {
89
125
return (l , c ) -> {
90
126
assertThat (c , sameInstance (context ));
91
- return bucket (l .get (0 ).getKeyAsString (), l .stream ().mapToLong (Bucket ::getDocCount ).sum ());
127
+ context .consumeBucketsAndMaybeBreak (l .get (0 ).getAggregations ().asList ().size ());
128
+ return bucket (l .get (0 ).getKeyAsString (), l .stream ().mapToLong (Bucket ::getDocCount ).sum (), l .get (0 ).getAggregations ());
92
129
};
93
130
}
131
+
132
+ @ SuppressWarnings ("unchecked" )
133
+ private InternalAggregations mockMultiBucketAgg () {
134
+ List <InternalBucket > buckets = List .of (bucket ("sub" , 1 ));
135
+ InternalMultiBucketAggregation <?, InternalBucket > mock = (InternalMultiBucketAggregation <?, InternalBucket >) mock (
136
+ InternalMultiBucketAggregation .class
137
+ );
138
+ when (mock .getBuckets ()).thenReturn (buckets );
139
+ return InternalAggregations .from (List .of (mock ));
140
+ }
94
141
}
0 commit comments