Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Added progress bar to import, laravel scout version... #4

Merged
merged 10 commits into from
Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@
}
},
"require": {
"laravel/scout": "^3.0|^4.0|^5.0",
"laravel/scout": "^3.0|^4.0|^5.0|^6.0|^7.0",
"ethanhann/redisearch-php": "^1.0.0",
"ethanhann/redis-raw": "^0.3.1"
},
"require-dev": {
"phpunit/phpunit": "^7.1",
"mockery/mockery": "^1.0"
},
"extra": {
"laravel": {
"providers": [
"Ehann\\LaravelRediSearch\\RediSearchServiceProvider"
]
}
}
}
42 changes: 23 additions & 19 deletions src/Scout/Console/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ImportCommand extends Command
{
protected $signature = 'ehann:redisearch:import
{model : The model class to import.}
{--chunk-size=1000 : Import chunk size.}
{--recreate-index : Drop the index before importing.}
{--no-id : Do not select by "id" primary key.}
{--no-import-models : Create index but dont import model.}
Expand All @@ -23,6 +24,7 @@ class ImportCommand extends Command
public function handle(RedisRawClientInterface $redisClient)
{
$class = $this->argument('model');
$chunk_size = $this->option('chunk-size') ?? 1000;
$model = new $class();
$index = new Index($redisClient, $model->searchableAs());

Expand All @@ -36,9 +38,6 @@ public function handle(RedisRawClientInterface $redisClient)
$query = '*';
}

$records = $class::select(DB::raw($query))
->get();

// Define Schema
foreach ($model->searchableSchema() as $name => $value) {

Expand All @@ -62,10 +61,6 @@ public function handle(RedisRawClientInterface $redisClient)
}
}

if ($records->isEmpty()) {
$this->warn('There are no models to import.');
}

if ($this->option('recreate-index')) {
$index->drop();
}
Expand All @@ -75,22 +70,31 @@ public function handle(RedisRawClientInterface $redisClient)
}

if (!$this->option('no-import-models')) {
$records_total = $class::count();
if (!$records_total) {
$this->warn('There are no models to import.');
}
$bar = $this->output->createProgressBar($records_total);
$records = $class::select(DB::raw($query));
$records
->each(function ($item) use ($index, $model) {
$document = $index->makeDocument(
$item->getKey()
);
foreach ($item->toSearchableArray() as $name => $value) {
if ($name !== $model->getKeyName()) {
$value = $value ?? '';
$document->$name->setValue($value);
->chunk($chunk_size, function ($models) use ($index, $model, $bar) {
foreach($models as $item) {
$document = $index->makeDocument(
$item->getKey()
);
foreach ($item->toSearchableArray() as $name => $value) {
if ($name !== $model->getKeyName()) {
$value = $value ?? '';
$document->$name->setValue($value);
}
}
$index->add($document);
$bar->advance();
}

$index->add($document);

});
$this->info("[$class] models imported created successfully");
$bar->finish();

$this->info(PHP_EOL."[$class] models imported created successfully");
} else {
$this->info("$class index created successfully");
}
Expand Down
8 changes: 7 additions & 1 deletion src/Scout/Engines/RediSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ public function map(Builder $builder, $results, $model)
*/
public function getTotalCount($results)
{
return $results->first();
return $results->getCount();
}

public function flush($model)
{
$index = new Index($this->redisRawClient, (new $model())->searchableAs());
$index->drop();
}
}