Functions |
querytree(tsquery)
numnode(tsquery)
setweight(tsvector, "char")
strip(tsvector)
to_tsquery(text)
to_tsquery(regconfig, text)
to_tsvector(text)
to_tsvector(regconfig, text)
ts_debug(text)
ts_headline(text, tsquery)
ts_headline(regconfig, text, tsquery, text)
ts_headline(regconfig, text, tsquery)
ts_headline(text, tsquery, text)
ts_lexize(regdictionary, text)
ts_match_qv(tsquery, tsvector) |
ts_match_tq(text, tsquery)
ts_match_tt(text, text)
ts_match_vq(tsvector, tsquery)
ts_parse(oid, text)
ts_parse(text, text)
ts_rank(real[], tsvector, tsquery)
ts_rank(tsvector, tsquery) ts_rank(tsvector, tsquery, integer)
ts_rank_cd(tsvector, tsquery)
ts_rewrite(tsquery, tsquery, tsquery)
ts_rewrite(tsquery, text)
ts_stat(text)
ts_stat(text, text)
|
|
DDL and Data Load Examples |
CREATE TABLE sometable
( myid serial PRIMARY KEY, title varchar(255), description text,
mytsfield tsvector, myconfig regconfig);
CREATE INDEX idx_sometable_somefield
ON sometable
USING gin(to_tsvector('pg_catalog.english', mytsfield));
--This is if you don't want to store ts vector and you always want to recalc from fields as needed
CREATE INDEX idx_sometable_ts
ON sometable
USING gin(to_tsvector(myconfig, COALESCE(title,'') || ' ' || COALESCE(description)));
INSERT INTO sometable(title, description,myconfig, mytsfield)
VALUES('John Doe', 'a story about a man name John', 'pg_catalog.english',
to_tsvector('pg_catalog.english', 'John Doe' || ' ' || 'a story about a man name John'));
|
CREATE TEXT SEARCH DICTIONARY thesaurus_simple (
TEMPLATE = thesaurus,
DictFile = mythesaurus,
Dictionary = pg_catalog.english_stem
); ALTER TEXT SEARCH CONFIGURATION russian
ALTER MAPPING FOR asciiword, asciihword, hword_asciipart
WITH thesaurus_simple;
|
TRIGGER EXAMPLES |
CREATE TRIGGER mytable_mytsvfield_trigger
BEFORE INSERT OR UPDATE
ON mytable
FOR EACH ROW
EXECUTE PROCEDURE
tsvector_update_trigger('mytsvfield', 'pg_catalog.english', 'field1', 'field2');
CREATE TRIGGER mytable_mytsvfield_trigger
BEFORE INSERT OR UPDATE ON mytable
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger_column('mytsvfield', 'myconfig_column', 'field1', 'field2');
|
CREATE FUNCTION mytable_ft_trigger() RETURNS trigger AS $$
begin
new.tsv :=
setweight(to_tsvector('pg_catalog.english',
coalesce(new.field1,'')), 'A') ||
setweight(to_tsvector('pg_catalog.english',
coalesce(new.field2,'')), 'B');
return new;
end
$$ LANGUAGE plpgsql;
CREATE TRIGGER mytable_trigiu BEFORE INSERT OR UPDATE
ON mytable FOR EACH ROW EXECUTE PROCEDURE mytable_ft_trigger();
|
SIMPLE QUERY EXAMPLES |
SELECT to_tsvector('english', 'My dog is sick')
@@ to_tsquery('english', 'dog & SICK');
SELECT to_tsvector('english', 'My doggy is sick')
@@ to_tsquery('english', 'dog & SICK');
SELECT to_tsvector('english', 'I want a dog')
@@@ to_tsquery('english', 'want & dogs');
|
SELECT cfgname FROM pg_catalog.pg_ts_config;
SELECT to_tsvector('english', 'I like to ski')
@@ to_tsquery('english', 'like & skiing');
SELECT to_tsvector('My dog is sick')
@@ to_tsquery('dog & SICK');
|
SELECT * FROM information_schema.views
WHERE to_tsvector(view_definition) @@ to_tsquery('sum | film');
SELECT *
FROM sometable
WHERE mytsfield @@ to_tsquery(myconfig, '(sum & store) & !film ');
|
Ranking Examples |
ALTER TABLE film ADD COLUMN ftext_weighted tsvector;
UPDATE film SET ftext_weighted = (setweight(to_tsvector(title), 'A')
|| setweight(to_tsvector(description), 'B'));
CREATE INDEX idx_books_ftext_weighted ON film
USING gin(ftext_weighted);
SELECT title, description, therank, ts_headline(title || ' ' || description, q,
'StartSel = , StopSel = , HighlightAll=TRUE') as htmlmarked_summary
FROM (SELECT title, description, ts_rank('{0,0,0.10,0.90}', ftext_weighted, q) as therank, q
FROM film, to_tsquery('(epic | documentary | chocolate) & mysql') as q
WHERE ftext_weighted @@ q
ORDER BY therank DESC
LIMIT 3) As results;
|
SELECT title, description, therank,
ts_headline(title || ' ' || description, q) as htmlmarked_summary
FROM (SELECT title, description,
ts_rank_cd('{0,0,0.9,0.10}', ftext_weighted, q) as therank, q
FROM film,
to_tsquery('(chocolate | secretary | mad) & (mysql | boring)') as q
WHERE ftext_weighted @@ q
ORDER BY therank DESC
LIMIT 3) As results;
SELECT title, description, therank,
ts_headline(title || ' ' || description, q) as htmlmarked_summary
FROM (SELECT title, description,
ts_rank_cd('{0,0,0.9,0.10}', ftext_weighted, q) as therank, q
FROM film, to_tsquery('(chocolate | secretary:A | mad:A)
& (mysql | boring)') as q
WHERE ftext_weighted @@@ q
ORDER BY therank DESC
LIMIT 3) As results;
|
http://www.postgresonline.com |
|