Skip to content

Commit

Permalink
Merge pull request #324 from limenet/patch-1
Browse files Browse the repository at this point in the history
Compatibility with Laravel Scout v9
  • Loading branch information
nticaric authored Jun 4, 2021
2 parents 09b9c92 + 6d01efe commit ea96227
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion src/Engines/TNTSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace TeamTNT\Scout\Engines;

use Exception;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
use InvalidArgumentException;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use TeamTNT\Scout\Events\SearchPerformed;
Expand Down Expand Up @@ -222,6 +225,45 @@ public function map(Builder $builder, $results, $model)
})->filter()->values();
}

/**
* Map the given results to instances of the given model via a lazy collection.
*
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return LazyCollection
*/
public function lazyMap(Builder $builder, $results, $model)
{
if (empty($results['ids'])) {
return LazyCollection::make();
}

$keys = collect($results['ids'])->values()->all();

$builder = $this->getBuilder($model);

if ($this->builder->queryCallback) {
call_user_func($this->builder->queryCallback, $builder);
}

$models = $builder->whereIn(
$model->getQualifiedKeyName(), $keys
)->get()->keyBy($model->getKeyName());

// sort models by user choice
if (!empty($this->builder->orders)) {
return $models->values();
}

// sort models by tnt search result set
return $model->newCollection($results['ids'])->map(function ($hit) use ($models) {
if (isset($models[$hit])) {
return $models[$hit];
}
})->filter()->values();
}

/**
* Return query builder either from given constraints, or as
* new query. Add where statements to builder when given.
Expand Down Expand Up @@ -255,7 +297,7 @@ public function mapIds($results)
if (empty($results['ids'])) {
return collect();
}

return collect($results['ids'])->values();
}

Expand Down Expand Up @@ -423,6 +465,32 @@ public function flush($model)
}
}


/**
* Create a search index.
*
* @param string $name
* @param array $options
* @return mixed
*
* @throws \Exception
*/
public function createIndex($name, array $options = [])
{
throw new Exception('TNT indexes are created automatically upon adding objects.');
}

/**
* Delete a search index.
*
* @param string $name
* @return mixed
*/
public function deleteIndex($name)
{
throw new Exception(sprintf('TNT indexes cannot reliably be removed. Please manually remove the file in %s/%s.index', config('scout.tntsearch.storage'), $name));
}

/**
* Adds a filter
*
Expand Down

0 comments on commit ea96227

Please sign in to comment.