3
0

15 Commits

Author SHA1 Message Date
3f57c78d01 Merge branch 'release/0.0.17' 2022-07-29 11:18:35 +01:00
e88da82267 Added loader for doctrine.yaml to bring in MATCH AGAINST method 2022-07-29 11:18:20 +01:00
319334834b Merge tag '0.0.16' into develop
Working on DI 0.0.16
2022-07-20 21:29:45 +01:00
9bad1cb2b9 Merge branch 'release/0.0.16' 2022-07-20 21:29:43 +01:00
df927a8197 Adding doctrine.yaml config 2022-07-20 21:29:33 +01:00
5c98989631 Merge tag '0.0.15' into develop
Working on DI 0.0.15
2022-07-20 21:26:13 +01:00
373087a5d5 Merge branch 'release/0.0.15' 2022-07-20 21:26:11 +01:00
205b29e07c Working on DependencyInjection 2022-07-20 21:26:00 +01:00
e1d5f094de Merge tag '0.0.14' into develop
Working on DI 0.0.14
2022-07-20 21:23:55 +01:00
8145b87242 Merge branch 'release/0.0.14' 2022-07-20 21:23:53 +01:00
d027933ad9 Working on DependencyInjection 2022-07-20 21:23:43 +01:00
cbad86a01c Merge tag '0.0.13' into develop
Working on DI 0.0.13
2022-07-20 21:21:37 +01:00
15bf7cb324 Merge branch 'release/0.0.13' 2022-07-20 21:21:35 +01:00
1b9787aa49 Working on DependencyInjection 2022-07-20 21:21:15 +01:00
9d4297659d Merge tag '0.0.12' into develop
Working on DI 0.0.12
2022-07-20 21:16:15 +01:00
4 changed files with 139 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
doctrine:
orm:
dql:
string_functions:
match: DoctrineExtensions\Query\Mysql\MatchAgainst

View File

@@ -3,6 +3,11 @@ services:
autowire: true
autoconfigure: true
pcm_search.command.index:
class: Pcm\SearchBundle\Command\SearchIndexCommand
tags:
- { name: 'console.command', command: 'pcm:search:reindex' }
pcm_search.searchable_subscriber:
class: Pcm\SearchBundle\EventSubscriber\SearchableSubscriber
public: true
@@ -14,3 +19,7 @@ services:
public: true
Pcm\SearchBundle\Service\SearchService: ~
Pcm\SearchBundle\Repository\SearchIndexRepository:
autowire: true
tags: ['doctrine.repository_service']

View File

@@ -18,5 +18,6 @@ class PcmSearchExtension extends Extension
new FileLocator(__DIR__.'/../../config')
);
$loader->load('services.yaml');
$loader->load('packages/doctrine.yaml');
}
}

View File

@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace Pcm\SearchBundle\Repository;
use Pcm\SearchBundle\Entity\SearchIndex;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\Persistence\ManagerRegistry;
class SearchIndexRepository extends ServiceEntityRepository
{
/**
* @param ManagerRegistry $registry
*/
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, SearchIndex::class);
}
/**
* Saves a SearchIndex entity
*
* @param SearchIndex $entity
* @param boolean $flush
* @return void
*/
public function add(SearchIndex $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Removes a SearchIndex entity
*
* @param SearchIndex $entity
* @param boolean $flush
* @return void
*/
public function remove(SearchIndex $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Returns a Query object ready to be paginated or used to present results.
*
* @param string $query
* @param integer $minScore
* @return Query
*/
public function findAllPagination(string $query, int $minScore = 0): Query
{
$qb = $this->createQueryBuilder('r')
->addSelect('MATCH(r.data) AGAINST(:searchText boolean) AS score')
->where(
sprintf(
'MATCH(r.data) AGAINST(:searchText boolean) > %f',
$minScore
)
)->orderBy('score', 'DESC')
->setParameter(
'searchText',
$this->convertSearchTerm($query)
);
return $qb->getQuery();
}
/**
* Takes a string $query and explodes into individual words. Each word
* is then prefixed with + and ends with *, making the full text search
* operate as wildcard on all words
*
* @param string $query
* @return string
*/
private function convertSearchTerm(string $query): string
{
$extractedWords = [];
$sanitisedString = preg_replace('/[^\w^\d]/', ' ', $query);
$words = mb_split('\s', preg_replace(['/([^\w+])/','/(\s+)/'], ' ', $sanitisedString));
foreach ($words as $word) {
if (strlen($word)< 1) {
//
continue;
}
$word = strtoupper($word);
$extractedWords[$word] = $word;
}
array_walk(
$extractedWords,
function(&$word) {
// require every word but allow matching just the start
$word = '+' . $word . '*';
}
);
return implode(' ', $extractedWords);
}
/**
* Clears the index table of all results
* @return void
*/
public function clearIndex(): void
{
$this
->createQueryBuilder('s')
->delete()
->getQuery()
->execute();
}
}