Skip to content

Commit

Permalink
Fix Search Parameters Issue (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
karakhanyans authored Jan 23, 2024
1 parent f6c5887 commit 1a89c3d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
30 changes: 3 additions & 27 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TypesenseEngine extends Engine
/**
* Create new Typesense engine instance.
*
* @param \Typesense $typesense
* @param Typesense $typesense
*/
public function __construct(Typesense $typesense)
{
Expand Down Expand Up @@ -261,8 +261,8 @@ public function buildSearchParameters(Builder $builder, int $page, int|null $per
'highlight_affix_num_tokens' => 4,
];

if (! empty($this->searchParameters)) {
$parameters = array_merge($parameters, $this->searchParameters);
if (! empty($builder->options)) {
$parameters = array_merge($parameters, $builder->options);
}

if (! empty($builder->orders)) {
Expand Down Expand Up @@ -522,30 +522,6 @@ protected function usesSoftDelete($model): bool
return in_array(SoftDeletes::class, class_uses_recursive($model), true);
}

/**
* Set the search options provided by user.
*
* @param array $options
* @return $this
*/
public function withSearchParameters(array $options): static
{
return $this->setSearchParameters($options);
}

/**
* Set the search options provided by user.
*
* @param array $options
* @return $this
*/
public function setSearchParameters(array $options): static
{
$this->searchParameters = $options;

return $this;
}

/**
* Dynamically proxy missing methods to the Typesense client instance.
*
Expand Down
10 changes: 9 additions & 1 deletion tests/Unit/TypesenseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Scout\Tests\Unit;

use Http\Client\Exception;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
Expand All @@ -13,6 +14,7 @@
use Typesense\Client as TypesenseClient;
use Typesense\Collection as TypesenseCollection;
use Typesense\Documents;
use Typesense\Exceptions\TypesenseClientError;

class TypesenseEngineTest extends TestCase
{
Expand Down Expand Up @@ -234,6 +236,10 @@ public function test_create_index_method_throws_exception(): void
$this->engine->createIndex('test_index');
}

/**
* @throws Exception
* @throws TypesenseClientError
*/
public function test_set_search_params_method(): void
{
// Mock the Builder
Expand All @@ -260,8 +266,10 @@ public function test_set_search_params_method(): void
'highlight_affix_num_tokens' => 4,
]);

// Set search options
$builder->options(['query_by' => 'id']);
// Call the search method
$this->engine->setSearchParameters(['query_by' => 'id'])->search($builder);
$this->engine->search($builder);
}

public function test_soft_deleted_objects_are_returned_with_only_trashed_method()
Expand Down

0 comments on commit 1a89c3d

Please sign in to comment.