Skip to content

Commit

Permalink
Added support for suggestions. Fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
Radiergummi committed Mar 29, 2021
1 parent 1b5fcfc commit 6c23f74
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class Collection extends BaseCollection
*/
protected $shards;

/**
* @var array|null
*/
protected $suggestions;

/**
* Collection constructor.
*
Expand All @@ -60,6 +65,7 @@ class Collection extends BaseCollection
* @param bool|null $timedOut
* @param string|null $scrollId
* @param stdClass|null $shards
* @param array|null $suggestions
*/
public function __construct(
array $items = [],
Expand All @@ -68,7 +74,8 @@ public function __construct(
?float $duration = null,
?bool $timedOut = null,
?string $scrollId = null,
?stdClass $shards = null
?stdClass $shards = null,
?array $suggestions = null
) {
parent::__construct($items);

Expand All @@ -79,6 +86,7 @@ public function __construct(
$this->timedOut = $timedOut;
$this->scrollId = $scrollId;
$this->shards = $shards;
$this->suggestions = $suggestions;
}

public static function fromResponse(
Expand All @@ -92,6 +100,7 @@ public static function fromResponse(
$timedOut = (bool)$response['timed_out'];
$scrollId = (string)($response['_scroll_id'] ?? null);
$shards = (object)$response['_shards'];
$suggestions = $response['suggest'] ?? [];
$total = (int)(is_array($response['hits']['total'])
? $response['hits']['total']['value']
: $response['hits']['total']
Expand All @@ -104,7 +113,8 @@ public static function fromResponse(
$duration,
$timedOut,
$scrollId,
$shards
$shards,
$suggestions
);
}

Expand Down Expand Up @@ -138,6 +148,18 @@ public function getShards(): ?stdClass
return $this->shards;
}

public function getAllSuggestions(): BaseCollection
{
return BaseCollection
::make($this->suggestions)
->mapInto(BaseCollection::class);
}

public function getSuggestions(string $name): BaseCollection
{
return new BaseCollection($this->suggestions[$name] ?? []);
}

/**
* Get the collection of items as Array.
*
Expand Down

0 comments on commit 6c23f74

Please sign in to comment.