3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # Tests focused on phrase queries.
15 source [file join [file dirname [info script]] fts5_common.tcl]
16 set testprefix fts5origintext
18 # If SQLITE_ENABLE_FTS5 is defined, omit this file.
24 foreach_detail_mode $testprefix {
26 sqlite3_fts5_register_origintext db
28 CREATE VIRTUAL TABLE ft USING fts5(
29 x, tokenize="origintext unicode61", detail=%DETAIL%
31 CREATE VIRTUAL TABLE vocab USING fts5vocab(ft, instance);
35 INSERT INTO ft VALUES('Hello world');
39 INSERT INTO ft(ft) VALUES('integrity-check');
42 proc b {x} { string map [list "\0" "."] $x }
46 select b(term) from vocab;
53 SELECT rowid FROM ft('Hello');
56 #-------------------------------------------------------------------------
59 # Return a random integer between 0 and n-1.
62 expr {abs(int(rand()*$n))}
65 proc select_one {list} {
67 lindex $list [random $n]
72 a b c d e f g h i j k l m n o p q r s t u v w x y z
73 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
76 set term [select_one $first_letter]
77 append term [random 100]
81 set nTerm [expr [random 5] + 5]
83 for {set ii 0} {$ii < $nTerm} {incr ii} {
88 db func document document
90 sqlite3_fts5_register_origintext db
92 CREATE VIRTUAL TABLE ft USING fts5(
93 x, tokenize="origintext unicode61", detail=%DETAIL%
95 INSERT INTO ft(ft, rank) VALUES('pgsz', 128);
96 CREATE VIRTUAL TABLE vocab USING fts5vocab(ft, instance);
100 for {set ii 0} {$ii < 500} {incr ii} {
101 execsql { INSERT INTO ft VALUES( document() ) }
105 do_execsql_test 2.2 {
106 INSERT INTO ft(ft) VALUES('integrity-check');
109 do_execsql_test 2.3 {
110 INSERT INTO ft(ft, rank) VALUES('merge', 16);
113 do_execsql_test 2.4 {
114 INSERT INTO ft(ft) VALUES('integrity-check');
117 do_execsql_test 2.5 {
118 INSERT INTO ft(ft) VALUES('optimize');
121 #-------------------------------------------------------------------------
124 sqlite3_fts5_register_origintext db
125 do_execsql_test 3.0 {
126 CREATE VIRTUAL TABLE ft USING fts5(
127 x, tokenize="origintext unicode61", detail=%DETAIL%
129 CREATE VIRTUAL TABLE vocab USING fts5vocab(ft, instance);
131 INSERT INTO ft(rowid, x) VALUES(1, 'hello');
132 INSERT INTO ft(rowid, x) VALUES(2, 'Hello');
133 INSERT INTO ft(rowid, x) VALUES(3, 'HELLO');
136 #proc b {x} { string map [list "\0" "."] $x }
138 #execsql_pp { SELECT b(term) FROM vocab }
140 do_execsql_test 3.1.1 { SELECT rowid FROM ft('hello') } 1
141 do_execsql_test 3.1.2 { SELECT rowid FROM ft('Hello') } 2
142 do_execsql_test 3.1.3 { SELECT rowid FROM ft('HELLO') } 3
144 do_execsql_test 3.2 {
145 CREATE VIRTUAL TABLE ft2 USING fts5(x,
146 tokenize="origintext unicode61",
150 CREATE VIRTUAL TABLE vocab2 USING fts5vocab(ft2, instance);
152 INSERT INTO ft2(rowid, x) VALUES(1, 'hello');
153 INSERT INTO ft2(rowid, x) VALUES(2, 'Hello');
154 INSERT INTO ft2(rowid, x) VALUES(3, 'HELLO');
156 INSERT INTO ft2(rowid, x) VALUES(10, 'helloooo');
159 #proc b {x} { string map [list "\0" "."] $x }
161 #execsql_pp { SELECT b(term) FROM vocab }
163 do_execsql_test 3.3.1 { SELECT rowid FROM ft2('hello') } {1 2 3}
164 do_execsql_test 3.3.2 { SELECT rowid FROM ft2('Hello') } {1 2 3}
165 do_execsql_test 3.3.3 { SELECT rowid FROM ft2('HELLO') } {1 2 3}
167 do_execsql_test 3.3.4 { SELECT rowid FROM ft2('hello*') } {1 2 3 10}
169 #-------------------------------------------------------------------------
172 sqlite3_fts5_register_origintext db
173 proc querytoken {cmd iPhrase iToken} {
174 set txt [$cmd xQueryToken $iPhrase $iToken]
175 string map [list "\0" "."] $txt
177 sqlite3_fts5_create_function db querytoken querytoken
179 do_execsql_test 4.0 {
180 CREATE VIRTUAL TABLE ft USING fts5(
181 x, tokenize='origintext unicode61', tokendata=1, detail=%DETAIL%
183 INSERT INTO ft VALUES('one two three four');
186 do_execsql_test 4.1 {
187 SELECT rowid, querytoken(ft, 0, 0) FROM ft('TwO')
189 do_execsql_test 4.2 {
190 SELECT rowid, querytoken(ft, 0, 0) FROM ft('one TWO ThreE')
192 do_execsql_test 4.3 {
193 SELECT rowid, querytoken(ft, 1, 0) FROM ft('one TWO ThreE')
196 if {"%DETAIL%"=="full"} {
197 # Phrase queries are only supported for detail=full.
199 do_execsql_test 4.4 {
200 SELECT rowid, querytoken(ft, 0, 2) FROM ft('"one TWO ThreE"')
202 do_catchsql_test 4.5 {
203 SELECT rowid, querytoken(ft, 0, 3) FROM ft('"one TWO ThreE"')
205 do_catchsql_test 4.6 {
206 SELECT rowid, querytoken(ft, 1, 0) FROM ft('"one TWO ThreE"')
208 do_catchsql_test 4.7 {
209 SELECT rowid, querytoken(ft, -1, 0) FROM ft('"one TWO ThreE"')
213 #-------------------------------------------------------------------------
216 sqlite3_fts5_register_origintext db
217 proc insttoken {cmd iIdx iToken} {
218 set txt [$cmd xInstToken $iIdx $iToken]
219 string map [list "\0" "."] $txt
221 sqlite3_fts5_create_function db insttoken insttoken
222 fts5_aux_test_functions db
224 do_execsql_test 5.0 {
225 CREATE VIRTUAL TABLE ft USING fts5(
226 x, tokenize='origintext unicode61', tokendata=1, detail=%DETAIL%
228 INSERT INTO ft VALUES('one ONE One oNe oNE one');
231 do_execsql_test 5.1 {
232 SELECT insttoken(ft, 0, 0),
240 one one.ONE one.One one.oNe one.oNE one
243 do_execsql_test 5.2 {
244 SELECT insttoken(ft, 1, 0) FROM ft('one');
249 do_execsql_test 5.3 {
250 SELECT fts5_test_poslist(ft) FROM ft('one');
252 {0.0.0 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5}
255 #-------------------------------------------------------------------------
256 # Test the xInstToken() API with:
258 # * a non tokendata=1 table.
262 sqlite3_fts5_register_origintext db
263 do_execsql_test 6.0 {
264 CREATE VIRTUAL TABLE ft USING fts5(
265 x, y, tokenize='origintext unicode61', detail=%DETAIL%
268 INSERT INTO ft VALUES('One Two', 'Three two');
269 INSERT INTO ft VALUES('three Three', 'one One');
273 for {set iTok 0} {$iTok < [$cmd xInstCount]} {incr iTok} {
274 set txt [$cmd xInstToken $iTok 0]
275 set txt [string map [list "\0" "."] $txt]
280 sqlite3_fts5_create_function db tokens tokens
282 do_execsql_test 6.1 {
283 SELECT rowid, tokens(ft) FROM ft('One');
284 } {1 one.One 2 one.One}
286 do_execsql_test 6.2 {
287 SELECT rowid, tokens(ft) FROM ft('on*');
290 do_execsql_test 6.3 {
291 SELECT rowid, tokens(ft) FROM ft('Three*');