Symfony 4 and FULLTEXT search

Iker Ibarguren
Jun 27 · 2 min read
mkdir -p Extensions/Doctrine
<?php


namespace
App\Extensions\Doctrine;


use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
class MatchAgainst extends FunctionNode
{
/** @var array list of \Doctrine\ORM\Query\AST\PathExpression */
protected
$pathExp = null;
/** @var string */
protected
$against = null;
/** @var bool */
protected
$booleanMode = false;
/** @var bool */
protected
$queryExpansion = false;
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
// match
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
// first Path Expression is mandatory
$this->pathExp = [];
$this->pathExp[] = $parser->StateFieldPathExpression();
// Subsequent Path Expressions are optional
$lexer = $parser->getLexer();
while ($lexer->isNextToken(Lexer::T_COMMA)) {
$parser->match(Lexer::T_COMMA);
$this->pathExp[] = $parser->StateFieldPathExpression();
}
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
// against
if
(strtolower($lexer->lookahead['value']) !== 'against') {
$parser->syntaxError('against');
}
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->against = $parser->StringPrimary();
if (strtolower($lexer->lookahead['value']) === 'boolean') {
$parser->match(Lexer::T_IDENTIFIER);
$this->booleanMode = true;
}
if (strtolower($lexer->lookahead['value']) === 'expand') {
$parser->match(Lexer::T_IDENTIFIER);
$this->queryExpansion = true;
}
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $walker)
{
$fields = [];
foreach ($this->pathExp as $pathExp) {
$fields[] = $pathExp->dispatch($walker);
}
$against = $walker->walkStringPrimary($this->against)
. ($this->booleanMode ? ' IN BOOLEAN MODE' : '')
. ($this->queryExpansion ? ' WITH QUERY EXPANSION' : '');
return sprintf('MATCH (%s) AGAINST (%s)', implode(', ', $fields), $against);
}
}
orm:
...
dql:
string_functions:
MATCH_AGAINST: App\Extensions\Doctrine\MatchAgainst
/**
*
@Route("/", name="amp_index", methods={"GET"})
*
*
@param Request $request
*
*
*
@param PaginatorInterface $paginator
*
*
@return Response
*/
public function
index(Request $request, PaginatorInterface $paginator): Response
{
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();

/** @var QueryBuilder $queryBuilder */
$queryBuilder = $em->getRepository(Amp::class)->createQueryBuilder('a');

$filter = $request->query->get('filter');
if ($filter) {
$queryBuilder->where('MATCH_AGAINST(a.clasificacion, a.expediente, a.fecha, a.observaciones, a.signatura) AGAINST(:searchterm boolean)>0')
->setParameter('searchterm', $filter);
}

$query = $queryBuilder->getQuery();

$amps = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1)/*page number*/,
$request->query->getInt('limit', 10)/*limit per page*/
);



return $this->render(
'amp/index.html.twig',
[
'amps' => $amps,
]
);
}

Iker Ibarguren

Written by

Informatikari bidaizale euskaldun eta independentea.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade