3
0

24 Commits

Author SHA1 Message Date
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
92842c552b Merge branch 'release/0.0.12' 2022-07-20 21:16:13 +01:00
80e584e29f Working on DependencyInjection 2022-07-20 21:16:05 +01:00
c03d2ae9ad Merge tag '0.0.11' into develop
Working on DI 0.0.11
2022-07-20 21:15:24 +01:00
c27ae18aa0 Merge branch 'release/0.0.11' 2022-07-20 21:15:22 +01:00
89c88520d9 Working on DependencyInjection 2022-07-20 21:15:13 +01:00
c19a755ed1 Merge tag '0.0.10' into develop
Working on DI 0.0.10
2022-07-20 21:09:54 +01:00
12925cc7d7 Merge branch 'release/0.0.10' 2022-07-20 21:09:52 +01:00
393a704f1c Working on DependencyInjection 2022-07-20 21:09:43 +01:00
398e2903ea Merge tag '0.0.9' into develop
Working on DI 0.0.9
2022-07-20 21:06:17 +01:00
95db65f56d Merge branch 'release/0.0.9' 2022-07-20 21:06:16 +01:00
1ec9b05413 Working on DependencyInjection 2022-07-20 21:05:54 +01:00
6a4ce3bef3 Merge tag '0.0.8' into develop
Working on DI
2022-07-20 21:02:39 +01:00
bf89b962ec Merge branch 'release/0.0.8' 2022-07-20 21:02:33 +01:00
7de30e78ac Working on DependencyInjection 2022-07-20 21:02:17 +01:00
6d65df0b9c Merge tag '0.0.7' into develop
Working on DependencyInjection
2022-07-20 21:01:11 +01:00
5d56d98325 Merge branch 'release/0.0.7' 2022-07-20 21:01:06 +01:00
2f6597a957 Working on DependencyInjection 2022-07-20 21:00:54 +01:00
93bb85f69f Merge tag '0.0.6' into develop
Working on DependencyInjection
2022-07-20 20:54:28 +01:00
218b174d4f Merge branch 'release/0.0.6' 2022-07-20 20:54:19 +01:00
2258fa0c96 Working on DependencyInjection 2022-07-20 20:54:10 +01:00
34b0e7f146 Merge tag '0.0.5' into develop
Working on DependencyInjection
2022-07-20 20:47:30 +01:00
4 changed files with 143 additions and 3 deletions

View File

@@ -1,8 +1,10 @@
PHP = docker compose run php
composer_install:
@$(PHP) composer install
composer_update:
@$(PHP) composer update
static_analysis:
@$(PHP) vendor/bin/psalm
@$(PHP) vendor/bin/psalm

View File

@@ -1,4 +1,16 @@
services:
Pcm\SearchBundle\EventSubscriber\SearchableSubscriber:
_defaults:
autowire: true
autoconfigure: true
pcm_search.searchable_subscriber:
class: Pcm\SearchBundle\EventSubscriber\SearchableSubscriber
public: true
tags:
- { name: doctrine.event_subscriber }
pcm_search.search_service:
alias: Pcm\SearchBundle\Service\SearchService
public: true
Pcm\SearchBundle\Service\SearchService: ~

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Pcm\SearchBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;

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();
}
}