From e875e901b78abf54f0f884df2be5f55af2b80149 Mon Sep 17 00:00:00 2001 From: karakhanyans Date: Fri, 10 Nov 2023 09:14:16 +0400 Subject: [PATCH] Typesense Collection Settings: Query Params --- config/scout.php | 46 +++++++++++++++++++-------------- src/EngineManager.php | 2 +- src/Engines/TypesenseEngine.php | 8 ++++-- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/config/scout.php b/config/scout.php index 39b9f5de..80aa42b5 100644 --- a/config/scout.php +++ b/config/scout.php @@ -153,7 +153,7 @@ */ 'typesense' => [ - 'client_configuration' => [ + 'client-settings' => [ 'api_key' => env('TYPESENSE_API_KEY', 'xyz'), 'nodes' => [ [ @@ -174,25 +174,31 @@ 'num_retries' => env('TYPESENSE_NUM_RETRIES', 3), 'retry_interval_seconds' => env('TYPESENSE_RETRY_INTERVAL_SECONDS', 1), ], - 'model_configuration' => [ - // 'users' => [ // name of the table - // 'name' => 'users', // name of the table - // 'fields' => [ - // [ - // 'name' => 'id', - // 'type' => 'string', - // ], - // [ - // 'name' => 'name', - // 'type' => 'string', - // ], - // [ - // 'name' => 'created_at', - // 'type' => 'int64', - // ], - // ], - // 'default_sorting_field' => 'created_at', - // ], + 'collection-settings' => [ +// User::class => [ +// 'schema' => [ +// 'fields' => [ +// [ +// 'name' => 'id', +// 'type' => 'string', +// ], +// [ +// 'name' => 'name', +// 'type' => 'string', +// ], +// [ +// 'name' => 'created_at', +// 'type' => 'int64', +// ], +// ], +// 'default_sorting_field' => 'created_at', +// ], +// 'search-options' => [ +// 'query_by' => [ +// 'name', +// ], +// ], +// ], ], ], ]; diff --git a/src/EngineManager.php b/src/EngineManager.php index e232a122..3f005b13 100644 --- a/src/EngineManager.php +++ b/src/EngineManager.php @@ -151,7 +151,7 @@ public function createTypesenseDriver() { $this->ensureTypesenseClientIsInstalled(); - return new TypesenseEngine(new Typesense(config('scout.typesense.client_configuration'))); + return new TypesenseEngine(new Typesense(config('scout.typesense.client-settings'))); } /** diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 7060f24a..69629e32 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -231,7 +231,7 @@ public function buildSearchParameters(Builder $builder, int $page, int|null $per { $params = [ 'q' => $builder->query, - 'query_by' => '', + 'query_by' => implode(',', config('scout.typesense.collection-settings.'.get_class($builder->model).'.search-options.query_by')) ?? '', 'filter_by' => $this->filters($builder), 'per_page' => $perPage, 'page' => $page, @@ -509,7 +509,11 @@ protected function getOrCreateCollectionFromModel($model): TypesenseCollection return $index; } catch (ObjectNotFound $exception) { - $schema = config('scout.typesense.model_configuration.'.$model->getTable()) ?? []; + $schema = config('scout.typesense.collection-settings.'.get_class($model).'.schema') ?? []; + + if (!isset($schema['name'])) { + $schema['name'] = $model->searchableAs(); + } $this->typesense->getCollections()->create($schema);