Skip to content

Commit

Permalink
Add request_cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Mar 28, 2023
1 parent 44e405d commit 5cd1965
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"require": {
"php": "^7.4 || ^8.0",
"babenkoivan/elastic-scout-driver": "^3.1",
"babenkoivan/elastic-adapter": "^3.3"
"babenkoivan/elastic-adapter": "^3.4"
},
"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 @@ -12,6 +12,7 @@
* [postFilter](#postfilter)
* [preference](#preference)
* [refineModels](#refinemodels)
* [requestCache](#requestcache)
* [rescore](#rescore)
* [routing](#routing)
* [searchAfter](#searchafter)
Expand Down Expand Up @@ -300,6 +301,16 @@ $models = Book::searchQuery($query)
->models();
```

### requestCache

This method allows you to [enable or disable cache per request](https://www.elastic.co/guide/en/elasticsearch/reference/current/shard-request-cache.html#_enabling_and_disabling_caching_per_request):

```php
$searchResult = Book::searchQuery($query)
->requestCache(true)
->execute();
```

### rescore

This method allows you to [rescore](https://www.elastic.co/guide/en/elasticsearch/reference/current/filter-search-results.html#rescore)
Expand Down
11 changes: 11 additions & 0 deletions src/Builders/SearchParametersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SearchParametersBuilder
private ?array $routing;
private ?bool $explain;
private ?int $terminateAfter;
private ?bool $requestCache;

public function __construct(Model $model)
{
Expand Down Expand Up @@ -312,6 +313,12 @@ public function terminateAfter(int $terminateAfter): self
return $this;
}

public function requestCache(bool $requestCache): self
{
$this->requestCache = $requestCache;
return $this;
}

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

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

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 @@ -635,4 +635,17 @@ public function test_search_parameters_with_terminate_after_can_be_built(): void

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

public function test_search_parameters_with_request_cache_can_be_built(): void
{
$expected = (new SearchParameters())
->indices([(new Book())->searchableAs()])
->requestCache(true);

$actual = (new SearchParametersBuilder(new Book()))
->requestCache(true)
->buildSearchParameters();

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

0 comments on commit 5cd1965

Please sign in to comment.