Skip to content

Commit

Permalink
Merge pull request #2473 from bolt/enhancement/less-strict-search
Browse files Browse the repository at this point in the history
Accept less strict search like `%foo%bar%`
  • Loading branch information
bobdenotter authored Mar 21, 2021
2 parents 0753739 + aa7087d commit 751aa62
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Repository/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bolt\Repository;

use Bolt\Common\Str;
use Bolt\Configuration\Content\ContentType;
use Bolt\Doctrine\JsonHelper;
use Bolt\Entity\Content;
Expand Down Expand Up @@ -98,6 +99,10 @@ public function searchNaive(string $searchTerm, int $page, int $amountPerPage, C
$connection = $qb->getEntityManager()->getConnection();
[$where] = JsonHelper::wrapJsonFunction('t.value', $searchTerm, $connection);

// Rather than searching for '%foo bar%', search '%foo%bar%' which doesn't require
// an exact match, but requires 'foo' to appear before 'bar'.
$searchTerm = str_replace(' ', '%', Str::cleanWhitespace($searchTerm));

// The search term must match the format of the content in the database
// Therefore, it is JSON encoded and escaped with backslashes
$encodedSearchTerm = addslashes(trim(json_encode($searchTerm), '"'));
Expand Down

0 comments on commit 751aa62

Please sign in to comment.