From 6c23f74e4141031a3de584d1cdfcba463972e180 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Mon, 29 Mar 2021 16:07:57 +0200 Subject: [PATCH] Added support for suggestions. Fixes #26 --- src/Collection.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 575bab1..77d8097 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -50,6 +50,11 @@ class Collection extends BaseCollection */ protected $shards; + /** + * @var array|null + */ + protected $suggestions; + /** * Collection constructor. * @@ -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 = [], @@ -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); @@ -79,6 +86,7 @@ public function __construct( $this->timedOut = $timedOut; $this->scrollId = $scrollId; $this->shards = $shards; + $this->suggestions = $suggestions; } public static function fromResponse( @@ -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'] @@ -104,7 +113,8 @@ public static function fromResponse( $duration, $timedOut, $scrollId, - $shards + $shards, + $suggestions ); } @@ -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. *