Skip to content

Commit

Permalink
Add terminate_after support
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Mar 28, 2023
1 parent b7af6f3 commit 44e405d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"require": {
"php": "^7.4 || ^8.0",
"babenkoivan/elastic-scout-driver": "^3.1"
"babenkoivan/elastic-scout-driver": "^3.1",
"babenkoivan/elastic-adapter": "^3.3"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
11 changes: 11 additions & 0 deletions docs/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [sort](#sort)
* [source](#source)
* [suggest](#suggest)
* [terminateAfter](#terminateafter)
* [trackScores](#trackscores)
* [trackTotalHits](#tracktotalhits)
* [unless](#unless)
Expand Down Expand Up @@ -473,6 +474,16 @@ $models = $firstSuggestion->models();
$raw = $firstSuggestion->raw();
```

### terminateAfter

This method allows you to set the maximum number of documents to collect for each shard:

```php
$searchResult = Book::searchQuery($query)
->terminateAfter(10)
->execute();
```

### trackScores

This method forces [scores to be computed and tracked](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#_track_scores):
Expand Down
11 changes: 11 additions & 0 deletions src/Builders/SearchParametersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class SearchParametersBuilder
private ?array $searchAfter;
private ?array $routing;
private ?bool $explain;
private ?int $terminateAfter;

public function __construct(Model $model)
{
Expand Down Expand Up @@ -305,6 +306,12 @@ public function explain(bool $explain): self
return $this;
}

public function terminateAfter(int $terminateAfter): self
{
$this->terminateAfter = $terminateAfter;
return $this;
}

public function buildSearchParameters(): SearchParameters
{
$searchParameters = new SearchParameters();
Expand Down Expand Up @@ -395,6 +402,10 @@ public function buildSearchParameters(): SearchParameters
$searchParameters->explain($this->explain);
}

if (isset($this->terminateAfter)) {
$searchParameters->terminateAfter($this->terminateAfter);
}

return $searchParameters;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/Builders/SearchParametersBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,17 @@ public function test_search_parameters_with_explain_can_be_built(): void

$this->assertEquals($expected, $actual);
}

public function test_search_parameters_with_terminate_after_can_be_built(): void
{
$expected = (new SearchParameters())
->indices([(new Book())->searchableAs()])
->terminateAfter(10);

$actual = (new SearchParametersBuilder(new Book()))
->terminateAfter(10)
->buildSearchParameters();

$this->assertEquals($expected, $actual);
}
}
11 changes: 11 additions & 0 deletions tests/Integration/Queries/RawQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,4 +628,15 @@ public function test_query_can_be_explained(): void

$this->assertGreaterThan(0, $found->hits()->first()->explanation()->value());
}

public function test_query_can_be_terminated(): void
{
factory(Book::class, 10)->create([
'author_id' => factory(Author::class)->create(),
]);

$found = Book::searchQuery()->terminateAfter(1)->execute();

$this->assertCount(1, $found);
}
}

0 comments on commit 44e405d

Please sign in to comment.