Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Fix SearchResult return value in custom search callable #88

Merged
merged 1 commit into from
Feb 1, 2021
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
5 changes: 4 additions & 1 deletion src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use MeiliSearch\Client as Meilisearch;
use MeiliSearch\Search\SearchResult;

class MeilisearchEngine extends Engine
{
Expand Down Expand Up @@ -120,12 +121,14 @@ protected function performSearch(Builder $builder, array $searchParams = [])
$meilisearch = $this->meilisearch->index($builder->index ?: $builder->model->searchableAs());

if ($builder->callback) {
return call_user_func(
$result = call_user_func(
$builder->callback,
$meilisearch,
$builder->query,
$searchParams
);

return $result instanceof SearchResult ? $result->getRaw() : $result;
}

return $meilisearch->rawSearch($builder->query, $searchParams);
Expand Down
65 changes: 65 additions & 0 deletions tests/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MeiliSearch\Endpoints\Indexes;
use Meilisearch\Scout\Engines\MeilisearchEngine;
use Meilisearch\Scout\Tests\Fixtures\SearchableModel;
use MeiliSearch\Search\SearchResult;
use Mockery as m;
use stdClass;

Expand Down Expand Up @@ -57,6 +58,70 @@ public function searchSendsCorrectParametersToMeilisearch()
$engine->search($builder);
}

/**
* @test
*/
public function submittingACallableSearchWithSearchMethodReturnsArray()
{
$builder = new Builder(
new SearchableModel(),
$query = 'mustang',
$callable = function ($meilisearch, $query, $options) {
$options['filters'] = 'foo=1';

return $meilisearch->search($query, $options);
}
);
$client = m::mock(Client::class);
$client->shouldReceive('index')->with('table')->andReturn($index = m::mock(Indexes::class));
$index->shouldReceive('search')->with($query, ['filters' => 'foo=1'])->andReturn(new SearchResult($expectedResult = [
'hits' => [],
'offset' => 0,
'limit' => 20,
'nbHits' => 0,
'exhaustiveNbHits' => false,
'processingTimeMs' => 1,
'query' => 'mustang',
]));

$engine = new MeilisearchEngine($client);
$result = $engine->search($builder);

$this->assertSame($expectedResult, $result);
}

/**
* @test
*/
public function submittingACallableSearchWithRawSearchMethodWorks()
{
$builder = new Builder(
new SearchableModel(),
$query = 'mustang',
$callable = function ($meilisearch, $query, $options) {
$options['filters'] = 'foo=1';

return $meilisearch->rawSearch($query, $options);
}
);
$client = m::mock(Client::class);
$client->shouldReceive('index')->with('table')->andReturn($index = m::mock(Indexes::class));
$index->shouldReceive('rawSearch')->with($query, ['filters' => 'foo=1'])->andReturn($expectedResult = [
'hits' => [],
'offset' => 0,
'limit' => 20,
'nbHits' => 0,
'exhaustiveNbHits' => false,
'processingTimeMs' => 1,
'query' => 'mustang',
]);

$engine = new MeilisearchEngine($client);
$result = $engine->search($builder);

$this->assertSame($expectedResult, $result);
}

/** @test */
public function mapIdsReturnsEmptyCollectionIfNoHits()
{
Expand Down