diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..581cb40 --- /dev/null +++ b/composer.json @@ -0,0 +1,38 @@ +{ + "name": "teamtnt/laravel-scout-tntsearch-driver", + "description": "Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch", + "keywords": ["tntsearch", "search", "scout", "laravel"], + "license": "MIT", + "authors": [ + { + "name": "TNT Studio", + "email": "info@tntstudio.hr " + } + ], + "require": { + "teamtnt/tntsearch": "0.8.*", + "laravel/scout": "*", + "illuminate/contracts": "~5.3", + "illuminate/database": "~5.3", + "illuminate/support": "~5.3" + }, + "require-dev": { + "mockery/mockery": "~0.9", + "phpunit/phpunit": "~5.0" + }, + "autoload": { + "psr-4": { + "TeamTNT\\Scout\\": "src/" + } + }, + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "suggest": { + "teamtnt/tntsearch": "Required to use the TNTSearch engine." + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/src/Engines/TNTSearchEngine.php b/src/Engines/TNTSearchEngine.php new file mode 100644 index 0000000..e600224 --- /dev/null +++ b/src/Engines/TNTSearchEngine.php @@ -0,0 +1,133 @@ +tnt = $tnt; + $this->tnt->loadConfig(config('scout.tntsearch')); + } + /** + * Update the given model in the index. + * + * @param Collection $models + * @return void + */ + public function update($models) + { + $this->initIndex($models->first()->searchableAs()); + $models->each(function ($model) { + $this->tnt->selectIndex("{$model->searchableAs()}.index"); + $index = $this->tnt->getIndex(); + if ($model->id) { + $index->update($model->id, $model->toSearchableArray()); + } else { + $index->insert($model->toSearchableArray()); + } + }); + } + /** + * Remove the given model from the index. + * + * @param Collection $models + * @return void + */ + public function delete($models) + { + $this->initIndex($models->first()->searchableAs()); + $models->each(function ($model) { + $this->tnt->selectIndex("{$model->searchableAs()}.index"); + $index = $this->tnt->getIndex(); + $index->delete($model->id); + }); + } + /** + * Perform the given search on the engine. + * + * @param Builder $builder + * @return mixed + */ + public function search(Builder $builder) + { + return $this->performSearch($builder); + } + /** + * Perform the given search on the engine. + * + * @param Builder $builder + * @param int $perPage + * @param int $page + * @return mixed + */ + public function paginate(Builder $builder, $perPage, $page) + { + return $this->performSearch($builder); + } + /** + * Perform the given search on the engine. + * + * @param Builder $builder + * @param array $options + * @return mixed + */ + protected function performSearch(Builder $builder, array $options = []) + { + $index = $builder->index ?: $builder->model->searchableAs(); + $limit = $builder->limit ?: 10; + $this->tnt->selectIndex("{$index}.index"); + return $this->tnt->search($builder->query, $limit); + } + /** + * Get the filter array for the query. + * + * @param Builder $builder + * @return array + */ + protected function filters(Builder $builder) + { + return collect($builder->wheres)->map(function ($value, $key) { + return $key . '=' . $value; + })->values()->all(); + } + /** + * Map the given results to instances of the given model. + * + * @param mixed $results + * @param \Illuminate\Database\Eloquent\Model $model + * @return Collection + */ + public function map($results, $model) + { + if (count($results['ids']) === 0) { + return Collection::make(); + } + $keys = collect($results['ids']); + $models = $model->whereIn( + $model->getKeyName(), $keys + )->get()->keyBy($model->getKeyName()); + return collect($results['ids'])->map(function ($hit) use ($models) { + return $models[$hit]; + }); + } + public function initIndex($indexName) + { + if (!file_exists(config('scout.tntsearch.storage') . "/{$indexName}.index")) { + $indexer = $this->tnt->createIndex("$indexName.index"); + $indexer->disableOutput = true; + $indexer->query("SELECT * FROM $indexName"); + $indexer->run(); + } + } +} diff --git a/src/TNTSearchScoutServiceProvider.php b/src/TNTSearchScoutServiceProvider.php new file mode 100644 index 0000000..fb234c6 --- /dev/null +++ b/src/TNTSearchScoutServiceProvider.php @@ -0,0 +1,22 @@ +app[EngineManager::class]->extend('tntsearch', function () { + return new Engines\TNTSearchEngine(new TNTSearch); + }); + } +}