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

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanhann committed Jun 2, 2019
1 parent 90de0e2 commit 5b50877
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 105 deletions.
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
>
<testsuites>
Expand Down
98 changes: 48 additions & 50 deletions src/Scout/Console/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Ehann\LaravelRediSearch\Scout\Console;

use DB;
use Ehann\RediSearch\Fields\FieldFactory;
use Ehann\RediSearch\Index;
use Ehann\RedisRaw\RedisRawClientInterface;
use Illuminate\Console\Command;
use Ehann\RediSearch\Fields\TextField;
use Ehann\RediSearch\Fields\NumericField;
use Ehann\RediSearch\Fields\GeoField;
use Ehann\RediSearch\Fields\TagField;
Expand Down Expand Up @@ -36,33 +34,33 @@ public function handle(RedisRawClientInterface $redisClient)

if ($this->option('no-id') || $query === '') {
$query = '*';
}
}

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

// Define Schema
foreach ($model->searchableSchema() as $name => $value) {
if ($name !== $model->getKeyName()) {
$value = $value ?? '';

if ($value === NumericField::class) {
$index->addNumericField($name);
continue;
}
if ($value === GeoField::class) {
$index->addGeoField($name);
continue;
}
if ($value === TagField::class) {
$index->addTagField($name);
continue;
}

$index->addTextField($name);
}
}
->get();

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

if ($name !== $model->getKeyName()) {
$value = $value ?? '';

if ($value === NumericField::class) {
$index->addNumericField($name);
continue;
}
if ($value === GeoField::class) {
$index->addGeoField($name);
continue;
}
if ($value === TagField::class) {
$index->addTagField($name);
continue;
}

$index->addTextField($name);
}
}

if ($records->isEmpty()) {
$this->warn('There are no models to import.');
Expand All @@ -74,27 +72,27 @@ public function handle(RedisRawClientInterface $redisClient)

if (!$index->create()) {
$this->warn('The index already exists. Use --recreate-index to recreate the index before importing.');
}
if (!$this->option('no-import-models')) {
$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);
}
}
$index->add($document);
});
$this->info("[$class] models imported created successfully");
}else{
$this->info("$class index created successfully");
}
}

if (!$this->option('no-import-models')) {
$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);
}
}

$index->add($document);

});
$this->info("[$class] models imported created successfully");
} else {
$this->info("$class index created successfully");
}
}
}
99 changes: 46 additions & 53 deletions src/Scout/Engines/RediSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Illuminate\Database\Eloquent\Collection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use Ehann\RediSearch\Fields\FieldFactory;
use Ehann\RediSearch\Fields\TextField;
use Ehann\RediSearch\Fields\NumericField;
use Ehann\RediSearch\Fields\GeoField;
use Ehann\RediSearch\Fields\TagField;
Expand Down Expand Up @@ -37,54 +35,49 @@ public function __construct(RedisRawClientInterface $redisRawClient)
*/
public function update($models)
{
$model = $models->first();
$model = $models->first();
$index = new Index($this->redisRawClient, $model->first()->searchableAs());

//$models->each(function ($item) use ($index, $model) {
foreach ($model->searchableSchema() as $name => $value) {

if ($name !== $model->getKeyName()) {
$value = $value ?? '';

if ($value === NumericField::class) {
$index->addNumericField($name);
continue;
}
if ($value === GeoField::class) {
$index->addGeoField($name);
continue;
}
if ($value === TagField::class) {
$index->addTagField($name);
continue;
}

$index->addTextField($name);
}
foreach ($model->searchableSchema() as $name => $value) {

if ($name !== $model->getKeyName()) {
$value = $value ?? '';

if ($value === NumericField::class) {
$index->addNumericField($name);
continue;
}
if ($value === GeoField::class) {
$index->addGeoField($name);
continue;
}
if ($value === TagField::class) {
$index->addTagField($name);
continue;
}

$index->addTextField($name);
}
//});

$models
->each(function ($item) use ($index, $model) {
$document = $index->makeDocument(
$item->getKey()
// property_exists($item, $model->getKeyName()) ? $item->{$model->getKeyName()} : null
);
foreach ($item->toSearchableArray() as $name => $value) {
if ($name !== $model->getKeyName()) {
$value = $value ?? '';
$document->$name->setValue($value); //= FieldFactory::make($name, $value);
}
}
try {
$index->add($document);
} catch (\Throwable $th) {
if ($th->getMessage() == "Document already exists") {
$index->replace($document);
}
}

});
}

$models
->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);
}
}
try {
$index->add($document);
} catch (\Throwable $th) {
if ($th->getMessage() == "Document already exists") {
$index->replace($document);
}
}

});
}

/**
Expand Down Expand Up @@ -114,13 +107,13 @@ public function delete($models)
*/
public function search(Builder $builder)
{
$index = (new Index($this->redisRawClient, $builder->index ?? $builder->model->searchableAs()));
$index = (new Index($this->redisRawClient, $builder->index ?? $builder->model->searchableAs()));

if ($builder->callback) {
$advanced_search = (call_user_func($builder->callback, $index));
if ($builder->callback) {
$advanced_search = (call_user_func($builder->callback, $index));

return $advanced_search->search($builder->query);
}
return $advanced_search->search($builder->query);
}

return $index
->search($builder->query);
Expand Down Expand Up @@ -155,7 +148,7 @@ public function mapIds($results)

public function map(Builder $builder, $results, $model)
{
$results = collect($results);
$results = collect($results);

$count = $results->first();
if ($count === 0) {
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require __DIR__.'/../vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';

use Carbon\Carbon;

Expand Down

0 comments on commit 5b50877

Please sign in to comment.