Symfony2 bundle with tools full-text search PostgreSQL in Doctrine 2.
Added type 'tsvector' to be used in the mapping.
Added functions 'to_tsquery' and 'ts_rank' to be used in the DQL.
Add PostgreSearchBundle in your composer.json:
{"require": {"ddmaster/postgre-search-bundle": "dev-master"}}Now tell composer to download the bundle by running the command:
$ php composer.phar update ddmaster/postgre-search-bundleComposer will install the bundle to your project's vendor/ddmaster/postgre-search-bundle directory.
Enable the bundle in the kernel:
<?php// app/AppKernel.phppublicfunctionregisterBundles()
{
$bundles = array(
// ...newDdmaster\PostgreSearchBundle\PostgreSearchBundle(),
);
}Add in your config.yml:
# Doctrine Configurationdoctrine:
dbal:
types:
tsvector: Ddmaster\PostgreSearchBundle\Dbal\TsvectorTypemapping_types:
tsvector: tsvectororm:
entity_managers:
default:
dql:
string_functions:
TSQUERY: Ddmaster\PostgreSearchBundle\DQL\TsqueryFunctionTSRANK: Ddmaster\PostgreSearchBundle\DQL\TsrankFunction/** * @var string * * @ORM\Column(name="search_fts", type="tsvector", nullable=true) */private$searchFts;$searchQuery = 'family | history';
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT b.id, sum(TSRANK(b.searchFts, :searchQuery)) as rank FROM DemoSearchBundle:Books b WHERE TSQUERY( b.searchFts, :searchQuery ) = true GROUP BY b.id ORDER BY rank DESC')
->setParameter('searchQuery', $searchQuery)
;
$result = $query->getArrayResult();Result example:
Array([0] => Array([id] => 2[rank] => 0.0607927)[1] => Array([id] => 3[rank] => 0.0303964))