Skip to content

Commit

Permalink
laravel scout tntsearch support
Browse files Browse the repository at this point in the history
  • Loading branch information
nticaric committed Aug 15, 2016
1 parent 9af42f5 commit b70e58e
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected] "
}
],
"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
}
133 changes: 133 additions & 0 deletions src/Engines/TNTSearchEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
namespace TeamTNT\Scout\Engines;

use Illuminate\Database\Eloquent\Collection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use TeamTNT\TNTSearch\TNTSearch;

class TNTSearchEngine extends Engine
{
/**
* Create a new engine instance.
*
* @param TeamTNT\TNTSearch\TNTSearch $tnt
* @return void
*/
public function __construct(TNTSearch $tnt)
{
$this->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();
}
}
}
22 changes: 22 additions & 0 deletions src/TNTSearchScoutServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace TeamTNT\Scout;

use Illuminate\Support\ServiceProvider;
use Laravel\Scout\EngineManager;
use TeamTNT\TNTSearch\TNTSearch;

class TNTSearchScoutServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app[EngineManager::class]->extend('tntsearch', function () {
return new Engines\TNTSearchEngine(new TNTSearch);
});
}
}

0 comments on commit b70e58e

Please sign in to comment.