From 017626419d0a1df815c77be867c4ce945dcb14d9 Mon Sep 17 00:00:00 2001 From: Tayeb B Date: Wed, 24 Apr 2024 15:19:07 +0200 Subject: [PATCH] fix: fix dynamic properties deprecation notices, fixes #359 --- src/Console/ImportCommand.php | 10 +++++----- src/Console/StatusCommand.php | 9 ++++----- src/Engines/ExtendedScoutBuilder.php | 10 ++++++++++ src/Engines/TNTSearchEngine.php | 15 +++++++++------ src/Events/SearchPerformed.php | 9 +++++++++ src/ExtendedTNTSearch.php | 12 ++++++++++++ src/TNTSearchScoutServiceProvider.php | 8 ++++++-- tests/TNTSearchEngineTest.php | 7 +++---- 8 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 src/Engines/ExtendedScoutBuilder.php create mode 100644 src/ExtendedTNTSearch.php diff --git a/src/Console/ImportCommand.php b/src/Console/ImportCommand.php index 5d6d944..c6502f6 100644 --- a/src/Console/ImportCommand.php +++ b/src/Console/ImportCommand.php @@ -4,7 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Contracts\Events\Dispatcher; -use TeamTNT\TNTSearch\TNTSearch; +use TeamTNT\Scout\ExtendedTNTSearch as TNTSearch; use Illuminate\Support\Facades\Schema; class ImportCommand extends Command @@ -43,12 +43,12 @@ public function handle(Dispatcher $events) $tnt->loadConfig($config); $tnt->setDatabaseHandle($db->getPdo()); - if(!$model->count()) { + if (!$model->count()) { $this->info('Nothing to import.'); exit(0); } - - $indexer = $tnt->createIndex($model->searchableAs().'.index'); + + $indexer = $tnt->createIndex($model->searchableAs() . '.index'); $indexer->setPrimaryKey($model->getKeyName()); $availableColumns = Schema::connection($driver)->getColumnListing($model->getTable()); @@ -74,6 +74,6 @@ public function handle(Dispatcher $events) $indexer->query($sql); $indexer->run(); - $this->info('All ['.$class.'] records have been imported.'); + $this->info('All [' . $class . '] records have been imported.'); } } diff --git a/src/Console/StatusCommand.php b/src/Console/StatusCommand.php index 4b40e8e..8134dc3 100644 --- a/src/Console/StatusCommand.php +++ b/src/Console/StatusCommand.php @@ -6,7 +6,7 @@ use Illuminate\Contracts\Events\Dispatcher; use Symfony\Component\Finder\Finder; use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException; -use TeamTNT\TNTSearch\TNTSearch; +use TeamTNT\Scout\ExtendedTNTSearch as TNTSearch; class StatusCommand extends Command { @@ -42,7 +42,7 @@ public function handle(Dispatcher $events) $searchableModels = $this->getSearchableModels(); - $this->output->text('🔎 Analysing information from: ['.implode(',', $searchableModels).']'); + $this->output->text('🔎 Analysing information from: [' . implode(',', $searchableModels) . ']'); $this->output->newLine(); $this->output->progressStart(count($searchableModels)); @@ -52,7 +52,7 @@ public function handle(Dispatcher $events) $model = new $class(); $tnt = $this->loadTNTEngine($model); - $indexName = $model->searchableAs().'.index'; + $indexName = $model->searchableAs() . '.index'; try { $tnt->selectIndex($indexName); @@ -66,14 +66,13 @@ public function handle(Dispatcher $events) $indexedColumns = $rowsTotal ? implode(",", array_keys($model->first()->toSearchableArray())) : ''; - if($recordsDifference == 0) { + if ($recordsDifference == 0) { $recordsDifference = 'Synchronized'; } else { $recordsDifference = "$recordsDifference"; } array_push($rows, [$class, $indexName, $indexedColumns, $rowsIndexed, $rowsTotal, $recordsDifference]); - } $this->output->progressFinish(); diff --git a/src/Engines/ExtendedScoutBuilder.php b/src/Engines/ExtendedScoutBuilder.php new file mode 100644 index 0000000..846dba4 --- /dev/null +++ b/src/Engines/ExtendedScoutBuilder.php @@ -0,0 +1,10 @@ +tnt->searchBoolean($builder->query, $limit); event(new SearchPerformed($builder, $res, true)); return $res; - } else { $res = $this->tnt->search($builder->query, $limit); event(new SearchPerformed($builder, $res)); @@ -207,7 +207,8 @@ public function map(Builder $builder, $results, $model) } $models = $builder->whereIn( - $model->getQualifiedKeyName(), $keys + $model->getQualifiedKeyName(), + $keys )->get()->keyBy($model->getKeyName()); // sort models by user choice @@ -250,7 +251,8 @@ public function lazyMap(Builder $builder, $results, $model) } $models = $builder->whereIn( - $model->getQualifiedKeyName(), $keys + $model->getQualifiedKeyName(), + $keys )->get()->keyBy($model->getKeyName()); // sort models by user choice @@ -351,7 +353,8 @@ private function discardIdsFromResultSetByConstraints($builder, $searchResults) $subQualifiedKeyName = 'sub.' . $builder->model->getKeyName(); // sub.id $sub = $this->getBuilder($builder->model)->whereIn( - $qualifiedKeyName, $searchResults + $qualifiedKeyName, + $searchResults ); // sub query for left join $discardIds = $builder->model->newQuery() diff --git a/src/Events/SearchPerformed.php b/src/Events/SearchPerformed.php index 010884d..b3ebdee 100644 --- a/src/Events/SearchPerformed.php +++ b/src/Events/SearchPerformed.php @@ -4,6 +4,15 @@ class SearchPerformed { + public $query; + public $isBooleanSearch; + public $indexName; + public $model; + public $ids; + public $execution_time; + public $driver; + public $hits; + public function __construct($builder, $results, $isBooleanSearch = false) { $this->query = $builder->query; diff --git a/src/ExtendedTNTSearch.php b/src/ExtendedTNTSearch.php new file mode 100644 index 0000000..081702a --- /dev/null +++ b/src/ExtendedTNTSearch.php @@ -0,0 +1,12 @@ +addFilter("query_expansion", function ($query, $model) { - if ($query == "test" && $model == "TeamTNT\TNTSearch\TNTSearch") { - return "modified-".$query; + if ($query == "test" && $model == "TeamTNT\Scout\ExtendedTNTSearch") { + return "modified-" . $query; } return $query; - }); $query = $engine->applyFilters('query_expansion', "test", TNTSearch::class);