Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Fix Typesense search parameters issue #795

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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