diff --git a/README.md b/README.md index 46e3a68..0793069 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,13 @@ All mutations to discussions are automatically added and removed from the elasti *I have another question.* Reach out to us via https://helpdesk.blomstra.net. We will get back to you as soon as we can. If you have a running subscription please mention when you started your plan and/or which plan you are on. Always add sufficient information when reporting errors. We prefer errors being reported here, but understand that sometimes you can't. +*Can I dispatch the sync jobs to another queue?* +Yes: + +```php +\Blomstra\Search\Observe\Job::$onQueue = 'sync'; +``` + --- - Blomstra provides managed Flarum hosting. diff --git a/src/Commands/RebuildDocumentsCommand.php b/src/Commands/RebuildDocumentsCommand.php index aacc399..d23f350 100644 --- a/src/Commands/RebuildDocumentsCommand.php +++ b/src/Commands/RebuildDocumentsCommand.php @@ -2,7 +2,7 @@ namespace Blomstra\Search\Commands; -use Blomstra\Search\Observe\SavingJob; +use Blomstra\Search\Jobs\SavingJob; use Blomstra\Search\Seeders\Seeder; use Elasticsearch\Client; use Flarum\Settings\SettingsRepositoryInterface; @@ -59,7 +59,6 @@ class RebuildDocumentsCommand extends Command 'index' => $index, 'body' => [ 'settings' => [ - 'analysis' => [ 'analyzer' => [ 'flarum_analyzer' => [ diff --git a/src/Jobs/DeletingJob.php b/src/Jobs/DeletingJob.php new file mode 100644 index 0000000..2703404 --- /dev/null +++ b/src/Jobs/DeletingJob.php @@ -0,0 +1,33 @@ +models->isEmpty()) return; + + /** @var Client $client */ + $client = $container->make('blomstra.search.elastic'); + + // Preparing body for storing. + $body = $this->models->map(function (Model $model) { + $document = $this->seeder->toDocument($model); + + return [ + ['delete' => ['_index' => $this->index, '_id' => $document->id]] + ]; + })->flatten(1); + + $response = $client->bulk([ + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true + ]); + } +} diff --git a/src/Observe/Job.php b/src/Jobs/Job.php similarity index 71% rename from src/Observe/Job.php rename to src/Jobs/Job.php index e05df75..288d10e 100644 --- a/src/Observe/Job.php +++ b/src/Jobs/Job.php @@ -1,6 +1,6 @@ index = resolve('blomstra.search.elastic_index'); + + if (static::$onQueue) $this->onQueue(static::$onQueue); } } diff --git a/src/Observe/SavingJob.php b/src/Jobs/SavingJob.php similarity index 96% rename from src/Observe/SavingJob.php rename to src/Jobs/SavingJob.php index b6febed..d7731cf 100644 --- a/src/Observe/SavingJob.php +++ b/src/Jobs/SavingJob.php @@ -1,6 +1,6 @@ fromTagged('blomstra.search.documents', $type); - } - - protected function fromTagged(string $binding, $search): ?string - { - $entities = $this->container->tagged($binding); - - foreach ($entities as $entity) { - $instance = $this->container->make($entity); - - if ($instance->type() === $search) return $entity; - } - - return null; - } -} diff --git a/src/Observe/DeletingJob.php b/src/Observe/DeletingJob.php deleted file mode 100644 index 9a341b2..0000000 --- a/src/Observe/DeletingJob.php +++ /dev/null @@ -1,28 +0,0 @@ -models->isEmpty()) return; - - $document = $this->getDocument(); - - if (! $document) return; - - $keys = $this->models->map(function (Model $model) use ($document) { - return (new $document)($model)->id(); - })->toArray(); - - $meili - ->index(resolve('blomstra.search.elastic_index')) - ->deleteDocuments( - $keys - ); - } -} diff --git a/src/Provider.php b/src/Provider.php index b908c10..5f8a7c0 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -2,8 +2,8 @@ namespace Blomstra\Search; -use Blomstra\Search\Observe\DeletingJob; -use Blomstra\Search\Observe\SavingJob; +use Blomstra\Search\Jobs\DeletingJob; +use Blomstra\Search\Jobs\SavingJob; use Blomstra\Search\Seeders; use Elasticsearch\ClientBuilder; use Flarum\Foundation\AbstractServiceProvider;