From a5e702a07efe8f77d9e6b8e431b2165bf2323270 Mon Sep 17 00:00:00 2001 From: karakhanyans Date: Fri, 10 Nov 2023 21:07:54 +0400 Subject: [PATCH] Query By As String --- config/scout.php | 4 +- src/Engines/TypesenseEngine.php | 6 +- tests/Unit/TypesenseEngineTest.php | 98 +++++++++++++++++++----------- 3 files changed, 66 insertions(+), 42 deletions(-) diff --git a/config/scout.php b/config/scout.php index 0fb0b316..682d8ad3 100644 --- a/config/scout.php +++ b/config/scout.php @@ -194,9 +194,7 @@ // 'default_sorting_field' => 'created_at', // ], // 'search-parameters' => [ - // 'query_by' => [ - // 'name', - // ], + // 'query_by' => 'name, email' // ], // ], ], diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index f3fde380..cbb09fd5 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' => implode(',', config('scout.typesense.model-settings.'.get_class($builder->model).'.search-parameters.query_by')) ?? '', + 'query_by' => config('scout.typesense.model-settings.'.get_class($builder->model).'.search-parameters.query_by') ?? '', 'filter_by' => $this->filters($builder), 'per_page' => $perPage, 'page' => $page, @@ -248,10 +248,6 @@ public function buildSearchParameters(Builder $builder, int $page, int|null $per if (! empty($this->searchParameters)) { $params = array_merge($params, $this->searchParameters); - - if ($this->searchParameters['query_by']) { - $params['query_by'] = implode(',', $this->searchParameters['query_by']); - } } if (! empty($builder->orders)) { diff --git a/tests/Unit/TypesenseEngineTest.php b/tests/Unit/TypesenseEngineTest.php index 2a98f7f6..e7308aac 100644 --- a/tests/Unit/TypesenseEngineTest.php +++ b/tests/Unit/TypesenseEngineTest.php @@ -28,7 +28,7 @@ protected function setUp(): void ->getMock(); } - public function testUpdateMethod(): void + public function test_update_method(): void { // Mock models and their methods $models = [ @@ -62,7 +62,7 @@ public function testUpdateMethod(): void $this->engine->update(collect($models)); } - public function testDeleteMethod(): void + public function test_delete_method(): void { // Mock models and their methods $models = [ @@ -88,7 +88,7 @@ public function testDeleteMethod(): void $this->engine->delete(collect($models)); } - public function testSearchMethod(): void + public function test_search_method(): void { // Mock the Builder $builder = $this->createMock(Builder::class); @@ -98,19 +98,19 @@ public function testSearchMethod(): void ->method('buildSearchParameters') ->with($builder, 1) ->willReturn([ - 'q' => $builder->query, - 'query_by' => implode(',', ['id']), - 'filter_by' => '', - 'per_page' => 10, - 'page' => 1, - 'highlight_start_tag' => '', - 'highlight_end_tag' => '', - 'snippet_threshold' => 30, - 'exhaustive_search' => false, - 'use_cache' => false, - 'cache_ttl' => 60, - 'prioritize_exact_match' => true, - 'enable_overrides' => true, + 'q' => $builder->query, + 'query_by' => 'id', + 'filter_by' => '', + 'per_page' => 10, + 'page' => 1, + 'highlight_start_tag' => '', + 'highlight_end_tag' => '', + 'snippet_threshold' => 30, + 'exhaustive_search' => false, + 'use_cache' => false, + 'cache_ttl' => 60, + 'prioritize_exact_match' => true, + 'enable_overrides' => true, 'highlight_affix_num_tokens' => 4, ]); @@ -118,7 +118,7 @@ public function testSearchMethod(): void $this->engine->search($builder); } - public function testPaginateMethod(): void + public function test_paginate_method(): void { // Mock the Builder $builder = $this->createMock(Builder::class); @@ -128,19 +128,19 @@ public function testPaginateMethod(): void ->method('buildSearchParameters') ->with($builder, 2, 10) ->willReturn([ - 'q' => $builder->query, - 'query_by' => implode(',', ['id']), - 'filter_by' => '', - 'per_page' => 10, - 'page' => 2, - 'highlight_start_tag' => '', - 'highlight_end_tag' => '', - 'snippet_threshold' => 30, - 'exhaustive_search' => false, - 'use_cache' => false, - 'cache_ttl' => 60, - 'prioritize_exact_match' => true, - 'enable_overrides' => true, + 'q' => $builder->query, + 'query_by' => 'id', + 'filter_by' => '', + 'per_page' => 10, + 'page' => 2, + 'highlight_start_tag' => '', + 'highlight_end_tag' => '', + 'snippet_threshold' => 30, + 'exhaustive_search' => false, + 'use_cache' => false, + 'cache_ttl' => 60, + 'prioritize_exact_match' => true, + 'enable_overrides' => true, 'highlight_affix_num_tokens' => 4, ]); @@ -148,7 +148,7 @@ public function testPaginateMethod(): void $this->engine->paginate($builder, 10, 2); } - public function testMapIdsMethod(): void + public function test_map_ids_method(): void { // Sample search results $results = [ @@ -169,7 +169,7 @@ public function testMapIdsMethod(): void $this->assertEquals([1, 2, 3], $mappedIds->toArray()); } - public function testGetTotalCountMethod(): void + public function test_get_total_count_method(): void { // Sample search results with 'found' key $resultsWithFound = ['found' => 5]; @@ -188,7 +188,7 @@ public function testGetTotalCountMethod(): void $this->assertEquals(0, $totalCountWithoutFound); } - public function testFlushMethod(): void + public function test_flush_method(): void { // Mock a model instance $model = $this->createMock(Model::class); @@ -208,7 +208,7 @@ public function testFlushMethod(): void $this->engine->flush($model); } - public function testCreateIndexMethodThrowsException(): void + public function test_create_index_method_throws_exception(): void { // Define the expected exception class and message $expectedException = \Exception::class; @@ -221,4 +221,34 @@ public function testCreateIndexMethodThrowsException(): void // Call the createIndex method which should throw an exception $this->engine->createIndex('test_index'); } + + public function test_set_search_params_method(): void + { + // Mock the Builder + $builder = $this->createMock(Builder::class); + + // Mock the buildSearchParameters method + $this->engine->expects($this->once()) + ->method('buildSearchParameters') + ->with($builder, 1) + ->willReturn([ + 'q' => $builder->query, + 'query_by' => 'id', + 'filter_by' => '', + 'per_page' => 10, + 'page' => 1, + 'highlight_start_tag' => '', + 'highlight_end_tag' => '', + 'snippet_threshold' => 30, + 'exhaustive_search' => false, + 'use_cache' => false, + 'cache_ttl' => 60, + 'prioritize_exact_match' => true, + 'enable_overrides' => true, + 'highlight_affix_num_tokens' => 4, + ]); + + // Call the search method + $this->engine->setSearchParameters(['query_by' => 'id'])->search($builder); + } }