Skip to content

Commit 907351e

Browse files
committed
Adding README.md
1 parent 84b36ec commit 907351e

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# DoctrineFullTextPostrgres
2+
3+
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/4754c670-381a-46fe-a0d6-42b189f83ebd/big.png)](https://insight.sensiolabs.com/projects/4754c670-381a-46fe-a0d6-42b189f83ebd)
4+
5+
A simple to use set of database types, and annotations to use postgresql's full text search engine with doctrine
6+
7+
## Installation
8+
* Register Doctrine Annotation:
9+
10+
```php
11+
\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace("VertigoLabs\\DoctrineFullTextPostgres\\ORM\\Mapping\\");
12+
```
13+
* Register Doctrine Type:
14+
15+
```php
16+
Type::addType('tsvector',\VertigoLabs\DoctrineFullTextPostgres\ORM\Mapping\TsVectorType::class);
17+
```
18+
* Register Doctrine Event Subscriber
19+
20+
```php
21+
$this->em->getEventManager()->addEventSubscriber(new \VertigoLabs\DoctrineFullTextPostgres\Common\TsVectorSubscriber());
22+
```
23+
24+
* Register Doctrine Functions
25+
```php
26+
$doctrineConfig->addCustomStringFunction('tsquery', \VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsQueryFunction::class);
27+
$doctrineConfig->addCustomStringFunction('tsrank', \VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsRankFunction::class);
28+
$doctrineConfig->addCustomStringFunction('tsrankcd', \VertigoLabs\DoctrineFullTextPostgres\ORM\Query\AST\Functions\TsRankCDFunction::class);
29+
```
30+
31+
## Usage
32+
* Create your entity
33+
34+
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.
35+
A TsVector annotation only requires the ```fields``` parameter. There are optional ```weight``` and ```language``` parameters as well, however they are not used yet.
36+
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
37+
38+
```php
39+
class Article
40+
{
41+
/**
42+
* @var string
43+
* @Column(name="title", type="string", nullable=false)
44+
*/
45+
private $title;
46+
47+
/**
48+
* @var TsVector
49+
* @TsVector(name="title_fts", fields={"title"})
50+
*/
51+
private $titleFTS;
52+
53+
/**
54+
* @var string
55+
* @Column(name="body", type="text", nullable=true)
56+
*/
57+
private $body;
58+
59+
/**
60+
* @var TsVector
61+
* @TsVector(name="body_fts", fields={"body"})
62+
*/
63+
private $bodyFTS;
64+
}
65+
```
66+
67+
* Insert some data
68+
69+
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.
70+
71+
```php
72+
$article = new Article();
73+
$article->setTitle('Baboons Invade Seaworld');
74+
$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');
75+
$this->em->persist($article);
76+
$this->em->flush();
77+
```
78+
79+
* Query your database!
80+
81+
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
82+
83+
```php
84+
$query = $this->em->createQuery('SELECT a FROM Article a WHERE tsquery(a.title,:searchQuery) = true');
85+
$query->setParameter('searchQuery','Baboons');
86+
$result = $query->getArrayResult();
87+
```
88+
89+
If you'd like to retrieve the ranking of your full text search, simply use the tsrank function:
90+
91+
```php
92+
$query = $this->em->createQuery('SELECT a, tsrank(a.title,:searchQuery) as rank FROM Article a WHERE tsquery(a.title,:searchQuery) = true');
93+
$query->setParameter('searchQuery','Baboons');
94+
$result = $query->getArrayResult();
95+
96+
var_dump($result[0]['rank']); // int 0.67907
97+
```
98+
99+
You can even order by rank:
100+
101+
```php
102+
$query = $this->em->createQuery('SELECT a FROM Article a WHERE tsquery(a.title,:searchQuery) = true ORDER BY tsrank(a.title,:searchQuery) DESC');
103+
```
104+
105+
## TODO
106+
* Add language to SQL field definition
107+
* Add language and weighting to queries

0 commit comments

Comments
 (0)