Skip to content

Commit ea96227

Browse files
authored
Merge pull request #324 from limenet/patch-1
Compatibility with Laravel Scout v9
2 parents 09b9c92 + 6d01efe commit ea96227

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

src/Engines/TNTSearchEngine.php

+69-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace TeamTNT\Scout\Engines;
44

5+
use Exception;
56
use Illuminate\Database\Eloquent\Collection;
67
use Illuminate\Database\Eloquent\SoftDeletes;
78
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\LazyCollection;
10+
use InvalidArgumentException;
811
use Laravel\Scout\Builder;
912
use Laravel\Scout\Engines\Engine;
1013
use TeamTNT\Scout\Events\SearchPerformed;
@@ -222,6 +225,45 @@ public function map(Builder $builder, $results, $model)
222225
})->filter()->values();
223226
}
224227

228+
/**
229+
* Map the given results to instances of the given model via a lazy collection.
230+
*
231+
* @param mixed $results
232+
* @param \Illuminate\Database\Eloquent\Model $model
233+
*
234+
* @return LazyCollection
235+
*/
236+
public function lazyMap(Builder $builder, $results, $model)
237+
{
238+
if (empty($results['ids'])) {
239+
return LazyCollection::make();
240+
}
241+
242+
$keys = collect($results['ids'])->values()->all();
243+
244+
$builder = $this->getBuilder($model);
245+
246+
if ($this->builder->queryCallback) {
247+
call_user_func($this->builder->queryCallback, $builder);
248+
}
249+
250+
$models = $builder->whereIn(
251+
$model->getQualifiedKeyName(), $keys
252+
)->get()->keyBy($model->getKeyName());
253+
254+
// sort models by user choice
255+
if (!empty($this->builder->orders)) {
256+
return $models->values();
257+
}
258+
259+
// sort models by tnt search result set
260+
return $model->newCollection($results['ids'])->map(function ($hit) use ($models) {
261+
if (isset($models[$hit])) {
262+
return $models[$hit];
263+
}
264+
})->filter()->values();
265+
}
266+
225267
/**
226268
* Return query builder either from given constraints, or as
227269
* new query. Add where statements to builder when given.
@@ -255,7 +297,7 @@ public function mapIds($results)
255297
if (empty($results['ids'])) {
256298
return collect();
257299
}
258-
300+
259301
return collect($results['ids'])->values();
260302
}
261303

@@ -423,6 +465,32 @@ public function flush($model)
423465
}
424466
}
425467

468+
469+
/**
470+
* Create a search index.
471+
*
472+
* @param string $name
473+
* @param array $options
474+
* @return mixed
475+
*
476+
* @throws \Exception
477+
*/
478+
public function createIndex($name, array $options = [])
479+
{
480+
throw new Exception('TNT indexes are created automatically upon adding objects.');
481+
}
482+
483+
/**
484+
* Delete a search index.
485+
*
486+
* @param string $name
487+
* @return mixed
488+
*/
489+
public function deleteIndex($name)
490+
{
491+
throw new Exception(sprintf('TNT indexes cannot reliably be removed. Please manually remove the file in %s/%s.index', config('scout.tntsearch.storage'), $name));
492+
}
493+
426494
/**
427495
* Adds a filter
428496
*

0 commit comments

Comments
 (0)