55use Ensi \LaravelElasticQuery \Aggregating \Bucket ;
66use Ensi \LaravelElasticQuery \Aggregating \Bucket \TermsAggregation ;
77use Ensi \LaravelElasticQuery \Aggregating \BucketCollection ;
8+ use Ensi \LaravelElasticQuery \Aggregating \Metrics \MinMaxScoreAggregation ;
9+ use Ensi \LaravelElasticQuery \Aggregating \MinMax ;
10+ use Ensi \LaravelElasticQuery \Search \Sorting \Sort ;
811use Ensi \LaravelElasticQuery \Tests \AssertsArray ;
912use Ensi \LaravelElasticQuery \Tests \Unit \UnitTestCase ;
1013
@@ -26,6 +29,58 @@ public function testToDSLWithSize(): void
2629 $ this ->assertArrayStructure (['agg1 ' => ['terms ' => ['field ' , 'size ' ]]], $ testing ->toDSL ());
2730 }
2831
32+ public function testToDSLWithSort (): void
33+ {
34+ $ orderField = 'name ' ;
35+ $ sort = new Sort ($ orderField );
36+
37+ $ testing = new TermsAggregation (
38+ name: 'agg1 ' ,
39+ field: 'code ' ,
40+ sort: $ sort
41+ );
42+
43+ $ this ->assertArrayStructure (['agg1 ' => ['terms ' => ['field ' , 'order ' => [$ orderField ]]]], $ testing ->toDSL ());
44+ }
45+
46+ public function testToDSLWithComposite (): void
47+ {
48+ $ composite = new MinMaxScoreAggregation ();
49+
50+ $ testing = new TermsAggregation (
51+ name: 'agg1 ' ,
52+ field: 'code ' ,
53+ composite: $ composite
54+ );
55+
56+ $ this ->assertArrayStructure (['agg1 ' => ['terms ' => ['field ' ], 'aggs ' => [
57+ 'score_min ' => ['min ' => ['script ' ]],
58+ 'score_max ' => ['max ' => ['script ' ]],
59+ ]]], $ testing ->toDSL ());
60+ }
61+
62+ public function testToDSLWithAll (): void
63+ {
64+ $ orderField = 'name ' ;
65+ $ sort = new Sort ($ orderField );
66+ $ composite = new MinMaxScoreAggregation ();
67+
68+ $ testing = new TermsAggregation (
69+ name: 'agg1 ' ,
70+ field: 'code ' ,
71+ size: 24 ,
72+ sort: $ sort ,
73+ composite: $ composite
74+ );
75+
76+ $ this ->assertArrayStructure ([
77+ 'agg1 ' => ['terms ' => ['field ' , 'size ' , 'order ' => [$ orderField ]],
78+ 'aggs ' => [
79+ 'score_min ' => ['min ' => ['script ' ]],
80+ 'score_max ' => ['max ' => ['script ' ]],
81+ ]]], $ testing ->toDSL ());
82+ }
83+
2984 public function testParseResults (): void
3085 {
3186 $ result = $ this ->executeParseResults ('agg1 ' );
@@ -47,6 +102,29 @@ public function testParseResultsReadsBuckets(): void
47102 $ this ->assertInstanceOf (Bucket::class, $ result ['agg1 ' ]->first ());
48103 }
49104
105+ public function testParseResultsReadsBucketsWithComposite (): void
106+ {
107+ $ buckets = [
108+ [
109+ 'key ' => 'tv ' ,
110+ 'doc_count ' => 4 ,
111+ 'score_max ' => ['value ' => 2 ],
112+ 'score_min ' => ['value ' => 1 ],
113+ ],
114+ ];
115+
116+ $ result = $ this ->executeParseResultsWithComposite ('agg1 ' , $ buckets );
117+
118+ /** @var Bucket $bucket */
119+ $ bucket = $ result ['agg1 ' ]->first ();
120+ /** @var MinMax $score */
121+ $ score = $ bucket ->getCompositeValue ('score ' );
122+
123+ $ this ->assertInstanceOf (Bucket::class, $ bucket );
124+ $ this ->assertEquals ($ buckets [0 ]['score_min ' ]['value ' ], $ score ->min );
125+ $ this ->assertEquals ($ buckets [0 ]['score_max ' ]['value ' ], $ score ->max );
126+ }
127+
50128 public function testParseEmptyResults (): void
51129 {
52130 $ result = $ this ->executeParseResults ('agg1 ' , []);
@@ -55,6 +133,14 @@ public function testParseEmptyResults(): void
55133 $ this ->assertInstanceOf (BucketCollection::class, $ result ['agg1 ' ]);
56134 }
57135
136+ public function testParseEmptyResultsWithComposite (): void
137+ {
138+ $ result = $ this ->executeParseResultsWithComposite ('agg1 ' , []);
139+
140+ $ this ->assertArrayHasKey ('agg1 ' , $ result );
141+ $ this ->assertInstanceOf (BucketCollection::class, $ result ['agg1 ' ]);
142+ }
143+
58144 private function executeParseResults (string $ aggName , ?array $ buckets = null ): array
59145 {
60146 if ($ buckets === null ) {
@@ -70,4 +156,32 @@ private function executeParseResults(string $aggName, ?array $buckets = null): a
70156
71157 return $ testing ->parseResults ($ response );
72158 }
159+
160+ private function executeParseResultsWithComposite (string $ aggName , ?array $ buckets = null ): array
161+ {
162+ if ($ buckets === null ) {
163+ $ buckets = [
164+ [
165+ 'key ' => 'tv ' ,
166+ 'doc_count ' => 4 ,
167+ 'score_max ' => ['value ' => 1 ],
168+ 'score_min ' => ['value ' => 1 ],
169+ ],
170+ ];
171+ }
172+
173+ $ response = [$ aggName => [
174+ 'doc_count_error_upper_bound ' => 0 ,
175+ 'buckets ' => $ buckets ,
176+ ]];
177+
178+ $ composite = new MinMaxScoreAggregation ();
179+ $ testing = new TermsAggregation (
180+ name: $ aggName ,
181+ field: 'code ' ,
182+ composite: $ composite
183+ );
184+
185+ return $ testing ->parseResults ($ response );
186+ }
73187}
0 commit comments