PHP code example of vertigolabs / doctrine-full-text-postgres

1. Go to this page and download the library: Download vertigolabs/doctrine-full-text-postgres library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vertigolabs / doctrine-full-text-postgres example snippets


 \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace("VertigoLabs\\DoctrineFullTextPostgres\\ORM\\Mapping\\");
 

 Type::addType('tsvector',\VertigoLabs\DoctrineFullTextPostgres\ORM\Mapping\TsVectorType::class);
 

 $this->em->getEventManager()->addEventSubscriber(new \VertigoLabs\DoctrineFullTextPostgres\Common\TsVectorSubscriber());
 

 $doctrineConfig->addCustomStringFunction('tsquery', \VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsQueryFunction::class);
 $doctrineConfig->addCustomStringFunction('tsplainquery', \VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsPlainQueryFunction::class);
 $doctrineConfig->addCustomStringFunction('tsrank', \VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsRankFunction::class);
 $doctrineConfig->addCustomStringFunction('tsrankcd', \VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsRankCDFunction::class);
 

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

  $article = new Article();
  $article->setTitle('Baboons Invade Seaworld');
  $article->setBody('In a crazy turn of events a pack a rabid red baboons invade Seaworld. Officials say that the Dolphins are being held hostage');
  $this->em->persist($article);
  $this->em->flush();
  

  $query = $this->em->createQuery('SELECT a FROM Article a WHERE tsquery(a.title,:searchQuery) = true');
  $query->setParameter('searchQuery','Baboons');
  $result = $query->getArrayResult();
  

  $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
  

  $query = $this->em->createQuery('SELECT a FROM Article a WHERE tsquery(a.title,:searchQuery) = true ORDER BY tsrank(a.title,:searchQuery) DESC');