make('blomstra.search.elastic_index'); /** @var array $seeders */ $seeders = $container->tagged('blomstra.search.seeders'); /** @var Queue $queue */ $queue = $container->make(Queue::class); /** @var Client $client */ $client = $container->make(Client::class); /** @var SettingsRepositoryInterface $settings */ $settings = $container->make(SettingsRepositoryInterface::class); $properties = [ 'properties' => [ 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer'], 'created_at' => ['type' => 'date'], 'updated_at' => ['type' => 'date'], 'is_private' => ['type' => 'boolean'], 'is_sticky' => ['type' => 'boolean'], 'groups' => ['type' => 'integer'], 'recipient_groups' => ['type' => 'integer'], 'recipient_users' => ['type' => 'integer'], ] ]; if ($this->option('recreate')) { // Flush the index. $client->indices()->delete([ 'index' => $index, 'ignore_unavailable' => true ]); // Create a new index. $client->indices()->create([ 'index' => $index, 'body' => [ 'settings' => [ 'analysis' => [ 'analyzer' => [ 'flarum_analyzer' => [ 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english' ] ] ] ] ] ]); $client->indices()->putMapping([ 'index' => $index, 'body' => $properties ]); } $only = $this->option('only'); /** @var Seeder $seeder */ foreach ($seeders as $seeder) { if ($only && $seeder->type() !== $only) continue; $total = 0; $continueAt = $this->option('continue') ? ($this->continueAt($seeder->type()) ?? $seeder->query()->max('id')) : $seeder->query()->max('id'); while($continueAt !== null) { /** @var Collection $collection */ $collection = $seeder->query() ->latest('id') ->whereBetween('id', [$continueAt - 1000, $continueAt]) ->when($this->option('max-id'), function ($query, $id) { $query->where('id', '<=', $id); }) ->get(); $min = $collection->min('id'); $continueAt = $min && $min > 2 ? $min - 1 : null; $queue->pushOn(Job::$onQueue, new SavingJob($collection, $seeder)); $this->info("Pushed into the index, type: {$seeder->type()}, amount: {$collection->count()}."); $total += $collection->count(); $this->continueAt( $seeder->type(), $continueAt ); if ($throttle = $this->option('throttle')) { $this->info("Throttling for $throttle seconds"); sleep($throttle); } } $this->info("Pushed a total of $total into the index."); } } protected function continueAt(string $type, int $at = null) { /** @var SettingsRepositoryInterface $settings */ $settings = resolve(SettingsRepositoryInterface::class); $key = "blomstra-search.continued-at.$type"; if ($at) { $settings->set($key, $at); } else { return $settings->get($key); } } }