|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Statamic\Search; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 6 | +use Illuminate\Foundation\Queue\Queueable; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | +use Illuminate\Support\LazyCollection; |
| 9 | +use Statamic\Contracts\Search\Searchable; |
| 10 | +use Statamic\Facades\Search; |
| 11 | +use Statamic\Search\Searchables\Providers; |
| 12 | +use Statamic\Support\Str; |
| 13 | + |
| 14 | +class InsertMultipleJob implements ShouldQueue |
| 15 | +{ |
| 16 | + use Queueable; |
| 17 | + |
| 18 | + /** |
| 19 | + * Create a new job instance. |
| 20 | + */ |
| 21 | + public function __construct( |
| 22 | + public string $name, |
| 23 | + public ?string $locale, |
| 24 | + public Collection|LazyCollection $documents, |
| 25 | + ) { |
| 26 | + $this->onConnection($connection = config('statamic.search.queue_connection', config('queue.default'))); |
| 27 | + $this->onQueue(config('statamic.search.queue', config("queue.connections.{$connection}.queue"))); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Execute the job. |
| 32 | + */ |
| 33 | + public function handle(): void |
| 34 | + { |
| 35 | + $providers = app(Providers::class); |
| 36 | + $index = Search::index($this->name, $this->locale); |
| 37 | + |
| 38 | + $documents = $this->documents |
| 39 | + ->groupBy(fn ($document) => explode('::', $document)[0]) |
| 40 | + ->flatMap(function ($documents, $prefix) use ($providers) { |
| 41 | + return $providers |
| 42 | + ->getByPrefix($prefix) |
| 43 | + ->find($documents->map(fn ($reference) => Str::after($reference, '::'))->all()) |
| 44 | + ->all(); |
| 45 | + }) |
| 46 | + ->mapWithKeys(function (Searchable $item) use ($index) { |
| 47 | + return [$item->getSearchReference() => $index->fields($item)]; |
| 48 | + }); |
| 49 | + |
| 50 | + $index->insertDocuments(new Documents($documents)); |
| 51 | + } |
| 52 | +} |
0 commit comments