Skip to content

Commit

Permalink
Merge pull request #16 from Sander-Toonen/master
Browse files Browse the repository at this point in the history
Implemented plainto_tsquery. Fixes #2
  • Loading branch information
jaimz22 authored Apr 2, 2019
2 parents 577298e + b0e9d38 commit ed52e62
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 329 deletions.
30 changes: 30 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
;

$rules = [
'@Symfony' => true,
'list_syntax' => ['syntax' => 'short'],
'array_syntax' => ['syntax' => 'short'],
'linebreak_after_opening_tag' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
// Risky checks
'psr4' => true,
'ereg_to_preg' => true,
'is_null' => true,
'non_printable_character' => true,
'random_api_migration' => true,
'ternary_to_null_coalescing' => true,
];

return PhpCsFixer\Config::create()
->setRules($rules)
->setUsingCache(false)
->setFinder($finder)
->setRiskyAllowed(true)
;

60 changes: 31 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@ A simple to use set of database types, and annotations to use postgresql's full

## Installation
* Register Doctrine Annotation:

```php
\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace("VertigoLabs\\DoctrineFullTextPostgres\\ORM\\Mapping\\");
```
* Register Doctrine Type:

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

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

* Register Doctrine Functions
```php
$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

```yaml
doctrine:
dbal:
Expand All @@ -45,23 +46,24 @@ A simple to use set of database types, and annotations to use postgresql's full
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

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

## Usage
* Create your entity

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

```php
class Article
{
Expand All @@ -70,65 +72,65 @@ services:
* @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

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.

```php
$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!

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

```php
$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:

```php
$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:

```php
$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
Loading

0 comments on commit ed52e62

Please sign in to comment.