18
18
import static org .assertj .core .api .Assertions .*;
19
19
import static org .elasticsearch .index .query .QueryBuilders .*;
20
20
import static org .elasticsearch .search .aggregations .AggregationBuilders .*;
21
+ import static org .elasticsearch .search .aggregations .PipelineAggregatorBuilders .*;
21
22
import static org .springframework .data .elasticsearch .annotations .FieldType .*;
22
23
import static org .springframework .data .elasticsearch .annotations .FieldType .Integer ;
23
24
26
27
import java .util .List ;
27
28
28
29
import org .elasticsearch .action .search .SearchType ;
30
+ import org .elasticsearch .search .aggregations .Aggregation ;
29
31
import org .elasticsearch .search .aggregations .Aggregations ;
32
+ import org .elasticsearch .search .aggregations .pipeline .InternalStatsBucket ;
33
+ import org .elasticsearch .search .aggregations .pipeline .ParsedStatsBucket ;
34
+ import org .elasticsearch .search .aggregations .pipeline .StatsBucket ;
30
35
import org .junit .jupiter .api .AfterEach ;
31
36
import org .junit .jupiter .api .BeforeEach ;
37
+ import org .junit .jupiter .api .DisplayName ;
32
38
import org .junit .jupiter .api .Test ;
33
39
import org .springframework .beans .factory .annotation .Autowired ;
34
40
import org .springframework .context .annotation .Configuration ;
@@ -109,7 +115,7 @@ public void after() {
109
115
indexOperations .delete ();
110
116
}
111
117
112
- @ Test
118
+ @ Test // DATAES-96
113
119
public void shouldReturnAggregatedResponseForGivenSearchQuery () {
114
120
115
121
// given
@@ -130,6 +136,56 @@ public void shouldReturnAggregatedResponseForGivenSearchQuery() {
130
136
assertThat (searchHits .hasSearchHits ()).isFalse ();
131
137
}
132
138
139
+ @ Test // #1255
140
+ @ DisplayName ("should work with pipeline aggregations" )
141
+ void shouldWorkWithPipelineAggregations () {
142
+
143
+ IndexInitializer .init (operations .indexOps (PipelineAggsEntity .class ));
144
+ operations .save ( //
145
+ new PipelineAggsEntity ("1-1" , "one" ), //
146
+ new PipelineAggsEntity ("2-1" , "two" ), //
147
+ new PipelineAggsEntity ("2-2" , "two" ), //
148
+ new PipelineAggsEntity ("3-1" , "three" ), //
149
+ new PipelineAggsEntity ("3-2" , "three" ), //
150
+ new PipelineAggsEntity ("3-3" , "three" ) //
151
+ ); //
152
+
153
+ NativeSearchQuery searchQuery = new NativeSearchQueryBuilder () //
154
+ .withQuery (matchAllQuery ()) //
155
+ .withSearchType (SearchType .DEFAULT ) //
156
+ .addAggregation (terms ("keyword_aggs" ).field ("keyword" )) //
157
+ .addAggregation (statsBucket ("keyword_bucket_stats" , "keyword_aggs._count" )) //
158
+ .withMaxResults (0 ) //
159
+ .build ();
160
+
161
+ SearchHits <PipelineAggsEntity > searchHits = operations .search (searchQuery , PipelineAggsEntity .class );
162
+
163
+ Aggregations aggregations = searchHits .getAggregations ();
164
+ assertThat (aggregations ).isNotNull ();
165
+ assertThat (aggregations .asMap ().get ("keyword_aggs" )).isNotNull ();
166
+ Aggregation keyword_bucket_stats = aggregations .asMap ().get ("keyword_bucket_stats" );
167
+ assertThat (keyword_bucket_stats ).isInstanceOf (StatsBucket .class );
168
+ if (keyword_bucket_stats instanceof ParsedStatsBucket ) {
169
+ // Rest client
170
+ ParsedStatsBucket statsBucket = (ParsedStatsBucket ) keyword_bucket_stats ;
171
+ assertThat (statsBucket .getMin ()).isEqualTo (1.0 );
172
+ assertThat (statsBucket .getMax ()).isEqualTo (3.0 );
173
+ assertThat (statsBucket .getAvg ()).isEqualTo (2.0 );
174
+ assertThat (statsBucket .getSum ()).isEqualTo (6.0 );
175
+ assertThat (statsBucket .getCount ()).isEqualTo (3L );
176
+ }
177
+ if (keyword_bucket_stats instanceof InternalStatsBucket ) {
178
+ // transport client
179
+ InternalStatsBucket statsBucket = (InternalStatsBucket ) keyword_bucket_stats ;
180
+ assertThat (statsBucket .getMin ()).isEqualTo (1.0 );
181
+ assertThat (statsBucket .getMax ()).isEqualTo (3.0 );
182
+ assertThat (statsBucket .getAvg ()).isEqualTo (2.0 );
183
+ assertThat (statsBucket .getSum ()).isEqualTo (6.0 );
184
+ assertThat (statsBucket .getCount ()).isEqualTo (3L );
185
+ }
186
+ }
187
+
188
+ // region entities
133
189
@ Document (indexName = "test-index-articles-core-aggregation" )
134
190
static class ArticleEntity {
135
191
@@ -256,4 +312,34 @@ public IndexQuery buildIndex() {
256
312
}
257
313
}
258
314
315
+ @ Document (indexName = "pipeline-aggs" )
316
+ static class PipelineAggsEntity {
317
+ @ Id private String id ;
318
+ @ Field (type = Keyword ) private String keyword ;
319
+
320
+ public PipelineAggsEntity () {}
321
+
322
+ public PipelineAggsEntity (String id , String keyword ) {
323
+ this .id = id ;
324
+ this .keyword = keyword ;
325
+ }
326
+
327
+ public String getId () {
328
+ return id ;
329
+ }
330
+
331
+ public void setId (String id ) {
332
+ this .id = id ;
333
+ }
334
+
335
+ public String getKeyword () {
336
+ return keyword ;
337
+ }
338
+
339
+ public void setKeyword (String keyword ) {
340
+ this .keyword = keyword ;
341
+ }
342
+ }
343
+ // endregion
344
+
259
345
}
0 commit comments