| 1 |
test_parser is an example of a custom parser for full-text |
| 2 |
search. It doesn't do anything especially useful, but can serve as |
| 3 |
a starting point for developing your own parser. |
| 4 |
|
| 5 |
test_parser recognizes words separated by white space, |
| 6 |
and returns just two token types: |
| 7 |
|
| 8 |
mydb=# SELECT * FROM ts_token_type('testparser'); |
| 9 |
tokid | alias | description |
| 10 |
-------+-------+--------------- |
| 11 |
3 | word | Word |
| 12 |
12 | blank | Space symbols |
| 13 |
(2 rows) |
| 14 |
|
| 15 |
These token numbers have been chosen to be compatible with the default |
| 16 |
parser's numbering. This allows us to use its headline() |
| 17 |
function, thus keeping the example simple. |
| 18 |
|
| 19 |
Usage |
| 20 |
===== |
| 21 |
|
| 22 |
Installing the test_parser extension creates a text search |
| 23 |
parser testparser. It has no user-configurable parameters. |
| 24 |
|
| 25 |
You can test the parser with, for example, |
| 26 |
|
| 27 |
mydb=# SELECT * FROM ts_parse('testparser', 'That''s my first own parser'); |
| 28 |
tokid | token |
| 29 |
-------+-------- |
| 30 |
3 | That's |
| 31 |
12 | |
| 32 |
3 | my |
| 33 |
12 | |
| 34 |
3 | first |
| 35 |
12 | |
| 36 |
3 | own |
| 37 |
12 | |
| 38 |
3 | parser |
| 39 |
|
| 40 |
Real-world use requires setting up a text search configuration |
| 41 |
that uses the parser. For example, |
| 42 |
|
| 43 |
mydb=# CREATE TEXT SEARCH CONFIGURATION testcfg ( PARSER = testparser ); |
| 44 |
CREATE TEXT SEARCH CONFIGURATION |
| 45 |
|
| 46 |
mydb=# ALTER TEXT SEARCH CONFIGURATION testcfg |
| 47 |
mydb-# ADD MAPPING FOR word WITH english_stem; |
| 48 |
ALTER TEXT SEARCH CONFIGURATION |
| 49 |
|
| 50 |
mydb=# SELECT to_tsvector('testcfg', 'That''s my first own parser'); |
| 51 |
to_tsvector |
| 52 |
------------------------------- |
| 53 |
'that':1 'first':3 'parser':5 |
| 54 |
(1 row) |
| 55 |
|
| 56 |
mydb=# SELECT ts_headline('testcfg', 'Supernovae stars are the brightest phenomena in galaxies', |
| 57 |
mydb(# to_tsquery('testcfg', 'star')); |
| 58 |
ts_headline |
| 59 |
----------------------------------------------------------------- |
| 60 |
Supernovae <b>stars</b> are the brightest phenomena in galaxies |
| 61 |
(1 row) |