Skip to content

Commit

Permalink
Add aggregations support
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Jul 1, 2020
1 parent b5457ec commit c6ff71d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ $request->setHighlight([
]);

$request->setSuggest([
'my_suggest' => [
'message_suggest' => [
'text' => 'test',
'term' => [
'field' => 'message'
Expand All @@ -246,6 +246,14 @@ $request->setCollapse([
'field' => 'user'
]);

$request->setAggregations([
'max_likes' => [
'max' => [
'field' => 'likes'
]
]
]);

$request->setSort([
['post_date' => ['order' => 'asc']],
'_score'
Expand All @@ -270,4 +278,7 @@ foreach ($hits as $hit) {

// suggestions
$suggestions = $response->getSuggestions();

// aggregations
$aggregations = $response->getAggregations();
```
12 changes: 11 additions & 1 deletion src/Search/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ final class SearchRequest implements ArrayableInterface
* @var array|null
*/
private $collapse;
/**
* @var array|null
*/
private $aggregations;

public function __construct(array $query)
{
Expand Down Expand Up @@ -91,13 +95,19 @@ public function setCollapse(array $collapse): self
return $this;
}

public function setAggregations(array $aggregations): self
{
$this->aggregations = $aggregations;
return $this;
}

public function toArray(): array
{
$request = [
'query' => $this->query
];

foreach (['highlight', 'sort', 'from', 'size', 'suggest', 'collapse'] as $property) {
foreach (['highlight', 'sort', 'from', 'size', 'suggest', 'collapse', 'aggregations'] as $property) {
if (isset($this->$property)) {
$request[$property] = $this->$property;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Search/SearchResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function getSuggestions(): array
}, $this->response['suggest'] ?? []);
}

public function getAggregations(): array
{
return $this->response['aggregations'] ?? [];
}

public function getRaw(): array
{
return $this->response;
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/Search/SearchRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,32 @@ public function test_array_casting_with_collapse(): void
]
], $request->toArray());
}

public function test_array_casting_with_aggregations(): void
{
$request = new SearchRequest([
'match_all' => new stdClass()
]);

$request->setAggregations([
'min_price' => [
'min' => [
'field' => 'price'
]
]
]);

$this->assertEquals([
'query' => [
'match_all' => new stdClass()
],
'aggregations' => [
'min_price' => [
'min' => [
'field' => 'price'
]
]
]
], $request->toArray());
}
}
18 changes: 18 additions & 0 deletions tests/Unit/Search/SearchResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ public function test_suggestions_can_be_retrieved(): void
], $searchResponse->getSuggestions());
}

public function test_aggregations_can_be_retrieved(): void
{
$searchResponse = new SearchResponse([
'hits' => [],
'aggregations' => [
'min_price' => [
'value' => 10
]
]
]);

$this->assertEquals([
'min_price' => [
'value' => 10
]
], $searchResponse->getAggregations());
}

public function test_raw_representation_can_be_retrieved(): void
{
$searchResponse = new SearchResponse([
Expand Down

0 comments on commit c6ff71d

Please sign in to comment.