2017 © Pedro Peláez
 

library doctrine-full-text-postgres

Tools to use full-text searching in Postgresql with Doctrine

image

vertigolabs/doctrine-full-text-postgres

Tools to use full-text searching in Postgresql with Doctrine

  • Friday, May 5, 2017
  • by jaimz22
  • Repository
  • 3 Watchers
  • 5 Stars
  • 8,777 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 9 Forks
  • 6 Open issues
  • 2 Versions
  • 18 % Grown

The README.md

DoctrineFullTextPostrgres

SensioLabsInsight, (*1)

A simple to use set of database types, and annotations to use postgresql's full text search engine with doctrine, (*2)

Installation

  • Register Doctrine Annotation:, (*3)

    \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace("VertigoLabs\\DoctrineFullTextPostgres\\ORM\\Mapping\\");
    
  • Register Doctrine Type:, (*4)

    Type::addType('tsvector',\VertigoLabs\DoctrineFullTextPostgres\ORM\Mapping\TsVectorType::class);
    
  • Register Doctrine Event Subscriber, (*5)

    $this->em->getEventManager()->addEventSubscriber(new \VertigoLabs\DoctrineFullTextPostgres\Common\TsVectorSubscriber());
    
  • Register Doctrine Functions, (*6)

    $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);
    

Symfony installation

  • Add to config, (*7)

    ```yaml doctrine: dbal: types: tsvector: VertigoLabs\DoctrineFullTextPostgres\DBAL\Types\TsVector mapping_types: tsvector: tsvector orm: entity_managers: default: dql: string_functions: tsquery: VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsQueryFunction tsplainquery: VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsPlainQueryFunction tsrank: VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsRankFunction tsrankcd: VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsRankCDFunction, (*8)

services: vertigolabs.doctrinefulltextpostgres.listener: class: VertigoLabs\DoctrineFullTextPostgres\Common\TsVectorSubscriber tags: - { name: doctrine.event_subscriber, connection: default } ```, (*9)

Usage

  • 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; }
  • Insert some data, (*12)

    You do not need to worry about setting any data to the fields marked with the TsVector annotation. The data for these fields will be automatically populated when you flush your changes to the database., (*13)

    $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 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');
    

TODO

  • Add language to SQL field definition
  • Add language and weighting to queries

The Versions

05/05 2017

dev-master

9999999-dev

Tools to use full-text searching in Postgresql with Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Murray

20/09 2015

v1.0

1.0.0.0

Tools to use full-text searching in Postgresql with Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

by James Murray