]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/tsearch.sql
Fix XML tag namespace change inadvertantly missed from previous fix. Add
[postgresql] / src / test / regress / sql / tsearch.sql
1 --
2 -- Sanity checks for text search catalogs
3 --
4 -- NB: we assume the oidjoins test will have caught any dangling links,
5 -- that is OID or REGPROC fields that are not zero and do not match some
6 -- row in the linked-to table.  However, if we want to enforce that a link
7 -- field can't be 0, we have to check it here.
8
9 -- Find unexpected zero link entries
10
11 SELECT oid, prsname
12 FROM pg_ts_parser
13 WHERE prsnamespace = 0 OR prsstart = 0 OR prstoken = 0 OR prsend = 0 OR
14       -- prsheadline is optional
15       prslextype = 0;
16
17 SELECT oid, dictname
18 FROM pg_ts_dict
19 WHERE dictnamespace = 0 OR dictowner = 0 OR dicttemplate = 0;
20
21 SELECT oid, tmplname
22 FROM pg_ts_template
23 WHERE tmplnamespace = 0 OR tmpllexize = 0;  -- tmplinit is optional
24
25 SELECT oid, cfgname
26 FROM pg_ts_config
27 WHERE cfgnamespace = 0 OR cfgowner = 0 OR cfgparser = 0;
28
29 SELECT mapcfg, maptokentype, mapseqno
30 FROM pg_ts_config_map
31 WHERE mapcfg = 0 OR mapdict = 0;
32
33 -- Look for pg_ts_config_map entries that aren't one of parser's token types
34 SELECT * FROM
35   ( SELECT oid AS cfgid, (ts_token_type(cfgparser)).tokid AS tokid
36     FROM pg_ts_config ) AS tt 
37 RIGHT JOIN pg_ts_config_map AS m
38     ON (tt.cfgid=m.mapcfg AND tt.tokid=m.maptokentype)
39 WHERE
40     tt.cfgid IS NULL OR tt.tokid IS NULL;
41
42 -- test basic text search behavior without indexes, then with
43
44 SELECT count(*) FROM test_tsvector WHERE a @@ 'wr|qh';
45 SELECT count(*) FROM test_tsvector WHERE a @@ 'wr&qh';
46 SELECT count(*) FROM test_tsvector WHERE a @@ 'eq&yt';
47 SELECT count(*) FROM test_tsvector WHERE a @@ 'eq|yt';
48 SELECT count(*) FROM test_tsvector WHERE a @@ '(eq&yt)|(wr&qh)';
49 SELECT count(*) FROM test_tsvector WHERE a @@ '(eq|yt)&(wr|qh)';
50
51 create index wowidx on test_tsvector using gist (a);
52
53 SET enable_seqscan=OFF;
54
55 SELECT count(*) FROM test_tsvector WHERE a @@ 'wr|qh';
56 SELECT count(*) FROM test_tsvector WHERE a @@ 'wr&qh';
57 SELECT count(*) FROM test_tsvector WHERE a @@ 'eq&yt';
58 SELECT count(*) FROM test_tsvector WHERE a @@ 'eq|yt';
59 SELECT count(*) FROM test_tsvector WHERE a @@ '(eq&yt)|(wr&qh)';
60 SELECT count(*) FROM test_tsvector WHERE a @@ '(eq|yt)&(wr|qh)';
61
62 RESET enable_seqscan;
63
64 DROP INDEX wowidx;
65
66 CREATE INDEX wowidx ON test_tsvector USING gin (a);
67
68 SET enable_seqscan=OFF;
69
70 SELECT count(*) FROM test_tsvector WHERE a @@ 'wr|qh';
71 SELECT count(*) FROM test_tsvector WHERE a @@ 'wr&qh';
72 SELECT count(*) FROM test_tsvector WHERE a @@ 'eq&yt';
73 SELECT count(*) FROM test_tsvector WHERE a @@ 'eq|yt';
74 SELECT count(*) FROM test_tsvector WHERE a @@ '(eq&yt)|(wr&qh)';
75 SELECT count(*) FROM test_tsvector WHERE a @@ '(eq|yt)&(wr|qh)';
76   
77 RESET enable_seqscan;
78 INSERT INTO test_tsvector VALUES ('???', 'DFG:1A,2B,6C,10 FGH');
79 SELECT * FROM ts_stat('SELECT a FROM test_tsvector') ORDER BY ndoc DESC, nentry DESC, word LIMIT 10;
80 SELECT * FROM ts_stat('SELECT a FROM test_tsvector', 'AB') ORDER BY ndoc DESC, nentry DESC, word;
81
82 --dictionaries and to_tsvector
83
84 SELECT ts_lexize('english_stem', 'skies');
85 SELECT ts_lexize('english_stem', 'identity');
86
87 SELECT * FROM ts_token_type('default');
88
89 SELECT * FROM ts_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>">
90 /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
91 <i <b> wow  < jqw <> qwerty');
92
93 SELECT 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>">
94 /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
95 <i <b> wow  < jqw <> qwerty');
96
97 SELECT 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>">
98 /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
99 <i <b> wow  < jqw <> qwerty'));
100
101 -- ts_debug
102
103 SELECT * from ts_debug('<myns:foo-bar_baz.blurfl>abc&nm1;def&#xa9;ghi&#245;jkl</myns:foo-bar_baz.blurfl>');
104
105 -- to_tsquery
106
107 SELECT to_tsquery('english', 'qwe & sKies ');
108 SELECT to_tsquery('simple', 'qwe & sKies ');
109 SELECT to_tsquery('english', '''the wether'':dc & ''           sKies '':BC ');
110 SELECT to_tsquery('english', 'asd&(and|fghj)');
111 SELECT to_tsquery('english', '(asd&and)|fghj');
112 SELECT to_tsquery('english', '(asd&!and)|fghj');
113 SELECT to_tsquery('english', '(the|and&(i&1))&fghj');
114
115 SELECT plainto_tsquery('english', 'the and z 1))& fghj');
116 SELECT plainto_tsquery('english', 'foo bar') && plainto_tsquery('english', 'asd');
117 SELECT plainto_tsquery('english', 'foo bar') || plainto_tsquery('english', 'asd fg');
118 SELECT plainto_tsquery('english', 'foo bar') || !!plainto_tsquery('english', 'asd fg');
119 SELECT plainto_tsquery('english', 'foo bar') && 'asd | fg';
120
121 SELECT ts_rank_cd(to_tsvector('english', 'Erosion It took the sea a thousand years,
122 A thousand years to trace
123 The granite features of this cliff
124 In crag and scarp and base.
125 It took the sea an hour one night
126 An hour of storm to place
127 The sculpture of these granite seams,
128 Upon a woman s face. E.  J.  Pratt  (1882 1964)
129 '), to_tsquery('english', 'sea&thousand&years'));
130
131 SELECT ts_rank_cd(to_tsvector('english', 'Erosion It took the sea a thousand years,
132 A thousand years to trace
133 The granite features of this cliff
134 In crag and scarp and base.
135 It took the sea an hour one night
136 An hour of storm to place
137 The sculpture of these granite seams,
138 Upon a woman s face. E.  J.  Pratt  (1882 1964)
139 '), to_tsquery('english', 'granite&sea'));
140
141 SELECT ts_rank_cd(to_tsvector('english', 'Erosion It took the sea a thousand years,
142 A thousand years to trace
143 The granite features of this cliff
144 In crag and scarp and base.
145 It took the sea an hour one night
146 An hour of storm to place
147 The sculpture of these granite seams,
148 Upon a woman s face. E.  J.  Pratt  (1882 1964)
149 '), to_tsquery('english', 'sea'));
150
151 --headline tests
152 SELECT ts_headline('english', 'Erosion It took the sea a thousand years,
153 A thousand years to trace
154 The granite features of this cliff
155 In crag and scarp and base.
156 It took the sea an hour one night
157 An hour of storm to place
158 The sculpture of these granite seams,
159 Upon a woman s face. E.  J.  Pratt  (1882 1964)
160 ', to_tsquery('english', 'sea&thousand&years'));
161
162 SELECT ts_headline('english', 'Erosion It took the sea a thousand years,
163 A thousand years to trace
164 The granite features of this cliff
165 In crag and scarp and base.
166 It took the sea an hour one night
167 An hour of storm to place
168 The sculpture of these granite seams,
169 Upon a woman s face. E.  J.  Pratt  (1882 1964)
170 ', to_tsquery('english', 'granite&sea'));
171
172 SELECT ts_headline('english', 'Erosion It took the sea a thousand years,
173 A thousand years to trace
174 The granite features of this cliff
175 In crag and scarp and base.
176 It took the sea an hour one night
177 An hour of storm to place
178 The sculpture of these granite seams,
179 Upon a woman s face. E.  J.  Pratt  (1882 1964)
180 ', to_tsquery('english', 'sea'));
181
182 SELECT ts_headline('english', '
183 <html>
184 <!-- some comment -->
185 <body>
186 Sea view wow <u>foo bar</u> <i>qq</i>
187 <a href="http://www.google.com/foo.bar.html" target="_blank">YES &nbsp;</a>
188 ff-bg
189 <script>
190        document.write(15);
191 </script>
192 </body>
193 </html>',
194 to_tsquery('english', 'sea&foo'), 'HighlightAll=true');
195
196 --Rewrite sub system
197
198 CREATE TABLE test_tsquery (txtkeyword TEXT, txtsample TEXT);
199 \set ECHO none
200 \copy test_tsquery from stdin
201 'New York'      new & york | big & apple | nyc
202 Moscow  moskva | moscow
203 'Sanct Peter'   Peterburg | peter | 'Sanct Peterburg'
204 'foo bar qq'    foo & (bar | qq) & city
205 \.
206 \set ECHO all
207
208 ALTER TABLE test_tsquery ADD COLUMN keyword tsquery;
209 UPDATE test_tsquery SET keyword = to_tsquery('english', txtkeyword);
210 ALTER TABLE test_tsquery ADD COLUMN sample tsquery;
211 UPDATE test_tsquery SET sample = to_tsquery('english', txtsample::text);
212
213
214 SELECT COUNT(*) FROM test_tsquery WHERE keyword <  'new & york';
215 SELECT COUNT(*) FROM test_tsquery WHERE keyword <= 'new & york';
216 SELECT COUNT(*) FROM test_tsquery WHERE keyword = 'new & york';
217 SELECT COUNT(*) FROM test_tsquery WHERE keyword >= 'new & york';
218 SELECT COUNT(*) FROM test_tsquery WHERE keyword >  'new & york';
219
220 CREATE UNIQUE INDEX bt_tsq ON test_tsquery (keyword);
221
222 SET enable_seqscan=OFF;
223
224 SELECT COUNT(*) FROM test_tsquery WHERE keyword <  'new & york';
225 SELECT COUNT(*) FROM test_tsquery WHERE keyword <= 'new & york';
226 SELECT COUNT(*) FROM test_tsquery WHERE keyword = 'new & york';
227 SELECT COUNT(*) FROM test_tsquery WHERE keyword >= 'new & york';
228 SELECT COUNT(*) FROM test_tsquery WHERE keyword >  'new & york';
229
230 RESET enable_seqscan;
231
232 SELECT ts_rewrite('foo & bar & qq & new & york',  'new & york'::tsquery, 'big & apple | nyc | new & york & city');
233
234 SELECT ts_rewrite('moscow', 'SELECT keyword, sample FROM test_tsquery'::text );
235 SELECT ts_rewrite('moscow & hotel', 'SELECT keyword, sample FROM test_tsquery'::text );
236 SELECT ts_rewrite('bar & new & qq & foo & york', 'SELECT keyword, sample FROM test_tsquery'::text );
237
238 SELECT ts_rewrite( 'moscow', 'SELECT keyword, sample FROM test_tsquery');
239 SELECT ts_rewrite( 'moscow & hotel', 'SELECT keyword, sample FROM test_tsquery');
240 SELECT ts_rewrite( 'bar & new & qq & foo & york', 'SELECT keyword, sample FROM test_tsquery');
241
242
243 SELECT keyword FROM test_tsquery WHERE keyword @> 'new';
244 SELECT keyword FROM test_tsquery WHERE keyword @> 'moscow';
245 SELECT keyword FROM test_tsquery WHERE keyword <@ 'new';
246 SELECT keyword FROM test_tsquery WHERE keyword <@ 'moscow';
247 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
248 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow & hotel') AS query;
249 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar &  new & qq & foo & york') AS query;
250 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
251 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow & hotel') AS query;
252 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
253
254 CREATE INDEX qq ON test_tsquery USING gist (keyword tsquery_ops);
255 SET enable_seqscan=OFF;
256
257 SELECT keyword FROM test_tsquery WHERE keyword @> 'new';
258 SELECT keyword FROM test_tsquery WHERE keyword @> 'moscow';
259 SELECT keyword FROM test_tsquery WHERE keyword <@ 'new';
260 SELECT keyword FROM test_tsquery WHERE keyword <@ 'moscow';
261 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
262 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow & hotel') AS query;
263 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar & new & qq & foo & york') AS query;
264 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow') AS query;
265 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'moscow & hotel') AS query;
266 SELECT ts_rewrite( query, 'SELECT keyword, sample FROM test_tsquery' ) FROM to_tsquery('english', 'bar &  new & qq & foo & york') AS query;
267
268 RESET enable_seqscan;
269
270 --test GUC
271 SET default_text_search_config=simple;
272
273 SELECT to_tsvector('SKIES My booKs');
274 SELECT plainto_tsquery('SKIES My booKs');
275 SELECT to_tsquery('SKIES & My | booKs');
276
277 SET default_text_search_config=english;
278
279 SELECT to_tsvector('SKIES My booKs');
280 SELECT plainto_tsquery('SKIES My booKs');
281 SELECT to_tsquery('SKIES & My | booKs');
282
283 --trigger
284 CREATE TRIGGER tsvectorupdate
285 BEFORE UPDATE OR INSERT ON test_tsvector
286 FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger(a, 'pg_catalog.english', t);
287
288 SELECT count(*) FROM test_tsvector WHERE a @@ to_tsquery('345&qwerty');
289 INSERT INTO test_tsvector (t) VALUES ('345 qwerty');
290 SELECT count(*) FROM test_tsvector WHERE a @@ to_tsquery('345&qwerty');
291 UPDATE test_tsvector SET t = null WHERE t = '345 qwerty';
292 SELECT count(*) FROM test_tsvector WHERE a @@ to_tsquery('345&qwerty');
293
294 INSERT INTO test_tsvector (t) VALUES ('345 qwerty');
295
296 SELECT count(*) FROM test_tsvector WHERE a @@ to_tsquery('345&qwerty');