From fce28dfc745d14b6c5fde72dbafabcd4064f96e4 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 27 Nov 2023 14:56:08 +0000 Subject: [PATCH 1/4] Provide searchables()->lazy and insertLazily methods to search indexes --- src/Search/Algolia/Index.php | 2 +- src/Search/Index.php | 12 +++++++++++- src/Search/Searchables.php | 10 ++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Search/Algolia/Index.php b/src/Search/Algolia/Index.php index 9e18054b54..4de0c64d51 100644 --- a/src/Search/Algolia/Index.php +++ b/src/Search/Algolia/Index.php @@ -61,7 +61,7 @@ public function update() $index->setSettings($this->config['settings']); } - $this->insertMultiple($this->searchables()->all()); + $this->insertLazily($this->searchables()->lazy()); return $this; } diff --git a/src/Search/Index.php b/src/Search/Index.php index b0d8317d8e..dad7115fc6 100644 --- a/src/Search/Index.php +++ b/src/Search/Index.php @@ -2,6 +2,7 @@ namespace Statamic\Search; +use Illuminate\Support\LazyCollection; use Statamic\Contracts\Search\Searchable; use Statamic\Support\Arr; @@ -57,7 +58,7 @@ public function update() { $this->deleteIndex(); - $this->insertMultiple($this->searchables()->all()); + $this->insertLazily($this->searchables()->lazy()); return $this; } @@ -87,6 +88,15 @@ public function insertMultiple($documents) return $this; } + public function insertLazily(LazyCollection $providers) + { + foreach ($providers as $documents) { + $this->insertMultiple($documents); + } + + return $this; + } + public function shouldIndex($searchable) { return $this->searchables()->contains($searchable); diff --git a/src/Search/Searchables.php b/src/Search/Searchables.php index d0562b0223..f569ce3043 100644 --- a/src/Search/Searchables.php +++ b/src/Search/Searchables.php @@ -4,6 +4,7 @@ use Closure; use Illuminate\Contracts\Container\BindingResolutionException; +use Illuminate\Support\LazyCollection; use Illuminate\Support\Collection; use Statamic\Contracts\Search\Searchable; use Statamic\Search\Searchables\Providers; @@ -43,6 +44,15 @@ public function all(): Collection return $this->providers->flatMap->provide(); } + public function lazy(): LazyCollection + { + return LazyCollection::make(function () { + foreach ($this->providers as $provider) { + yield $provider->provide(); + } + }); + } + public function contains($searchable) { foreach ($this->providers as $provider) { From 8a907785965fbd2508af9e1493230e49a2abc904 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 27 Nov 2023 14:56:43 +0000 Subject: [PATCH 2/4] :beer: --- src/Search/Searchables.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Search/Searchables.php b/src/Search/Searchables.php index f569ce3043..8bdb1d6e0c 100644 --- a/src/Search/Searchables.php +++ b/src/Search/Searchables.php @@ -4,8 +4,8 @@ use Closure; use Illuminate\Contracts\Container\BindingResolutionException; -use Illuminate\Support\LazyCollection; use Illuminate\Support\Collection; +use Illuminate\Support\LazyCollection; use Statamic\Contracts\Search\Searchable; use Statamic\Search\Searchables\Providers; use Statamic\Support\Arr; From 279cef0071e53b7e633c3f6aed1cd2953939f69b Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 27 Nov 2023 17:31:58 +0000 Subject: [PATCH 3/4] For Erin --- src/Search/Index.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Search/Index.php b/src/Search/Index.php index dad7115fc6..2c63dba3d6 100644 --- a/src/Search/Index.php +++ b/src/Search/Index.php @@ -90,9 +90,7 @@ public function insertMultiple($documents) public function insertLazily(LazyCollection $providers) { - foreach ($providers as $documents) { - $this->insertMultiple($documents); - } + $providers->each(fn ($documents) => $this->insertMultiple($documents)); return $this; } From 00aa0598cdd14112383261b38b07e2c13bc6a759 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 28 Nov 2023 10:26:56 -0500 Subject: [PATCH 4/4] extra method didnt feel necessary --- src/Search/Algolia/Index.php | 2 +- src/Search/Index.php | 10 +--------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/Search/Algolia/Index.php b/src/Search/Algolia/Index.php index 4de0c64d51..0efa46589d 100644 --- a/src/Search/Algolia/Index.php +++ b/src/Search/Algolia/Index.php @@ -61,7 +61,7 @@ public function update() $index->setSettings($this->config['settings']); } - $this->insertLazily($this->searchables()->lazy()); + $this->searchables()->lazy()->each(fn ($searchables) => $this->insertMultiple($searchables)); return $this; } diff --git a/src/Search/Index.php b/src/Search/Index.php index 2c63dba3d6..a57bab55c3 100644 --- a/src/Search/Index.php +++ b/src/Search/Index.php @@ -2,7 +2,6 @@ namespace Statamic\Search; -use Illuminate\Support\LazyCollection; use Statamic\Contracts\Search\Searchable; use Statamic\Support\Arr; @@ -58,7 +57,7 @@ public function update() { $this->deleteIndex(); - $this->insertLazily($this->searchables()->lazy()); + $this->searchables()->lazy()->each(fn ($searchables) => $this->insertMultiple($searchables)); return $this; } @@ -88,13 +87,6 @@ public function insertMultiple($documents) return $this; } - public function insertLazily(LazyCollection $providers) - { - $providers->each(fn ($documents) => $this->insertMultiple($documents)); - - return $this; - } - public function shouldIndex($searchable) { return $this->searchables()->contains($searchable);