Skip to content

Commit

Permalink
Fixed searching without setup leads to a sqlite error (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar authored Jan 18, 2024
1 parent 522de62 commit e3ea55a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Internal/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public function needsReindex(): bool

public function search(SearchParameters $parameters): SearchResult
{
if ($this->getIndexInfo()->needsSetup()) {
return SearchResult::createEmptyFromSearchParameters($parameters);
}

return (new Searcher($this, $this->filterParser, $parameters))->fetchResult();
}

Expand Down
13 changes: 13 additions & 0 deletions src/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public function __construct(
) {
}

public static function createEmptyFromSearchParameters(SearchParameters $searchParameters): self
{
return new self(
[],
$searchParameters->getQuery(),
0,
$searchParameters->getHitsPerPage(),
$searchParameters->getPage(),
0,
0
);
}

/**
* @return array<array<string, mixed>>
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/Functional/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,21 @@ public function testTypoTolerance(TypoTolerance $typoTolerance, string $query, a
$this->searchAndAssertResults($loupe, $searchParameters, $expectedResults);
}

public function testWithoutSetup(): void
{
$loupe = $this->createLoupe(Configuration::create());
$searchParameters = SearchParameters::create()->withQuery('foobar');

$this->searchAndAssertResults($loupe, $searchParameters, [
'hits' => [],
'query' => 'foobar',
'hitsPerPage' => 20,
'page' => 1,
'totalPages' => 0,
'totalHits' => 0,
]);
}

public static function typoToleranceProvider(): \Generator
{
yield 'Test finds exact match when typo tolerance is disabled' => [
Expand Down

0 comments on commit e3ea55a

Please sign in to comment.