File tree Expand file tree Collapse file tree 5 files changed +48
-1
lines changed
Expand file tree Collapse file tree 5 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 3939 },
4040 "require" : {
4141 "php" : " ^7.4 || ^8.0" ,
42- "babenkoivan/elastic-scout-driver" : " ^3.1"
42+ "babenkoivan/elastic-scout-driver" : " ^3.1" ,
43+ "babenkoivan/elastic-adapter" : " ^3.3"
4344 },
4445 "require-dev" : {
4546 "phpunit/phpunit" : " ^9.5" ,
Original file line number Diff line number Diff line change 2020* [ sort] ( #sort )
2121* [ source] ( #source )
2222* [ suggest] ( #suggest )
23+ * [ terminateAfter] ( #terminateafter )
2324* [ trackScores] ( #trackscores )
2425* [ trackTotalHits] ( #tracktotalhits )
2526* [ unless] ( #unless )
@@ -473,6 +474,16 @@ $models = $firstSuggestion->models();
473474$raw = $firstSuggestion->raw();
474475```
475476
477+ ### terminateAfter
478+
479+ This method allows you to set the maximum number of documents to collect for each shard:
480+
481+ ``` php
482+ $searchResult = Book::searchQuery($query)
483+ ->terminateAfter(10)
484+ ->execute();
485+ ```
486+
476487### trackScores
477488
478489This method forces [ scores to be computed and tracked] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#_track_scores ) :
Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ class SearchParametersBuilder
5959 private ?array $ searchAfter ;
6060 private ?array $ routing ;
6161 private ?bool $ explain ;
62+ private ?int $ terminateAfter ;
6263
6364 public function __construct (Model $ model )
6465 {
@@ -305,6 +306,12 @@ public function explain(bool $explain): self
305306 return $ this ;
306307 }
307308
309+ public function terminateAfter (int $ terminateAfter ): self
310+ {
311+ $ this ->terminateAfter = $ terminateAfter ;
312+ return $ this ;
313+ }
314+
308315 public function buildSearchParameters (): SearchParameters
309316 {
310317 $ searchParameters = new SearchParameters ();
@@ -395,6 +402,10 @@ public function buildSearchParameters(): SearchParameters
395402 $ searchParameters ->explain ($ this ->explain );
396403 }
397404
405+ if (isset ($ this ->terminateAfter )) {
406+ $ searchParameters ->terminateAfter ($ this ->terminateAfter );
407+ }
408+
398409 return $ searchParameters ;
399410 }
400411
Original file line number Diff line number Diff line change @@ -622,4 +622,17 @@ public function test_search_parameters_with_explain_can_be_built(): void
622622
623623 $ this ->assertEquals ($ expected , $ actual );
624624 }
625+
626+ public function test_search_parameters_with_terminate_after_can_be_built (): void
627+ {
628+ $ expected = (new SearchParameters ())
629+ ->indices ([(new Book ())->searchableAs ()])
630+ ->terminateAfter (10 );
631+
632+ $ actual = (new SearchParametersBuilder (new Book ()))
633+ ->terminateAfter (10 )
634+ ->buildSearchParameters ();
635+
636+ $ this ->assertEquals ($ expected , $ actual );
637+ }
625638}
Original file line number Diff line number Diff line change @@ -628,4 +628,15 @@ public function test_query_can_be_explained(): void
628628
629629 $ this ->assertGreaterThan (0 , $ found ->hits ()->first ()->explanation ()->value ());
630630 }
631+
632+ public function test_query_can_be_terminated (): void
633+ {
634+ factory (Book::class, 10 )->create ([
635+ 'author_id ' => factory (Author::class)->create (),
636+ ]);
637+
638+ $ found = Book::searchQuery ()->terminateAfter (1 )->execute ();
639+
640+ $ this ->assertCount (1 , $ found );
641+ }
631642}
You can’t perform that action at this time.
0 commit comments