Skip to content

Commit f0051d0

Browse files
fabkhoAnnyFabKhotaylorotwell
authored
Fix: [Typesense] Add custom index support (#908)
* fix: consider index when searching * Update TypesenseEngine.php --------- Co-authored-by: Fabian Kirchhoff <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4f03007 commit f0051d0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/Engines/TypesenseEngine.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ public function paginate(Builder $builder, $perPage, $page)
243243
*/
244244
protected function performSearch(Builder $builder, array $options = []): mixed
245245
{
246-
$documents = $this->getOrCreateCollectionFromModel($builder->model, false)->getDocuments();
246+
$documents = $this->getOrCreateCollectionFromModel(
247+
$builder->model,
248+
$builder->index,
249+
false,
250+
)->getDocuments();
251+
247252

248253
if ($builder->callback) {
249254
return call_user_func($builder->callback, $documents, $builder->query, $options);
@@ -591,16 +596,19 @@ public function deleteIndex($name)
591596
* Get collection from model or create new one.
592597
*
593598
* @param \Illuminate\Database\Eloquent\Model $model
594-
* @return TypesenseCollection
599+
* @return \Typesense\Collection
595600
*
596601
* @throws \Typesense\Exceptions\TypesenseClientError
597602
* @throws \Http\Client\Exception
598603
*/
599-
protected function getOrCreateCollectionFromModel($model, bool $indexOperation = true): TypesenseCollection
604+
protected function getOrCreateCollectionFromModel($model, ?string $collectionName = null, bool $indexOperation = true): TypesenseCollection
600605
{
601-
$method = $indexOperation ? 'indexableAs' : 'searchableAs';
606+
if (! $indexOperation) {
607+
$collectionName = $collectionName ?? $model->searchableAs();
608+
} else {
609+
$collectionName = $model->indexableAs();
610+
}
602611

603-
$collectionName = $model->{$method}();
604612
$collection = $this->typesense->getCollections()->{$collectionName};
605613

606614
if (! $indexOperation) {

0 commit comments

Comments
 (0)