Create your entity, (*10)
You do not have to create column annotations for your fields that will hold your full text search vectors (tsvector) the columns will be created automatically.
A TsVector annotation only requires the fields
parameter. There are optional weight
and language
parameters as well, however they are not used yet.
You do not need to set data for your TsVector field, the data will come from the fields specified in the fields
property automatically when the object is flushed to the database, (*11)
use VertigoLabs\DoctrineFullTextPostgres\ORM\Mapping\TsVector;
class Article
{
/**
* @var string
* @Column(name="title", type="string", nullable=false)
*/
private $title;
/**
* @var TsVector
* @TsVector(name="title_fts", fields={"title"})
*/
private $titleFTS;
/**
* @var string
* @Column(name="body", type="text", nullable=true)
*/
private $body;
/**
* @var TsVector
* @TsVector(name="body_fts", fields={"body"})
*/
private $bodyFTS;
}
Query your database!, (*14)
When you query your database, you'll query against the actual data. the query will be modified to search using the fields marked with the TsVector annotation automatically, (*15)
$query = $this->em->createQuery('SELECT a FROM Article a WHERE tsquery(a.title,:searchQuery) = true');
$query->setParameter('searchQuery','Baboons');
$result = $query->getArrayResult();
If you'd like to retrieve the ranking of your full text search, simply use the tsrank function:, (*16)
$query = $this->em->createQuery('SELECT a, tsrank(a.title,:searchQuery) as rank FROM Article a WHERE tsquery(a.title,:searchQuery) = true');
$query->setParameter('searchQuery','Baboons');
$result = $query->getArrayResult();
var_dump($result[0]['rank']); // int 0.67907
You can even order by rank:, (*17)
$query = $this->em->createQuery('SELECT a FROM Article a WHERE tsquery(a.title,:searchQuery) = true ORDER BY tsrank(a.title,:searchQuery) DESC');