refactored jobs, cleaned up
This commit is contained in:
parent
254c58101b
commit
47fc5c2f1b
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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' => [
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Blomstra\Search\Jobs;
|
||||
|
||||
use Elasticsearch\Client;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class DeletingJob extends Job
|
||||
{
|
||||
public function handle(Container $container)
|
||||
{
|
||||
if ($this->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
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Blomstra\Search\Observe;
|
||||
namespace Blomstra\Search\Jobs;
|
||||
|
||||
use Blomstra\Search\Seeders\Seeder;
|
||||
use Flarum\Queue\AbstractJob;
|
||||
|
|
@ -10,8 +10,12 @@ abstract class Job extends AbstractJob
|
|||
{
|
||||
protected string $index;
|
||||
|
||||
public static ?string $onQueue = null;
|
||||
|
||||
public function __construct(protected Collection $models, protected Seeder $seeder)
|
||||
{
|
||||
$this->index = resolve('blomstra.search.elastic_index');
|
||||
|
||||
if (static::$onQueue) $this->onQueue(static::$onQueue);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Blomstra\Search\Observe;
|
||||
namespace Blomstra\Search\Jobs;
|
||||
|
||||
use Elasticsearch\Client;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Blomstra\Search;
|
||||
|
||||
use Blomstra\Search\Documents\Document;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class Manager
|
||||
{
|
||||
public function __construct(protected Container $container)
|
||||
{}
|
||||
|
||||
public function document(string $type): ?string
|
||||
{
|
||||
return $this->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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Blomstra\Search\Observe;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use MeiliSearch\Client;
|
||||
|
||||
class DeletingJob extends Job
|
||||
{
|
||||
public function handle(Client $meili)
|
||||
{
|
||||
if ($this->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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue