Skip to content

Commit bfd91ef

Browse files
[performance] Optimize hyperf/scout, add --chunk and --column|c options to the scout:import command (#2883)
* perf: Optimize scout, use chunkById() instead of chunk() * feat: Add `--chunk` and `--column|c` options to the scout:import command * Update phpunit.xml * Update CHANGELOG-2.0.md Co-authored-by: 李铭昕 <[email protected]>
1 parent f6e0b7d commit bfd91ef

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/Console/ImportCommand.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Hyperf\Utils\ApplicationContext;
1717
use Psr\EventDispatcher\ListenerProviderInterface;
1818
use Symfony\Component\Console\Input\InputArgument;
19+
use Symfony\Component\Console\Input\InputOption;
1920

2021
class ImportCommand extends Command
2122
{
@@ -40,16 +41,27 @@ public function handle()
4041
{
4142
define('SCOUT_COMMAND', true);
4243
$class = $this->input->getArgument('model');
44+
$chunk = (int) $this->input->getOption('chunk');
45+
$column = (string) $this->input->getOption('column');
4346
$model = new $class();
4447
$provider = ApplicationContext::getContainer()->get(ListenerProviderInterface::class);
4548
$provider->on(ModelsImported::class, function ($event) use ($class) {
49+
/** @var ModelsImported $event */
4650
$key = $event->models->last()->getScoutKey();
4751
$this->line('<comment>Imported [' . $class . '] models up to ID:</comment> ' . $key);
4852
});
49-
$model::makeAllSearchable();
53+
$model::makeAllSearchable($chunk ?: null, $column ?: null);
5054
$this->info('All [' . $class . '] records have been imported.');
5155
}
5256

57+
protected function getOptions()
58+
{
59+
return [
60+
['column', 'c', InputOption::VALUE_OPTIONAL, 'Column used in chunking. (Default use primary key)'],
61+
['chunk', '', InputOption::VALUE_OPTIONAL, 'The number of records to import at a time (Defaults to configuration value: `scout.chunk.searchable`)'],
62+
];
63+
}
64+
5365
protected function getArguments()
5466
{
5567
return [

src/Searchable.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,16 @@ public static function search(?string $query = '', ?\Closure $callback = null)
111111
/**
112112
* Make all instances of the model searchable.
113113
*/
114-
public static function makeAllSearchable()
114+
public static function makeAllSearchable(?int $chunk = null, ?string $column = null)
115115
{
116116
$self = new static();
117117
$softDelete = static::usesSoftDelete() && config('scout.soft_delete', false);
118118
$self->newQuery()
119119
->when($softDelete, function ($query) {
120120
$query->withTrashed();
121121
})
122-
->orderBy($self->getKeyName())
123-
->searchable();
122+
->orderBy($column ?: $self->getKeyName())
123+
->searchable($chunk, $column);
124124
}
125125

126126
/**

src/SearchableScope.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ public function apply(EloquentBuilder $builder, Model $model)
3333
*/
3434
public function extend(EloquentBuilder $builder)
3535
{
36-
$builder->macro('searchable', function (EloquentBuilder $builder, $chunk = null) {
37-
$builder->chunk($chunk ?: config('scout.chunk.searchable', 500), function ($models) {
36+
$builder->macro('searchable', function (EloquentBuilder $builder, $chunk = null, $column = null) {
37+
$callback = function ($models) {
3838
$models->filter->shouldBeSearchable()->searchable();
3939
$dispatcher = ApplicationContext::getContainer()->get(EventDispatcherInterface::class);
4040
$dispatcher->dispatch(new ModelsImported($models));
41-
});
41+
};
42+
43+
$chunk = $chunk ?: config('scout.chunk.searchable', 500);
44+
45+
$column ? $builder->chunkById($chunk, $callback, $column) : $builder->chunk($chunk, $callback);
4246
});
4347
$builder->macro('unsearchable', function (EloquentBuilder $builder, $chunk = null) {
4448
$builder->chunk($chunk ?: config('scout.chunk.unsearchable', 500), function ($models) {

0 commit comments

Comments
 (0)