@@ -272,23 +272,21 @@ void terms() {
272
272
void terms_value () {
273
273
withinSearchSession ( searchSession -> {
274
274
// tag::terms-sum[]
275
- AggregationKey <Map <Double , Double >> sumByPriceKey = AggregationKey .of ( "sumByPrice " );
275
+ AggregationKey <Map <Genre , Double >> sumByCategoryKey = AggregationKey .of ( "sumByCategory " );
276
276
SearchResult <Book > result = searchSession .search ( Book .class )
277
277
.where ( f -> f .matchAll () )
278
278
.aggregation (
279
- sumByPriceKey , f -> f .terms ()
280
- .field ( "price " , Double .class ) // <1>
281
- .value ( f .sum ().field ( "price" , Double .class ) )
279
+ sumByCategoryKey , f -> f .terms ()
280
+ .field ( "genre " , Genre .class ) // <1>
281
+ .value ( f .sum ().field ( "price" , Double .class ) ) // <2>
282
282
)
283
283
.fetch ( 20 );
284
- Map <Double , Double > sumByPrice = result .aggregation ( sumByPriceKey );
284
+ Map <Genre , Double > sumByPrice = result .aggregation ( sumByCategoryKey );
285
285
// end::terms-sum[]
286
286
assertThat ( sumByPrice )
287
287
.containsExactly (
288
- entry ( 7.99 , 7.99 ),
289
- entry ( 15.99 , 15.99 ),
290
- entry ( 19.99 , 19.99 ),
291
- entry ( 24.99 , 24.99 )
288
+ entry ( Genre .SCIENCE_FICTION , 60.97 ),
289
+ entry ( Genre .CRIME_FICTION , 7.99 )
292
290
);
293
291
} );
294
292
@@ -339,26 +337,26 @@ void terms_value() {
339
337
@ Test
340
338
void range_value () {
341
339
withinSearchSession ( searchSession -> {
342
- // tag::range-sum []
343
- AggregationKey <Map <Range <Double >, Double >> countsByPriceKey = AggregationKey .of ( "countsByPrice " );
340
+ // tag::range-avg []
341
+ AggregationKey <Map <Range <Double >, Double >> avgRatingByPriceKey = AggregationKey .of ( "avgRatingByPrice " );
344
342
SearchResult <Book > result = searchSession .search ( Book .class )
345
343
.where ( f -> f .matchAll () )
346
344
.aggregation (
347
- countsByPriceKey , f -> f .range ()
345
+ avgRatingByPriceKey , f -> f .range ()
348
346
.field ( "price" , Double .class ) // <1>
349
- .range ( 0.0 , 10.0 ) // <2>
347
+ .range ( 0.0 , 10.0 )
350
348
.range ( 10.0 , 20.0 )
351
- .range ( 20.0 , null ) // <3>
352
- .value ( f .sum ().field ( "price " , Double .class ) )
349
+ .range ( 20.0 , null )
350
+ .value ( f .avg ().field ( "ratings " , Double .class , ValueModel . RAW ) ) // <2>
353
351
)
354
352
.fetch ( 20 );
355
- Map <Range <Double >, Double > countsByPrice = result .aggregation ( countsByPriceKey );
356
- // end::range-sum []
353
+ Map <Range <Double >, Double > countsByPrice = result .aggregation ( avgRatingByPriceKey );
354
+ // end::range-avg []
357
355
assertThat ( countsByPrice )
358
356
.containsExactly (
359
- entry ( Range .canonical ( 0.0 , 10.0 ), 7.99 ),
360
- entry ( Range .canonical ( 10.0 , 20.0 ), 35.98 ),
361
- entry ( Range .canonical ( 20.0 , null ), 24.99 )
357
+ entry ( Range .canonical ( 0.0 , 10.0 ), 4.0 ),
358
+ entry ( Range .canonical ( 10.0 , 20.0 ), 3.6 ),
359
+ entry ( Range .canonical ( 20.0 , null ), 3.2 )
362
360
);
363
361
} );
364
362
@@ -584,8 +582,8 @@ void sum() {
584
582
.aggregation ( sumPricesKey , f -> f .sum ().field ( "price" , Double .class ) ) // <1>
585
583
.fetch ( 20 );
586
584
Double sumPrices = result .aggregation ( sumPricesKey );
587
- assertThat ( sumPrices ).isEqualTo ( 60.97 );
588
585
// end::sums[]
586
+ assertThat ( sumPrices ).isEqualTo ( 60.97 );
589
587
} );
590
588
}
591
589
@@ -599,8 +597,8 @@ void min() {
599
597
.aggregation ( oldestReleaseKey , f -> f .min ().field ( "releaseDate" , Date .class ) ) // <1>
600
598
.fetch ( 20 );
601
599
Date oldestRelease = result .aggregation ( oldestReleaseKey );
602
- assertThat ( oldestRelease ).isEqualTo ( Date .valueOf ( "1950-12-02" ) );
603
600
// end::min[]
601
+ assertThat ( oldestRelease ).isEqualTo ( Date .valueOf ( "1950-12-02" ) );
604
602
} );
605
603
}
606
604
@@ -614,28 +612,43 @@ void max() {
614
612
.aggregation ( mostRecentReleaseKey , f -> f .max ().field ( "releaseDate" , Date .class ) ) // <1>
615
613
.fetch ( 20 );
616
614
Date mostRecentRelease = result .aggregation ( mostRecentReleaseKey );
617
-
618
615
// end::max[]
616
+ assertThat ( mostRecentRelease ).isEqualTo ( Date .valueOf ( "1983-01-01" ) );
619
617
} );
620
618
}
621
619
622
620
@ Test
623
- void count () {
621
+ void countDocuments () {
624
622
withinSearchSession ( searchSession -> {
625
- // tag::count[]
626
- AggregationKey <Long > countPricesKey = AggregationKey .of ( "countPrices " );
623
+ // tag::count-documents []
624
+ AggregationKey <Long > countBooksKey = AggregationKey .of ( "countBooks " );
627
625
SearchResult <Book > result = searchSession .search ( Book .class )
628
626
.where ( f -> f .match ().field ( "genre" ).matching ( Genre .SCIENCE_FICTION ) )
629
- .aggregation ( countPricesKey , f -> f .countValues (). field ( "price" ) ) // <1>
627
+ .aggregation ( countBooksKey , f -> f .countDocuments ( ) ) // <1>
630
628
.fetch ( 20 );
631
- Long countPrices = result .aggregation ( countPricesKey );
629
+ Long countPrices = result .aggregation ( countBooksKey );
630
+ // end::count-documents[]
632
631
assertThat ( countPrices ).isEqualTo ( 3L );
632
+ } );
633
+ }
634
+
635
+ @ Test
636
+ void countValues () {
637
+ withinSearchSession ( searchSession -> {
638
+ // tag::count[]
639
+ AggregationKey <Long > countRatingsKey = AggregationKey .of ( "countRatings" );
640
+ SearchResult <Book > result = searchSession .search ( Book .class )
641
+ .where ( f -> f .match ().field ( "genre" ).matching ( Genre .SCIENCE_FICTION ) )
642
+ .aggregation ( countRatingsKey , f -> f .countValues ().field ( "ratings" ) ) // <1>
643
+ .fetch ( 20 );
644
+ Long countPrices = result .aggregation ( countRatingsKey );
633
645
// end::count[]
646
+ assertThat ( countPrices ).isEqualTo ( 15L );
634
647
} );
635
648
}
636
649
637
650
@ Test
638
- void countDistinct () {
651
+ void countDistinctValues () {
639
652
withinSearchSession ( searchSession -> {
640
653
// tag::count-distinct[]
641
654
AggregationKey <Long > countDistinctPricesKey = AggregationKey .of ( "countDistinctPrices" );
@@ -644,8 +657,8 @@ void countDistinct() {
644
657
.aggregation ( countDistinctPricesKey , f -> f .countDistinctValues ().field ( "price" ) ) // <1>
645
658
.fetch ( 20 );
646
659
Long countDistinctPrices = result .aggregation ( countDistinctPricesKey );
647
- assertThat ( countDistinctPrices ).isEqualTo ( 3L );
648
660
// end::count-distinct[]
661
+ assertThat ( countDistinctPrices ).isEqualTo ( 3L );
649
662
} );
650
663
}
651
664
@@ -679,6 +692,7 @@ private void initData() {
679
692
book1 .setPrice ( 24.99 );
680
693
book1 .setGenre ( Genre .SCIENCE_FICTION );
681
694
book1 .setReleaseDate ( Date .valueOf ( "1950-12-02" ) );
695
+ book1 .setRatings ( List .of ( 5 , 5 , 4 , 2 , 0 ) );
682
696
addEdition ( book1 , "Mass Market Paperback, 1st Edition" , 9.99 );
683
697
addEdition ( book1 , "Kindle" , 9.99 );
684
698
@@ -688,6 +702,7 @@ private void initData() {
688
702
book2 .setPrice ( 19.99 );
689
703
book2 .setGenre ( Genre .SCIENCE_FICTION );
690
704
book2 .setReleaseDate ( Date .valueOf ( "1953-10-01" ) );
705
+ book2 .setRatings ( List .of ( 5 , 5 , 3 , 3 , 5 ) );
691
706
addEdition ( book2 , "Mass Market Paperback, 12th Edition" , 4.99 );
692
707
addEdition ( book2 , "Kindle" , 19.99 );
693
708
@@ -697,6 +712,7 @@ private void initData() {
697
712
book3 .setPrice ( 15.99 );
698
713
book3 .setGenre ( Genre .SCIENCE_FICTION );
699
714
book3 .setReleaseDate ( Date .valueOf ( "1983-01-01" ) );
715
+ book3 .setRatings ( List .of ( 3 , 3 , 3 , 3 , 3 ) );
700
716
addEdition ( book3 , "Mass Market Paperback, 59th Edition" , 3.99 );
701
717
addEdition ( book3 , "Kindle" , 5.99 );
702
718
@@ -706,6 +722,7 @@ private void initData() {
706
722
book4 .setPrice ( 7.99 );
707
723
book4 .setGenre ( Genre .CRIME_FICTION );
708
724
book4 .setReleaseDate ( Date .valueOf ( "2008-02-05" ) );
725
+ book4 .setRatings ( List .of ( 4 , 4 , 4 , 4 , 4 ) );
709
726
addEdition ( book4 , "Mass Market Paperback, 2nd Edition" , 10.99 );
710
727
addEdition ( book4 , "Kindle" , 12.99 );
711
728
0 commit comments