|
3 | 3 | namespace Ensi\LaravelElasticQuery\Tests\Functional\Aggregating; |
4 | 4 |
|
5 | 5 | use Ensi\LaravelElasticQuery\Aggregating\AggregationsQuery; |
| 6 | +use Ensi\LaravelElasticQuery\Aggregating\Bucket; |
| 7 | +use Ensi\LaravelElasticQuery\Aggregating\Metrics\MinMaxScoreAggregation; |
6 | 8 | use Ensi\LaravelElasticQuery\Aggregating\MinMax; |
7 | 9 | use Ensi\LaravelElasticQuery\Contracts\AggregationsBuilder; |
| 10 | +use Ensi\LaravelElasticQuery\Search\Sorting\Sort; |
8 | 11 | use Ensi\LaravelElasticQuery\Tests\Functional\ElasticTestCase; |
9 | 12 | use Ensi\LaravelElasticQuery\Tests\Models\ProductsIndex; |
10 | 13 | use Ensi\LaravelElasticQuery\Tests\Seeds\ProductIndexSeeder; |
@@ -77,4 +80,30 @@ public function testTermsSize(): void |
77 | 80 |
|
78 | 81 | $this->assertCount(1, $results->get('codes')); |
79 | 82 | } |
| 83 | + |
| 84 | + public function testTermsWithSortByCompositeValue(): void |
| 85 | + { |
| 86 | + $sort = new Sort('score_max'); |
| 87 | + $composite = new MinMaxScoreAggregation(); |
| 88 | + |
| 89 | + $this->testing |
| 90 | + ->whereMatch('description', 'water') |
| 91 | + ->where('package', 'bottle') |
| 92 | + ->terms( |
| 93 | + name: 'codes', |
| 94 | + field: 'code', |
| 95 | + size: 2, |
| 96 | + sort: $sort, |
| 97 | + composite: $composite |
| 98 | + ); |
| 99 | + |
| 100 | + $results = $this->testing->get(); |
| 101 | + |
| 102 | + $scores = $results->get('codes')->map( |
| 103 | + fn (Bucket $bucket) => $bucket->getCompositeValue('score')->max |
| 104 | + ); |
| 105 | + |
| 106 | + $this->assertCount(2, $results->get('codes')); |
| 107 | + $this->assertGreaterThanOrEqual($scores->first(), $scores->last()); |
| 108 | + } |
80 | 109 | } |
0 commit comments