|
2 | 2 |
|
3 | 3 | namespace TeamTNT\Scout\Engines;
|
4 | 4 |
|
| 5 | +use Exception; |
5 | 6 | use Illuminate\Database\Eloquent\Collection;
|
6 | 7 | use Illuminate\Database\Eloquent\SoftDeletes;
|
7 | 8 | use Illuminate\Support\Facades\DB;
|
| 9 | +use Illuminate\Support\LazyCollection; |
| 10 | +use InvalidArgumentException; |
8 | 11 | use Laravel\Scout\Builder;
|
9 | 12 | use Laravel\Scout\Engines\Engine;
|
10 | 13 | use TeamTNT\Scout\Events\SearchPerformed;
|
@@ -222,6 +225,45 @@ public function map(Builder $builder, $results, $model)
|
222 | 225 | })->filter()->values();
|
223 | 226 | }
|
224 | 227 |
|
| 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 | + |
225 | 267 | /**
|
226 | 268 | * Return query builder either from given constraints, or as
|
227 | 269 | * new query. Add where statements to builder when given.
|
@@ -255,7 +297,7 @@ public function mapIds($results)
|
255 | 297 | if (empty($results['ids'])) {
|
256 | 298 | return collect();
|
257 | 299 | }
|
258 |
| - |
| 300 | + |
259 | 301 | return collect($results['ids'])->values();
|
260 | 302 | }
|
261 | 303 |
|
@@ -423,6 +465,32 @@ public function flush($model)
|
423 | 465 | }
|
424 | 466 | }
|
425 | 467 |
|
| 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 | + |
426 | 494 | /**
|
427 | 495 | * Adds a filter
|
428 | 496 | *
|
|
0 commit comments