1CREATE EXTENSION tsearch2; 2 3--tsvector 4SELECT '1'::tsvector; 5SELECT '1 '::tsvector; 6SELECT ' 1'::tsvector; 7SELECT ' 1 '::tsvector; 8SELECT '1 2'::tsvector; 9SELECT '''1 2'''::tsvector; 10SELECT E'''1 \\''2'''::tsvector; 11SELECT E'''1 \\''2''3'::tsvector; 12SELECT E'''1 \\''2'' 3'::tsvector; 13SELECT E'''1 \\''2'' '' 3'' 4 '::tsvector; 14select '''w'':4A,3B,2C,1D,5 a:8'; 15select 'a:3A b:2a'::tsvector || 'ba:1234 a:1B'; 16select setweight('w:12B w:13* w:12,5,6 a:1,3* a:3 w asd:1dc asd zxc:81,567,222A'::tsvector, 'c'); 17select strip('w:12B w:13* w:12,5,6 a:1,3* a:3 w asd:1dc asd'::tsvector); 18 19 20--tsquery 21SELECT '1'::tsquery; 22SELECT '1 '::tsquery; 23SELECT ' 1'::tsquery; 24SELECT ' 1 '::tsquery; 25SELECT '''1 2'''::tsquery; 26SELECT E'''1 \\''2'''::tsquery; 27SELECT '!1'::tsquery; 28SELECT '1|2'::tsquery; 29SELECT '1|!2'::tsquery; 30SELECT '!1|2'::tsquery; 31SELECT '!1|!2'::tsquery; 32SELECT '!(!1|!2)'::tsquery; 33SELECT '!(!1|2)'::tsquery; 34SELECT '!(1|!2)'::tsquery; 35SELECT '!(1|2)'::tsquery; 36SELECT '1&2'::tsquery; 37SELECT '!1&2'::tsquery; 38SELECT '1&!2'::tsquery; 39SELECT '!1&!2'::tsquery; 40SELECT '(1&2)'::tsquery; 41SELECT '1&(2)'::tsquery; 42SELECT '!(1)&2'::tsquery; 43SELECT '!(1&2)'::tsquery; 44SELECT '1|2&3'::tsquery; 45SELECT '1|(2&3)'::tsquery; 46SELECT '(1|2)&3'::tsquery; 47SELECT '1|2&!3'::tsquery; 48SELECT '1|!2&3'::tsquery; 49SELECT '!1|2&3'::tsquery; 50SELECT '!1|(2&3)'::tsquery; 51SELECT '!(1|2)&3'::tsquery; 52SELECT '(!1|2)&3'::tsquery; 53SELECT '1|(2|(4|(5|6)))'::tsquery; 54SELECT '1|2|4|5|6'::tsquery; 55SELECT '1&(2&(4&(5&6)))'::tsquery; 56SELECT '1&2&4&5&6'::tsquery; 57SELECT '1&(2&(4&(5|6)))'::tsquery; 58SELECT '1&(2&(4&(5|!6)))'::tsquery; 59SELECT E'1&(''2''&('' 4''&(\\|5 | ''6 \\'' !|&'')))'::tsquery; 60SELECT '''the wether'':dc & '' sKies '':BC & a:d b:a'; 61 62select 'a' < 'b & c'::tsquery; 63select 'a' > 'b & c'::tsquery; 64select 'a | f' < 'b & c'::tsquery; 65select 'a | ff' < 'b & c'::tsquery; 66select 'a | f | g' < 'b & c'::tsquery; 67 68select numnode( 'new'::tsquery ); 69select numnode( 'new & york'::tsquery ); 70select numnode( 'new & york | qwery'::tsquery ); 71 72create table test_tsquery (txtkeyword text, txtsample text); 73\set ECHO none 74\copy test_tsquery from stdin 75'New York' new & york | big & apple | nyc 76Moscow moskva | moscow 77'Sanct Peter' Peterburg | peter | 'Sanct Peterburg' 78'foo bar qq' foo & (bar | qq) & city 79\. 80\set ECHO all 81 82alter table test_tsquery add column keyword tsquery; 83update test_tsquery set keyword = to_tsquery('english', txtkeyword); 84alter table test_tsquery add column sample tsquery; 85update test_tsquery set sample = to_tsquery('english', txtsample::text); 86 87create unique index bt_tsq on test_tsquery (keyword); 88 89select count(*) from test_tsquery where keyword < 'new & york'; 90select count(*) from test_tsquery where keyword <= 'new & york'; 91select count(*) from test_tsquery where keyword = 'new & york'; 92select count(*) from test_tsquery where keyword >= 'new & york'; 93select count(*) from test_tsquery where keyword > 'new & york'; 94 95set enable_seqscan=off; 96 97select count(*) from test_tsquery where keyword < 'new & york'; 98select count(*) from test_tsquery where keyword <= 'new & york'; 99select count(*) from test_tsquery where keyword = 'new & york'; 100select count(*) from test_tsquery where keyword >= 'new & york'; 101select count(*) from test_tsquery where keyword > 'new & york'; 102 103set enable_seqscan=on; 104 105select rewrite('foo & bar & qq & new & york', 'new & york'::tsquery, 'big & apple | nyc | new & york & city'); 106 107select rewrite('moscow', 'select keyword, sample from test_tsquery'::text ); 108select rewrite('moscow & hotel', 'select keyword, sample from test_tsquery'::text ); 109select rewrite('bar & new & qq & foo & york', 'select keyword, sample from test_tsquery'::text ); 110 111select rewrite( ARRAY['moscow', keyword, sample] ) from test_tsquery; 112select rewrite( ARRAY['moscow & hotel', keyword, sample] ) from test_tsquery; 113select rewrite( ARRAY['bar & new & qq & foo & york', keyword, sample] ) from test_tsquery; 114 115 116select keyword from test_tsquery where keyword @> 'new'; 117select keyword from test_tsquery where keyword @> 'moscow'; 118select keyword from test_tsquery where keyword <@ 'new'; 119select keyword from test_tsquery where keyword <@ 'moscow'; 120select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where keyword <@ query; 121select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where keyword <@ query; 122select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where keyword <@ query; 123select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where query @> keyword; 124select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where query @> keyword; 125select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where query @> keyword; 126 127create index qq on test_tsquery using gist (keyword gist_tp_tsquery_ops); 128set enable_seqscan='off'; 129 130select keyword from test_tsquery where keyword @> 'new'; 131select keyword from test_tsquery where keyword @> 'moscow'; 132select keyword from test_tsquery where keyword <@ 'new'; 133select keyword from test_tsquery where keyword <@ 'moscow'; 134select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where keyword <@ query; 135select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where keyword <@ query; 136select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where keyword <@ query; 137select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow') as query where query @> keyword; 138select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'moscow & hotel') as query where query @> keyword; 139select rewrite( ARRAY[query, keyword, sample] ) from test_tsquery, to_tsquery('english', 'bar & new & qq & foo & york') as query where query @> keyword; 140set enable_seqscan='on'; 141 142 143 144select lexize('simple', 'ASD56 hsdkf'); 145select lexize('english_stem', 'SKIES Problems identity'); 146 147select * from token_type('default'); 148select * from parse('default', '345 qwe@efd.r '' http://www.com/ http://aew.werc.ewr/?ad=qwe&dw 1aew.werc.ewr/?ad=qwe&dw 2aew.werc.ewr http://3aew.werc.ewr/?ad=qwe&dw http://4aew.werc.ewr http://5aew.werc.ewr:8100/? ad=qwe&dw 6aew.werc.ewr:8100/?ad=qwe&dw 7aew.werc.ewr:8100/?ad=qwe&dw=%20%32 +4.0e-10 qwe qwe qwqwe 234.435 455 5.005 teodor@stack.net qwe-wer asdf <fr>qwer jf sdjk<we hjwer <werrwe> ewr1> ewri2 <a href="qwe<qwe>"> 149/usr/local/fff /awdf/dwqe/4325 rewt/ewr wefjn /wqe-324/ewr gist.h gist.h.c gist.c. readline 4.2 4.2. 4.2, readline-4.2 readline-4.2. 234 150<i <b> wow < jqw <> qwerty'); 151 152SELECT to_tsvector('english', '345 qwe@efd.r '' http://www.com/ http://aew.werc.ewr/?ad=qwe&dw 1aew.werc.ewr/?ad=qwe&dw 2aew.werc.ewr http://3aew.werc.ewr/?ad=qwe&dw http://4aew.werc.ewr http://5aew.werc.ewr:8100/? ad=qwe&dw 6aew.werc.ewr:8100/?ad=qwe&dw 7aew.werc.ewr:8100/?ad=qwe&dw=%20%32 +4.0e-10 qwe qwe qwqwe 234.435 455 5.005 teodor@stack.net qwe-wer asdf <fr>qwer jf sdjk<we hjwer <werrwe> ewr1> ewri2 <a href="qwe<qwe>"> 153/usr/local/fff /awdf/dwqe/4325 rewt/ewr wefjn /wqe-324/ewr gist.h gist.h.c gist.c. readline 4.2 4.2. 4.2, readline-4.2 readline-4.2. 234 154<i <b> wow < jqw <> qwerty'); 155 156SELECT length(to_tsvector('english', '345 qw')); 157 158SELECT length(to_tsvector('english', '345 qwe@efd.r '' http://www.com/ http://aew.werc.ewr/?ad=qwe&dw 1aew.werc.ewr/?ad=qwe&dw 2aew.werc.ewr http://3aew.werc.ewr/?ad=qwe&dw http://4aew.werc.ewr http://5aew.werc.ewr:8100/? ad=qwe&dw 6aew.werc.ewr:8100/?ad=qwe&dw 7aew.werc.ewr:8100/?ad=qwe&dw=%20%32 +4.0e-10 qwe qwe qwqwe 234.435 455 5.005 teodor@stack.net qwe-wer asdf <fr>qwer jf sdjk<we hjwer <werrwe> ewr1> ewri2 <a href="qwe<qwe>"> 159/usr/local/fff /awdf/dwqe/4325 rewt/ewr wefjn /wqe-324/ewr gist.h gist.h.c gist.c. readline 4.2 4.2. 4.2, readline-4.2 readline-4.2. 234 160<i <b> wow < jqw <> qwerty')); 161 162 163select to_tsquery('english', 'qwe & sKies '); 164select to_tsquery('simple', 'qwe & sKies '); 165select to_tsquery('english', '''the wether'':dc & '' sKies '':BC '); 166select to_tsquery('english', 'asd&(and|fghj)'); 167select to_tsquery('english', '(asd&and)|fghj'); 168select to_tsquery('english', '(asd&!and)|fghj'); 169select to_tsquery('english', '(the|and&(i&1))&fghj'); 170 171select plainto_tsquery('english', 'the and z 1))& fghj'); 172select plainto_tsquery('english', 'foo bar') && plainto_tsquery('english', 'asd'); 173select plainto_tsquery('english', 'foo bar') || plainto_tsquery('english', 'asd fg'); 174select plainto_tsquery('english', 'foo bar') || !!plainto_tsquery('english', 'asd fg'); 175select plainto_tsquery('english', 'foo bar') && 'asd | fg'; 176 177select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca'; 178select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca:B'; 179select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca:A'; 180select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca:C'; 181select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca:CB'; 182 183CREATE TABLE test_tsvector( t text, a tsvector ); 184 185\copy test_tsvector from 'data/test_tsearch.data' 186 187SELECT count(*) FROM test_tsvector WHERE a @@ 'wr|qh'; 188SELECT count(*) FROM test_tsvector WHERE a @@ 'wr&qh'; 189SELECT count(*) FROM test_tsvector WHERE a @@ 'eq&yt'; 190SELECT count(*) FROM test_tsvector WHERE a @@ 'eq|yt'; 191SELECT count(*) FROM test_tsvector WHERE a @@ '(eq&yt)|(wr&qh)'; 192SELECT count(*) FROM test_tsvector WHERE a @@ '(eq|yt)&(wr|qh)'; 193 194create index wowidx on test_tsvector using gist (a); 195set enable_seqscan=off; 196 197SELECT count(*) FROM test_tsvector WHERE a @@ 'wr|qh'; 198SELECT count(*) FROM test_tsvector WHERE a @@ 'wr&qh'; 199SELECT count(*) FROM test_tsvector WHERE a @@ 'eq&yt'; 200SELECT count(*) FROM test_tsvector WHERE a @@ 'eq|yt'; 201SELECT count(*) FROM test_tsvector WHERE a @@ '(eq&yt)|(wr&qh)'; 202SELECT count(*) FROM test_tsvector WHERE a @@ '(eq|yt)&(wr|qh)'; 203 204select set_curcfg('english'); 205 206CREATE TRIGGER tsvectorupdate 207BEFORE UPDATE OR INSERT ON test_tsvector 208FOR EACH ROW EXECUTE PROCEDURE tsearch2(a, t); 209 210SELECT count(*) FROM test_tsvector WHERE a @@ to_tsquery('345&qwerty'); 211 212INSERT INTO test_tsvector (t) VALUES ('345 qwerty'); 213 214SELECT count(*) FROM test_tsvector WHERE a @@ to_tsquery('345&qwerty'); 215 216UPDATE test_tsvector SET t = null WHERE t = '345 qwerty'; 217 218SELECT count(*) FROM test_tsvector WHERE a @@ to_tsquery('345&qwerty'); 219 220insert into test_tsvector (t) values ('345 qwerty copyright'); 221select count(*) FROM test_tsvector WHERE a @@ to_tsquery('345&qwerty'); 222select count(*) FROM test_tsvector WHERE a @@ to_tsquery('copyright'); 223 224select rank(' a:1 s:2C d g'::tsvector, 'a | s'); 225select rank(' a:1 s:2B d g'::tsvector, 'a | s'); 226select rank(' a:1 s:2 d g'::tsvector, 'a | s'); 227select rank(' a:1 s:2C d g'::tsvector, 'a & s'); 228select rank(' a:1 s:2B d g'::tsvector, 'a & s'); 229select rank(' a:1 s:2 d g'::tsvector, 'a & s'); 230 231insert into test_tsvector (t) values ('foo bar foo the over foo qq bar'); 232drop trigger tsvectorupdate on test_tsvector; 233select * from stat('select a from test_tsvector') order by ndoc desc, nentry desc, word collate "C"; 234insert into test_tsvector values ('1', 'a:1a,2,3b b:5a,6a,7c,8'); 235insert into test_tsvector values ('1', 'a:1a,2,3c b:5a,6b,7c,8b'); 236select * from stat('select a from test_tsvector','a') order by ndoc desc, nentry desc, word collate "C"; 237select * from stat('select a from test_tsvector','b') order by ndoc desc, nentry desc, word collate "C"; 238select * from stat('select a from test_tsvector','c') order by ndoc desc, nentry desc, word collate "C"; 239select * from stat('select a from test_tsvector','d') order by ndoc desc, nentry desc, word collate "C"; 240select * from stat('select a from test_tsvector','ad') order by ndoc desc, nentry desc, word collate "C"; 241 242select to_tsquery('english', 'skies & books'); 243 244select rank_cd(to_tsvector('Erosion It took the sea a thousand years, 245A thousand years to trace 246The granite features of this cliff 247In crag and scarp and base. 248It took the sea an hour one night 249An hour of storm to place 250The sculpture of these granite seams, 251Upon a woman s face. E. J. Pratt (1882 1964) 252'), to_tsquery('sea&thousand&years')); 253 254select rank_cd(to_tsvector('Erosion It took the sea a thousand years, 255A thousand years to trace 256The granite features of this cliff 257In crag and scarp and base. 258It took the sea an hour one night 259An hour of storm to place 260The sculpture of these granite seams, 261Upon a woman s face. E. J. Pratt (1882 1964) 262'), to_tsquery('granite&sea')); 263 264select rank_cd(to_tsvector('Erosion It took the sea a thousand years, 265A thousand years to trace 266The granite features of this cliff 267In crag and scarp and base. 268It took the sea an hour one night 269An hour of storm to place 270The sculpture of these granite seams, 271Upon a woman s face. E. J. Pratt (1882 1964) 272'), to_tsquery('sea')); 273 274select headline('Erosion It took the sea a thousand years, 275A thousand years to trace 276The granite features of this cliff 277In crag and scarp and base. 278It took the sea an hour one night 279An hour of storm to place 280The sculpture of these granite seams, 281Upon a woman s face. E. J. Pratt (1882 1964) 282', to_tsquery('sea&thousand&years')); 283 284select headline('Erosion It took the sea a thousand years, 285A thousand years to trace 286The granite features of this cliff 287In crag and scarp and base. 288It took the sea an hour one night 289An hour of storm to place 290The sculpture of these granite seams, 291Upon a woman s face. E. J. Pratt (1882 1964) 292', to_tsquery('granite&sea')); 293 294select headline('Erosion It took the sea a thousand years, 295A thousand years to trace 296The granite features of this cliff 297In crag and scarp and base. 298It took the sea an hour one night 299An hour of storm to place 300The sculpture of these granite seams, 301Upon a woman s face. E. J. Pratt (1882 1964) 302', to_tsquery('sea')); 303 304 305select headline(' 306<html> 307<!-- some comment --> 308<body> 309Sea view wow <u>foo bar</u> <i>qq</i> 310<a href="http://www.google.com/foo.bar.html" target="_blank">YES </a> 311ff-bg 312<script> 313 document.write(15); 314</script> 315</body> 316</html>', 317to_tsquery('sea&foo'), 'HighlightAll=true'); 318--check debug 319select * from public.ts_debug('Tsearch module for PostgreSQL 7.3.3'); 320 321--check ordering 322insert into test_tsvector values (null, null); 323select a is null, a from test_tsvector order by a; 324 325drop index wowidx; 326create index wowidx on test_tsvector using gin (a); 327set enable_seqscan=off; 328 329SELECT count(*) FROM test_tsvector WHERE a @@ 'wr|qh'; 330SELECT count(*) FROM test_tsvector WHERE a @@ 'wr&qh'; 331SELECT count(*) FROM test_tsvector WHERE a @@ 'eq&yt'; 332SELECT count(*) FROM test_tsvector WHERE a @@ 'eq|yt'; 333SELECT count(*) FROM test_tsvector WHERE a @@ '(eq&yt)|(wr&qh)'; 334SELECT count(*) FROM test_tsvector WHERE a @@ '(eq|yt)&(wr|qh)'; 335