diff --git a/README.md b/README.md index 85f3c7d..5b52c83 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ class Post extends Model implements TypesenseSearch public function getCollectionSchema(): array { return [ - 'name' => $this->getTable(), + 'name' => $this->searchableAs(), 'fields' => [ [ 'name' => 'title', diff --git a/composer.json b/composer.json index f2fdc81..fdc43c6 100644 --- a/composer.json +++ b/composer.json @@ -34,18 +34,12 @@ } }, "require": { - "php": "~7.2", - "laravel/scout": "7.*|^8.0", - "illuminate/bus": "^6.0|^7.0", - "illuminate/contracts": "^6.0|^7.0", - "illuminate/database": "^6.0|^7.0", - "illuminate/pagination": "^6.0|^7.0", - "illuminate/queue": "^6.0|^7.0", - "illuminate/support": "^6.0|^7.0", - "devloopsnet/typesens-php": "^2.0" + "php": ">=7.4", + "laravel/scout": "^8.0", + "typesense/typesense-php": "^4.0" }, "suggest": { - "devloopsnet/typesens-php": "Required to use the Typesense php client." + "typesense/typesense-php": "Required to use the Typesense php client." }, "require-dev": { "phpunit/phpunit": "^8.0|^9.0", diff --git a/src/Engines/TypesenseSearchEngine.php b/src/Engines/TypesenseSearchEngine.php index f321506..7e846f4 100644 --- a/src/Engines/TypesenseSearchEngine.php +++ b/src/Engines/TypesenseSearchEngine.php @@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model; use Devloops\LaravelTypesense\Typesense; use GuzzleHttp\Exception\GuzzleException; -use Devloops\Typesence\Exceptions\TypesenseClientError; +use Typesense\Exceptions\TypesenseClientError; /** * Class TypesenseSearchEngine @@ -76,7 +76,7 @@ public function search(Builder $builder) [ 'q' => $builder->query, 'query_by' => implode(',', $builder->model->typesenseQueryBy()), - 'fiter_by' => $this->filters($builder), + 'filter_by' => $this->filters($builder), 'per_page' => $builder->limit, 'page' => 1, ] @@ -90,14 +90,16 @@ public function search(Builder $builder) public function paginate(Builder $builder, $perPage, $page) { return $this->performSearch( - $builder, - [ - 'q' => $builder->query, - 'query_by' => implode(',', $builder->model->typesenseQueryBy()), - 'fiter_by' => $this->filters($builder), - 'per_page' => $builder->limit, - 'page' => 1, - ] + $builder, + array_filter( + [ + 'q' => $builder->query, + 'query_by' => implode(',', $builder->model->typesenseQueryBy()), + 'filter_by' => $this->filters($builder), + 'per_page' => $perPage, + 'page' => $page, + ] + ) ); } @@ -106,7 +108,7 @@ public function paginate(Builder $builder, $perPage, $page) * @param array $options * * @return array|mixed - * @throws \Devloops\Typesence\Exceptions\TypesenseClientError + * @throws \Typesense\Exceptions\TypesenseClientError * @throws \GuzzleHttp\Exception\GuzzleException */ protected function performSearch(Builder $builder, array $options = []) @@ -129,15 +131,15 @@ protected function performSearch(Builder $builder, array $options = []) /** * @param \Laravel\Scout\Builder $builder * - * @return array + * @return string */ - protected function filters(Builder $builder): array + protected function filters(Builder $builder): string { return collect($builder->wheres)->map( static function ($value, $key) { return $key . ':=' . $value; } - )->values()->all(); + )->values()->implode(' && '); } /** @@ -188,11 +190,7 @@ public function getTotalCount($results): int public function flush($model): void { $collection = $this->typesense->getCollectionIndex($model); - try { - $collection->delete(); - } catch (TypesenseClientError $e) { - } catch (GuzzleException $e) { - } + $collection->delete(); } } \ No newline at end of file diff --git a/src/Typesense.php b/src/Typesense.php index a8abcb9..7af7000 100644 --- a/src/Typesense.php +++ b/src/Typesense.php @@ -1,15 +1,13 @@ client->getCollections()->{$model->getTable()}; + $index = $this->client->getCollections()->{$model->searchableAs()}; try { $index->retrieve(); @@ -64,28 +62,26 @@ private function createCollectionFromModel($model): Collection ); return $this->client->getCollections()->{$model->getTable()}; - } catch (TypesenseClientError $exception) { - throw $exception; } } /** * @param \Illuminate\Database\Eloquent\Model $model * - * @return \Devloops\Typesence\Collection - * @throws \Devloops\Typesence\Exceptions\TypesenseClientError + * @return \Typesense\Collection + * @throws \Typesense\Exceptions\TypesenseClientError * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCollectionIndex($model): Collection { - return $this->createCollectionFromModel($model); + return $this->getOrCreateCollectionFromModel($model); } /** - * @param \Devloops\Typesence\Collection $collectionIndex + * @param \Typesense\Collection $collectionIndex * @param $array * - * @throws \Devloops\Typesence\Exceptions\TypesenseClientError + * @throws \Typesense\Exceptions\TypesenseClientError * @throws \GuzzleHttp\Exception\GuzzleException */ public function upsertDocument(Collection $collectionIndex, $array): void @@ -101,14 +97,14 @@ public function upsertDocument(Collection $collectionIndex, $array): void $collectionIndex->getDocuments()->create($array); } catch (ObjectNotFound $e) { $collectionIndex->getDocuments()->create($array); - } catch (TypesenseClientError $e) { - } catch (GuzzleException $e) { } } /** - * @param \Devloops\Typesence\Collection $collectionIndex - * @param $modelId + * @param \Typesense\Collection $collectionIndex + * @param int $modelId + * @throws GuzzleException + * @throws TypesenseClientError */ public function deleteDocument(Collection $collectionIndex, $modelId): void { @@ -116,11 +112,7 @@ public function deleteDocument(Collection $collectionIndex, $modelId): void * @var $document Document */ $document = $collectionIndex->getDocuments()[(string)$modelId]; - try { - $document->delete(); - } catch (TypesenseClientError $e) { - } catch (GuzzleException $e) { - } + $document->delete(); } } \ No newline at end of file diff --git a/src/TypesenseServiceProvider.php b/src/TypesenseServiceProvider.php index 0000301..9440665 100644 --- a/src/TypesenseServiceProvider.php +++ b/src/TypesenseServiceProvider.php @@ -2,7 +2,7 @@ namespace Devloops\LaravelTypesense; -use Devloops\Typesence\Client; +use Typesense\Client; use Laravel\Scout\EngineManager; use Illuminate\Support\ServiceProvider; use Devloops\LaravelTypesense\Engines\TypesenseSearchEngine;