Full text search in Postgresql
The majority of a web-applications needs for a search functionality.
The easiest way to develop the search functionality in database driven web-applications on their own is using of a regexps.
Usually developers remember about that there are a some disadvantages.
and SQL-query that searches for a ello.
How we see. Using a built in search engine(in this case LIKE) is a native way for database driven applications.
Postgresql has 2 data types
- The pattern matching query processes all documents every time and there is no index support.
- There are no liguistic support, eg you are searching for a document that contains entry but documents that contains entries will be missed.
- There is no relevation, ranking, e.t.c.
Regular exrepssions
Postgresql supports 3 way of using pattern matching queries:- LIKE
- SIMILAR TO
- POSIX regular expressions
Table: tst_foobar | |
id | body |
1 | Hello world |
SELECT
*
FROM
foobar
WHERE
body LIKE '%ello%'
Full text search
Another way to operate with collection of documents where document considered as "a bag of words".Postgresql has 2 data types
- tsvector, eg 'Hello world'::tsvector represents document as "a bag of words"
- tsquery, eg 'Hello | world'::tsquery supports boolean operators
SELECT
*
FROM
foobar
WHERE
body::tsvector @@ 'Hello | world'::tsquery
April 14th, 2011