Search init

This commit is contained in:
Ernest Wiśniewski 2023-05-25 22:57:55 +02:00
parent afdb3edefb
commit 2590027273
3 changed files with 63 additions and 12 deletions

View file

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230525203803 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
$this->addSql("ALTER TABLE entry ADD COLUMN title_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', title)) STORED;");
$this->addSql("ALTER TABLE entry ADD COLUMN body_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', body)) STORED;");
$this->addSql("ALTER TABLE post ADD COLUMN body_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', body)) STORED;");
$this->addSql("ALTER TABLE post_comment ADD COLUMN body_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', body)) STORED;");
$this->addSql("ALTER TABLE entry_comment ADD COLUMN body_ts tsvector GENERATED ALWAYS AS (to_tsvector('english', body)) STORED;");
$this->addSql("CREATE INDEX entry_title_ts_idx ON entry USING GIN (title_ts);");
$this->addSql("CREATE INDEX entry_body_ts_idx ON entry USING GIN (body_ts);");
$this->addSql("CREATE INDEX post_body_ts_idx ON post USING GIN (body_ts);");
$this->addSql("CREATE INDEX post_comment_body_ts_idx ON post_comment USING GIN (body_ts);");
$this->addSql("CREATE INDEX entry_comment_body_ts_idx ON entry_comment USING GIN (body_ts);");
}
public function down(Schema $schema): void
{
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE entry DROP title_ts');
$this->addSql('ALTER TABLE entry DROP body_ts');
$this->addSql('ALTER TABLE post DROP body_ts');
$this->addSql('ALTER TABLE post_comment DROP body_ts');
$this->addSql('ALTER TABLE entry_comment DROP body_ts');
$this->addSql('DROP INDEX entry_title_ts_idx');
$this->addSql('DROP INDEX entry_body_ts_idx');
$this->addSql('DROP INDEX post_body_ts_idx');
$this->addSql('DROP INDEX post_comment_body_ts_idx');
$this->addSql('DROP INDEX entry_comment_body_ts_idx');
}
}

View file

@ -76,13 +76,14 @@ class SearchController extends AbstractController
}
}
$res = $this->manager->findPaginated($query, $this->getPageNb($request));
return $this->render(
'search/front.html.twig',
[
'objects' => $objects,
'results' => $this->overviewManager->buildList(
$this->manager->findPaginated($query, $this->getPageNb($request))
),
'results' => $this->overviewManager->buildList($res),
'pagination' => $res,
'q' => $request->query->get('q'),
]
);

View file

@ -21,7 +21,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class SearchRepository
{
public const PER_PAGE = 50;
public const PER_PAGE = 25;
public function __construct(private EntityManagerInterface $entityManager)
{
@ -89,7 +89,7 @@ class SearchRepository
$countAll = $pagerfanta->count();
try {
$pagerfanta->setMaxPerPage(20000);
$pagerfanta->setMaxPerPage(2000);
$pagerfanta->setCurrentPage(1);
} catch (NotValidCurrentPageException $e) {
throw new NotFoundHttpException();
@ -105,13 +105,13 @@ class SearchRepository
// @todo union adapter
$conn = $this->entityManager->getConnection();
$sql = "
(SELECT id, created_at, visibility, 'entry' AS type FROM entry WHERE body ILIKE '%".$query."%' OR title ILIKE '%".$query."%' AND visibility = '".VisibilityInterface::VISIBILITY_VISIBLE."')
UNION
(SELECT id, created_at, visibility, 'entry_comment' AS type FROM entry_comment WHERE body ILIKE '%".$query."%' AND visibility = '".VisibilityInterface::VISIBILITY_VISIBLE."')
UNION
(SELECT id, created_at, visibility, 'post' AS type FROM post WHERE body ILIKE '%".$query."%' AND visibility = '".VisibilityInterface::VISIBILITY_VISIBLE."')
UNION
(SELECT id, created_at, visibility, 'post_comment' AS type FROM post_comment WHERE body ILIKE '%".$query."%' AND visibility = '".VisibilityInterface::VISIBILITY_VISIBLE."')
(SELECT id, created_at, visibility, 'entry' AS type FROM entry WHERE body_ts @@ plainto_tsquery('".$query."') = true OR title_ts @@ plainto_tsquery('".$query."') = true AND visibility = '".VisibilityInterface::VISIBILITY_VISIBLE."')
UNION
(SELECT id, created_at, visibility, 'entry_comment' AS type FROM entry_comment WHERE body_ts @@ plainto_tsquery('".$query."') = true AND visibility = '".VisibilityInterface::VISIBILITY_VISIBLE."')
UNION
(SELECT id, created_at, visibility, 'post' AS type FROM post WHERE body_ts @@ plainto_tsquery('".$query."') = true AND visibility = '".VisibilityInterface::VISIBILITY_VISIBLE."')
UNION
(SELECT id, created_at, visibility, 'post_comment' AS type FROM post_comment WHERE body_ts @@ plainto_tsquery('".$query."') = true AND visibility = '".VisibilityInterface::VISIBILITY_VISIBLE."')
ORDER BY created_at DESC
";
$stmt = $conn->prepare($sql);