Skip to content

Commit

Permalink
The query parameter is now optional when making a new search request
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Feb 7, 2022
1 parent 5cda208 commit 64515c4
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
/.phpunit.result.cache
/.php_cs
/.php_cs.cache
/.php-cs-fixer.cache
/phpstan.neon
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"require": {
"php": "^7.3 || ^8.0",
"babenkoivan/elastic-adapter": "^2.1",
"babenkoivan/elastic-adapter": "^2.3",
"babenkoivan/elastic-scout-driver": "^2.0"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions docs/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This method can be used to [aggregate data](https://www.elastic.co/guide/en/elas
based on a search query;

```php
$searchResult = Book::searchQuery($query)
$searchResult = Book::searchQuery()
->aggregate('max_price', [
'max' => [
'field' => 'price',
Expand All @@ -37,7 +37,7 @@ $searchResult = Book::searchQuery($query)
Alternatively you can use the `aggregateRaw` method:

```php
$searchResult = Book::searchQuery($query)
$searchResult = Book::searchQuery()
->aggregateRaw([
'max_price' => [
'max' => [
Expand Down
6 changes: 3 additions & 3 deletions src/Builders/SearchRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SearchRequestBuilder
public const DEFAULT_PAGE_SIZE = 10;

/**
* @var array
* @var array|null
*/
private $query;
/**
Expand Down Expand Up @@ -88,11 +88,11 @@ class SearchRequestBuilder
private $indicesBoost = [];

/**
* @param Closure|QueryBuilderInterface|array $query
* @param Closure|QueryBuilderInterface|array|null $query
*/
public function __construct($query, Model $model)
{
$this->query = ParameterFactory::makeQuery($query);
$this->query = isset($query) ? ParameterFactory::makeQuery($query) : null;
$this->modelScope = new ModelScope(get_class($model));
$this->engine = $model->searchableUsing();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ trait Searchable
}

/**
* @param Closure|QueryBuilderInterface|array $query
* @param Closure|QueryBuilderInterface|array|null $query
*/
public static function searchQuery($query): SearchRequestBuilder
public static function searchQuery($query = null): SearchRequestBuilder
{
return new SearchRequestBuilder($query, new static());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Builders/SearchRequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ public function test_search_request_with_raw_aggregate_can_be_built(): void
],
];

$expected = (new SearchRequest($this->matchAllQuery))
$expected = (new SearchRequest())
->aggregations($aggregations);

$actual = (new SearchRequestBuilder($this->matchAllQuery, new Book()))
$actual = (new SearchRequestBuilder(null, new Book()))
->aggregateRaw($aggregations)
->buildSearchRequest();

Expand Down
6 changes: 2 additions & 4 deletions tests/Integration/Queries/RawQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function test_document_data_can_be_analyzed_using_raw_aggregations(): voi
$minPrice = $source->min('price');
$maxPrice = $source->max('price');

$found = Book::searchQuery(['match_all' => new stdClass()])
$found = Book::searchQuery()
->aggregateRaw([
'min_price' => [
'min' => [
Expand All @@ -278,7 +278,6 @@ public function test_document_data_can_be_analyzed_using_raw_aggregations(): voi
],
],
])
->size(0)
->execute();

$this->assertEquals($minPrice, $found->aggregations()->get('min_price')->raw()['value']);
Expand All @@ -291,13 +290,12 @@ public function test_document_data_can_be_analyzed_using_aggregations(): void
->state('belongs_to_author')
->create();

$found = Book::searchQuery(['match_all' => new stdClass()])
$found = Book::searchQuery()
->aggregate('max_price', [
'max' => [
'field' => 'price',
],
])
->size(0)
->execute();

$this->assertEquals($source->max('price'), $found->aggregations()->get('max_price')->raw()['value']);
Expand Down

0 comments on commit 64515c4

Please sign in to comment.