fts5.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. ** 2014 May 31
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. ******************************************************************************
  12. **
  13. ** Interfaces to extend FTS5. Using the interfaces defined in this file,
  14. ** FTS5 may be extended with:
  15. **
  16. ** * custom tokenizers, and
  17. ** * custom auxiliary functions.
  18. */
  19. #ifndef _FTS5_H
  20. #define _FTS5_H
  21. #include "sqlite3.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /*************************************************************************
  26. ** CUSTOM AUXILIARY FUNCTIONS
  27. **
  28. ** Virtual table implementations may overload SQL functions by implementing
  29. ** the sqlite3_module.xFindFunction() method.
  30. */
  31. typedef struct Fts5ExtensionApi Fts5ExtensionApi;
  32. typedef struct Fts5Context Fts5Context;
  33. typedef struct Fts5PhraseIter Fts5PhraseIter;
  34. typedef void (*fts5_extension_function)(
  35. const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
  36. Fts5Context *pFts, /* First arg to pass to pApi functions */
  37. sqlite3_context *pCtx, /* Context for returning result/error */
  38. int nVal, /* Number of values in apVal[] array */
  39. sqlite3_value **apVal /* Array of trailing arguments */
  40. );
  41. struct Fts5PhraseIter {
  42. const unsigned char *a;
  43. const unsigned char *b;
  44. };
  45. /*
  46. ** EXTENSION API FUNCTIONS
  47. **
  48. ** xUserData(pFts):
  49. ** Return a copy of the pUserData pointer passed to the xCreateFunction()
  50. ** API when the extension function was registered.
  51. **
  52. ** xColumnTotalSize(pFts, iCol, pnToken):
  53. ** If parameter iCol is less than zero, set output variable *pnToken
  54. ** to the total number of tokens in the FTS5 table. Or, if iCol is
  55. ** non-negative but less than the number of columns in the table, return
  56. ** the total number of tokens in column iCol, considering all rows in
  57. ** the FTS5 table.
  58. **
  59. ** If parameter iCol is greater than or equal to the number of columns
  60. ** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
  61. ** an OOM condition or IO error), an appropriate SQLite error code is
  62. ** returned.
  63. **
  64. ** xColumnCount(pFts):
  65. ** Return the number of columns in the table.
  66. **
  67. ** xColumnSize(pFts, iCol, pnToken):
  68. ** If parameter iCol is less than zero, set output variable *pnToken
  69. ** to the total number of tokens in the current row. Or, if iCol is
  70. ** non-negative but less than the number of columns in the table, set
  71. ** *pnToken to the number of tokens in column iCol of the current row.
  72. **
  73. ** If parameter iCol is greater than or equal to the number of columns
  74. ** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
  75. ** an OOM condition or IO error), an appropriate SQLite error code is
  76. ** returned.
  77. **
  78. ** This function may be quite inefficient if used with an FTS5 table
  79. ** created with the "columnsize=0" option.
  80. **
  81. ** xColumnText:
  82. ** If parameter iCol is less than zero, or greater than or equal to the
  83. ** number of columns in the table, SQLITE_RANGE is returned.
  84. **
  85. ** Otherwise, this function attempts to retrieve the text of column iCol of
  86. ** the current document. If successful, (*pz) is set to point to a buffer
  87. ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
  88. ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
  89. ** if an error occurs, an SQLite error code is returned and the final values
  90. ** of (*pz) and (*pn) are undefined.
  91. **
  92. ** xPhraseCount:
  93. ** Returns the number of phrases in the current query expression.
  94. **
  95. ** xPhraseSize:
  96. ** If parameter iCol is less than zero, or greater than or equal to the
  97. ** number of phrases in the current query, as returned by xPhraseCount,
  98. ** 0 is returned. Otherwise, this function returns the number of tokens in
  99. ** phrase iPhrase of the query. Phrases are numbered starting from zero.
  100. **
  101. ** xInstCount:
  102. ** Set *pnInst to the total number of occurrences of all phrases within
  103. ** the query within the current row. Return SQLITE_OK if successful, or
  104. ** an error code (i.e. SQLITE_NOMEM) if an error occurs.
  105. **
  106. ** This API can be quite slow if used with an FTS5 table created with the
  107. ** "detail=none" or "detail=column" option. If the FTS5 table is created
  108. ** with either "detail=none" or "detail=column" and "content=" option
  109. ** (i.e. if it is a contentless table), then this API always returns 0.
  110. **
  111. ** xInst:
  112. ** Query for the details of phrase match iIdx within the current row.
  113. ** Phrase matches are numbered starting from zero, so the iIdx argument
  114. ** should be greater than or equal to zero and smaller than the value
  115. ** output by xInstCount(). If iIdx is less than zero or greater than
  116. ** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
  117. **
  118. ** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
  119. ** to the column in which it occurs and *piOff the token offset of the
  120. ** first token of the phrase. SQLITE_OK is returned if successful, or an
  121. ** error code (i.e. SQLITE_NOMEM) if an error occurs.
  122. **
  123. ** This API can be quite slow if used with an FTS5 table created with the
  124. ** "detail=none" or "detail=column" option.
  125. **
  126. ** xRowid:
  127. ** Returns the rowid of the current row.
  128. **
  129. ** xTokenize:
  130. ** Tokenize text using the tokenizer belonging to the FTS5 table.
  131. **
  132. ** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
  133. ** This API function is used to query the FTS table for phrase iPhrase
  134. ** of the current query. Specifically, a query equivalent to:
  135. **
  136. ** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
  137. **
  138. ** with $p set to a phrase equivalent to the phrase iPhrase of the
  139. ** current query is executed. Any column filter that applies to
  140. ** phrase iPhrase of the current query is included in $p. For each
  141. ** row visited, the callback function passed as the fourth argument
  142. ** is invoked. The context and API objects passed to the callback
  143. ** function may be used to access the properties of each matched row.
  144. ** Invoking Api.xUserData() returns a copy of the pointer passed as
  145. ** the third argument to pUserData.
  146. **
  147. ** If parameter iPhrase is less than zero, or greater than or equal to
  148. ** the number of phrases in the query, as returned by xPhraseCount(),
  149. ** this function returns SQLITE_RANGE.
  150. **
  151. ** If the callback function returns any value other than SQLITE_OK, the
  152. ** query is abandoned and the xQueryPhrase function returns immediately.
  153. ** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
  154. ** Otherwise, the error code is propagated upwards.
  155. **
  156. ** If the query runs to completion without incident, SQLITE_OK is returned.
  157. ** Or, if some error occurs before the query completes or is aborted by
  158. ** the callback, an SQLite error code is returned.
  159. **
  160. **
  161. ** xSetAuxdata(pFts5, pAux, xDelete)
  162. **
  163. ** Save the pointer passed as the second argument as the extension function's
  164. ** "auxiliary data". The pointer may then be retrieved by the current or any
  165. ** future invocation of the same fts5 extension function made as part of
  166. ** the same MATCH query using the xGetAuxdata() API.
  167. **
  168. ** Each extension function is allocated a single auxiliary data slot for
  169. ** each FTS query (MATCH expression). If the extension function is invoked
  170. ** more than once for a single FTS query, then all invocations share a
  171. ** single auxiliary data context.
  172. **
  173. ** If there is already an auxiliary data pointer when this function is
  174. ** invoked, then it is replaced by the new pointer. If an xDelete callback
  175. ** was specified along with the original pointer, it is invoked at this
  176. ** point.
  177. **
  178. ** The xDelete callback, if one is specified, is also invoked on the
  179. ** auxiliary data pointer after the FTS5 query has finished.
  180. **
  181. ** If an error (e.g. an OOM condition) occurs within this function,
  182. ** the auxiliary data is set to NULL and an error code returned. If the
  183. ** xDelete parameter was not NULL, it is invoked on the auxiliary data
  184. ** pointer before returning.
  185. **
  186. **
  187. ** xGetAuxdata(pFts5, bClear)
  188. **
  189. ** Returns the current auxiliary data pointer for the fts5 extension
  190. ** function. See the xSetAuxdata() method for details.
  191. **
  192. ** If the bClear argument is non-zero, then the auxiliary data is cleared
  193. ** (set to NULL) before this function returns. In this case the xDelete,
  194. ** if any, is not invoked.
  195. **
  196. **
  197. ** xRowCount(pFts5, pnRow)
  198. **
  199. ** This function is used to retrieve the total number of rows in the table.
  200. ** In other words, the same value that would be returned by:
  201. **
  202. ** SELECT count(*) FROM ftstable;
  203. **
  204. ** xPhraseFirst()
  205. ** This function is used, along with type Fts5PhraseIter and the xPhraseNext
  206. ** method, to iterate through all instances of a single query phrase within
  207. ** the current row. This is the same information as is accessible via the
  208. ** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
  209. ** to use, this API may be faster under some circumstances. To iterate
  210. ** through instances of phrase iPhrase, use the following code:
  211. **
  212. ** Fts5PhraseIter iter;
  213. ** int iCol, iOff;
  214. ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
  215. ** iCol>=0;
  216. ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
  217. ** ){
  218. ** // An instance of phrase iPhrase at offset iOff of column iCol
  219. ** }
  220. **
  221. ** The Fts5PhraseIter structure is defined above. Applications should not
  222. ** modify this structure directly - it should only be used as shown above
  223. ** with the xPhraseFirst() and xPhraseNext() API methods (and by
  224. ** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
  225. **
  226. ** This API can be quite slow if used with an FTS5 table created with the
  227. ** "detail=none" or "detail=column" option. If the FTS5 table is created
  228. ** with either "detail=none" or "detail=column" and "content=" option
  229. ** (i.e. if it is a contentless table), then this API always iterates
  230. ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
  231. **
  232. ** In all cases, matches are visited in (column ASC, offset ASC) order.
  233. ** i.e. all those in column 0, sorted by offset, followed by those in
  234. ** column 1, etc.
  235. **
  236. ** xPhraseNext()
  237. ** See xPhraseFirst above.
  238. **
  239. ** xPhraseFirstColumn()
  240. ** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
  241. ** and xPhraseNext() APIs described above. The difference is that instead
  242. ** of iterating through all instances of a phrase in the current row, these
  243. ** APIs are used to iterate through the set of columns in the current row
  244. ** that contain one or more instances of a specified phrase. For example:
  245. **
  246. ** Fts5PhraseIter iter;
  247. ** int iCol;
  248. ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
  249. ** iCol>=0;
  250. ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
  251. ** ){
  252. ** // Column iCol contains at least one instance of phrase iPhrase
  253. ** }
  254. **
  255. ** This API can be quite slow if used with an FTS5 table created with the
  256. ** "detail=none" option. If the FTS5 table is created with either
  257. ** "detail=none" "content=" option (i.e. if it is a contentless table),
  258. ** then this API always iterates through an empty set (all calls to
  259. ** xPhraseFirstColumn() set iCol to -1).
  260. **
  261. ** The information accessed using this API and its companion
  262. ** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
  263. ** (or xInst/xInstCount). The chief advantage of this API is that it is
  264. ** significantly more efficient than those alternatives when used with
  265. ** "detail=column" tables.
  266. **
  267. ** xPhraseNextColumn()
  268. ** See xPhraseFirstColumn above.
  269. **
  270. ** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
  271. ** This is used to access token iToken of phrase iPhrase of the current
  272. ** query. Before returning, output parameter *ppToken is set to point
  273. ** to a buffer containing the requested token, and *pnToken to the
  274. ** size of this buffer in bytes.
  275. **
  276. ** If iPhrase or iToken are less than zero, or if iPhrase is greater than
  277. ** or equal to the number of phrases in the query as reported by
  278. ** xPhraseCount(), or if iToken is equal to or greater than the number of
  279. ** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
  280. are both zeroed.
  281. **
  282. ** The output text is not a copy of the query text that specified the
  283. ** token. It is the output of the tokenizer module. For tokendata=1
  284. ** tables, this includes any embedded 0x00 and trailing data.
  285. **
  286. ** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
  287. ** This is used to access token iToken of phrase hit iIdx within the
  288. ** current row. If iIdx is less than zero or greater than or equal to the
  289. ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
  290. ** output variable (*ppToken) is set to point to a buffer containing the
  291. ** matching document token, and (*pnToken) to the size of that buffer in
  292. ** bytes. This API is not available if the specified token matches a
  293. ** prefix query term. In that case both output variables are always set
  294. ** to 0.
  295. **
  296. ** The output text is not a copy of the document text that was tokenized.
  297. ** It is the output of the tokenizer module. For tokendata=1 tables, this
  298. ** includes any embedded 0x00 and trailing data.
  299. **
  300. ** This API can be quite slow if used with an FTS5 table created with the
  301. ** "detail=none" or "detail=column" option.
  302. **
  303. ** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
  304. ** If parameter iCol is less than zero, or greater than or equal to the
  305. ** number of columns in the table, SQLITE_RANGE is returned.
  306. **
  307. ** Otherwise, this function attempts to retrieve the locale associated
  308. ** with column iCol of the current row. Usually, there is no associated
  309. ** locale, and output parameters (*pzLocale) and (*pnLocale) are set
  310. ** to NULL and 0, respectively. However, if the fts5_locale() function
  311. ** was used to associate a locale with the value when it was inserted
  312. ** into the fts5 table, then (*pzLocale) is set to point to a nul-terminated
  313. ** buffer containing the name of the locale in utf-8 encoding. (*pnLocale)
  314. ** is set to the size in bytes of the buffer, not including the
  315. ** nul-terminator.
  316. **
  317. ** If successful, SQLITE_OK is returned. Or, if an error occurs, an
  318. ** SQLite error code is returned. The final value of the output parameters
  319. ** is undefined in this case.
  320. **
  321. ** xTokenize_v2:
  322. ** Tokenize text using the tokenizer belonging to the FTS5 table. This
  323. ** API is the same as the xTokenize() API, except that it allows a tokenizer
  324. ** locale to be specified.
  325. */
  326. struct Fts5ExtensionApi {
  327. int iVersion; /* Currently always set to 4 */
  328. void *(*xUserData)(Fts5Context*);
  329. int (*xColumnCount)(Fts5Context*);
  330. int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
  331. int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
  332. int (*xTokenize)(Fts5Context*,
  333. const char *pText, int nText, /* Text to tokenize */
  334. void *pCtx, /* Context passed to xToken() */
  335. int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
  336. );
  337. int (*xPhraseCount)(Fts5Context*);
  338. int (*xPhraseSize)(Fts5Context*, int iPhrase);
  339. int (*xInstCount)(Fts5Context*, int *pnInst);
  340. int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
  341. sqlite3_int64 (*xRowid)(Fts5Context*);
  342. int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
  343. int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
  344. int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
  345. int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
  346. );
  347. int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
  348. void *(*xGetAuxdata)(Fts5Context*, int bClear);
  349. int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
  350. void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
  351. int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
  352. void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
  353. /* Below this point are iVersion>=3 only */
  354. int (*xQueryToken)(Fts5Context*,
  355. int iPhrase, int iToken,
  356. const char **ppToken, int *pnToken
  357. );
  358. int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*);
  359. /* Below this point are iVersion>=4 only */
  360. int (*xColumnLocale)(Fts5Context*, int iCol, const char **pz, int *pn);
  361. int (*xTokenize_v2)(Fts5Context*,
  362. const char *pText, int nText, /* Text to tokenize */
  363. const char *pLocale, int nLocale, /* Locale to pass to tokenizer */
  364. void *pCtx, /* Context passed to xToken() */
  365. int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
  366. );
  367. };
  368. /*
  369. ** CUSTOM AUXILIARY FUNCTIONS
  370. *************************************************************************/
  371. /*************************************************************************
  372. ** CUSTOM TOKENIZERS
  373. **
  374. ** Applications may also register custom tokenizer types. A tokenizer
  375. ** is registered by providing fts5 with a populated instance of the
  376. ** following structure. All structure methods must be defined, setting
  377. ** any member of the fts5_tokenizer struct to NULL leads to undefined
  378. ** behaviour. The structure methods are expected to function as follows:
  379. **
  380. ** xCreate:
  381. ** This function is used to allocate and initialize a tokenizer instance.
  382. ** A tokenizer instance is required to actually tokenize text.
  383. **
  384. ** The first argument passed to this function is a copy of the (void*)
  385. ** pointer provided by the application when the fts5_tokenizer_v2 object
  386. ** was registered with FTS5 (the third argument to xCreateTokenizer()).
  387. ** The second and third arguments are an array of nul-terminated strings
  388. ** containing the tokenizer arguments, if any, specified following the
  389. ** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
  390. ** to create the FTS5 table.
  391. **
  392. ** The final argument is an output variable. If successful, (*ppOut)
  393. ** should be set to point to the new tokenizer handle and SQLITE_OK
  394. ** returned. If an error occurs, some value other than SQLITE_OK should
  395. ** be returned. In this case, fts5 assumes that the final value of *ppOut
  396. ** is undefined.
  397. **
  398. ** xDelete:
  399. ** This function is invoked to delete a tokenizer handle previously
  400. ** allocated using xCreate(). Fts5 guarantees that this function will
  401. ** be invoked exactly once for each successful call to xCreate().
  402. **
  403. ** xTokenize:
  404. ** This function is expected to tokenize the nText byte string indicated
  405. ** by argument pText. pText may or may not be nul-terminated. The first
  406. ** argument passed to this function is a pointer to an Fts5Tokenizer object
  407. ** returned by an earlier call to xCreate().
  408. **
  409. ** The third argument indicates the reason that FTS5 is requesting
  410. ** tokenization of the supplied text. This is always one of the following
  411. ** four values:
  412. **
  413. ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
  414. ** or removed from the FTS table. The tokenizer is being invoked to
  415. ** determine the set of tokens to add to (or delete from) the
  416. ** FTS index.
  417. **
  418. ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
  419. ** against the FTS index. The tokenizer is being called to tokenize
  420. ** a bareword or quoted string specified as part of the query.
  421. **
  422. ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
  423. ** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
  424. ** followed by a "*" character, indicating that the last token
  425. ** returned by the tokenizer will be treated as a token prefix.
  426. **
  427. ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
  428. ** satisfy an fts5_api.xTokenize() request made by an auxiliary
  429. ** function. Or an fts5_api.xColumnSize() request made by the same
  430. ** on a columnsize=0 database.
  431. ** </ul>
  432. **
  433. ** The sixth and seventh arguments passed to xTokenize() - pLocale and
  434. ** nLocale - are a pointer to a buffer containing the locale to use for
  435. ** tokenization (e.g. "en_US") and its size in bytes, respectively. The
  436. ** pLocale buffer is not nul-terminated. pLocale may be passed NULL (in
  437. ** which case nLocale is always 0) to indicate that the tokenizer should
  438. ** use its default locale.
  439. **
  440. ** For each token in the input string, the supplied callback xToken() must
  441. ** be invoked. The first argument to it should be a copy of the pointer
  442. ** passed as the second argument to xTokenize(). The third and fourth
  443. ** arguments are a pointer to a buffer containing the token text, and the
  444. ** size of the token in bytes. The 4th and 5th arguments are the byte offsets
  445. ** of the first byte of and first byte immediately following the text from
  446. ** which the token is derived within the input.
  447. **
  448. ** The second argument passed to the xToken() callback ("tflags") should
  449. ** normally be set to 0. The exception is if the tokenizer supports
  450. ** synonyms. In this case see the discussion below for details.
  451. **
  452. ** FTS5 assumes the xToken() callback is invoked for each token in the
  453. ** order that they occur within the input text.
  454. **
  455. ** If an xToken() callback returns any value other than SQLITE_OK, then
  456. ** the tokenization should be abandoned and the xTokenize() method should
  457. ** immediately return a copy of the xToken() return value. Or, if the
  458. ** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
  459. ** if an error occurs with the xTokenize() implementation itself, it
  460. ** may abandon the tokenization and return any error code other than
  461. ** SQLITE_OK or SQLITE_DONE.
  462. **
  463. ** If the tokenizer is registered using an fts5_tokenizer_v2 object,
  464. ** then the xTokenize() method has two additional arguments - pLocale
  465. ** and nLocale. These specify the locale that the tokenizer should use
  466. ** for the current request. If pLocale and nLocale are both 0, then the
  467. ** tokenizer should use its default locale. Otherwise, pLocale points to
  468. ** an nLocale byte buffer containing the name of the locale to use as utf-8
  469. ** text. pLocale is not nul-terminated.
  470. **
  471. ** FTS5_TOKENIZER
  472. **
  473. ** There is also an fts5_tokenizer object. This is an older, deprecated,
  474. ** version of fts5_tokenizer_v2. It is similar except that:
  475. **
  476. ** <ul>
  477. ** <li> There is no "iVersion" field, and
  478. ** <li> The xTokenize() method does not take a locale argument.
  479. ** </ul>
  480. **
  481. ** Legacy fts5_tokenizer tokenizers must be registered using the
  482. ** legacy xCreateTokenizer() function, instead of xCreateTokenizer_v2().
  483. **
  484. ** Tokenizer implementations registered using either API may be retrieved
  485. ** using both xFindTokenizer() and xFindTokenizer_v2().
  486. **
  487. ** SYNONYM SUPPORT
  488. **
  489. ** Custom tokenizers may also support synonyms. Consider a case in which a
  490. ** user wishes to query for a phrase such as "first place". Using the
  491. ** built-in tokenizers, the FTS5 query 'first + place' will match instances
  492. ** of "first place" within the document set, but not alternative forms
  493. ** such as "1st place". In some applications, it would be better to match
  494. ** all instances of "first place" or "1st place" regardless of which form
  495. ** the user specified in the MATCH query text.
  496. **
  497. ** There are several ways to approach this in FTS5:
  498. **
  499. ** <ol><li> By mapping all synonyms to a single token. In this case, using
  500. ** the above example, this means that the tokenizer returns the
  501. ** same token for inputs "first" and "1st". Say that token is in
  502. ** fact "first", so that when the user inserts the document "I won
  503. ** 1st place" entries are added to the index for tokens "i", "won",
  504. ** "first" and "place". If the user then queries for '1st + place',
  505. ** the tokenizer substitutes "first" for "1st" and the query works
  506. ** as expected.
  507. **
  508. ** <li> By querying the index for all synonyms of each query term
  509. ** separately. In this case, when tokenizing query text, the
  510. ** tokenizer may provide multiple synonyms for a single term
  511. ** within the document. FTS5 then queries the index for each
  512. ** synonym individually. For example, faced with the query:
  513. **
  514. ** <codeblock>
  515. ** ... MATCH 'first place'</codeblock>
  516. **
  517. ** the tokenizer offers both "1st" and "first" as synonyms for the
  518. ** first token in the MATCH query and FTS5 effectively runs a query
  519. ** similar to:
  520. **
  521. ** <codeblock>
  522. ** ... MATCH '(first OR 1st) place'</codeblock>
  523. **
  524. ** except that, for the purposes of auxiliary functions, the query
  525. ** still appears to contain just two phrases - "(first OR 1st)"
  526. ** being treated as a single phrase.
  527. **
  528. ** <li> By adding multiple synonyms for a single term to the FTS index.
  529. ** Using this method, when tokenizing document text, the tokenizer
  530. ** provides multiple synonyms for each token. So that when a
  531. ** document such as "I won first place" is tokenized, entries are
  532. ** added to the FTS index for "i", "won", "first", "1st" and
  533. ** "place".
  534. **
  535. ** This way, even if the tokenizer does not provide synonyms
  536. ** when tokenizing query text (it should not - to do so would be
  537. ** inefficient), it doesn't matter if the user queries for
  538. ** 'first + place' or '1st + place', as there are entries in the
  539. ** FTS index corresponding to both forms of the first token.
  540. ** </ol>
  541. **
  542. ** Whether it is parsing document or query text, any call to xToken that
  543. ** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
  544. ** is considered to supply a synonym for the previous token. For example,
  545. ** when parsing the document "I won first place", a tokenizer that supports
  546. ** synonyms would call xToken() 5 times, as follows:
  547. **
  548. ** <codeblock>
  549. ** xToken(pCtx, 0, "i", 1, 0, 1);
  550. ** xToken(pCtx, 0, "won", 3, 2, 5);
  551. ** xToken(pCtx, 0, "first", 5, 6, 11);
  552. ** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
  553. ** xToken(pCtx, 0, "place", 5, 12, 17);
  554. **</codeblock>
  555. **
  556. ** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
  557. ** xToken() is called. Multiple synonyms may be specified for a single token
  558. ** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
  559. ** There is no limit to the number of synonyms that may be provided for a
  560. ** single token.
  561. **
  562. ** In many cases, method (1) above is the best approach. It does not add
  563. ** extra data to the FTS index or require FTS5 to query for multiple terms,
  564. ** so it is efficient in terms of disk space and query speed. However, it
  565. ** does not support prefix queries very well. If, as suggested above, the
  566. ** token "first" is substituted for "1st" by the tokenizer, then the query:
  567. **
  568. ** <codeblock>
  569. ** ... MATCH '1s*'</codeblock>
  570. **
  571. ** will not match documents that contain the token "1st" (as the tokenizer
  572. ** will probably not map "1s" to any prefix of "first").
  573. **
  574. ** For full prefix support, method (3) may be preferred. In this case,
  575. ** because the index contains entries for both "first" and "1st", prefix
  576. ** queries such as 'fi*' or '1s*' will match correctly. However, because
  577. ** extra entries are added to the FTS index, this method uses more space
  578. ** within the database.
  579. **
  580. ** Method (2) offers a midpoint between (1) and (3). Using this method,
  581. ** a query such as '1s*' will match documents that contain the literal
  582. ** token "1st", but not "first" (assuming the tokenizer is not able to
  583. ** provide synonyms for prefixes). However, a non-prefix query like '1st'
  584. ** will match against "1st" and "first". This method does not require
  585. ** extra disk space, as no extra entries are added to the FTS index.
  586. ** On the other hand, it may require more CPU cycles to run MATCH queries,
  587. ** as separate queries of the FTS index are required for each synonym.
  588. **
  589. ** When using methods (2) or (3), it is important that the tokenizer only
  590. ** provide synonyms when tokenizing document text (method (3)) or query
  591. ** text (method (2)), not both. Doing so will not cause any errors, but is
  592. ** inefficient.
  593. */
  594. typedef struct Fts5Tokenizer Fts5Tokenizer;
  595. typedef struct fts5_tokenizer_v2 fts5_tokenizer_v2;
  596. struct fts5_tokenizer_v2 {
  597. int iVersion; /* Currently always 2 */
  598. int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
  599. void (*xDelete)(Fts5Tokenizer*);
  600. int (*xTokenize)(Fts5Tokenizer*,
  601. void *pCtx,
  602. int flags, /* Mask of FTS5_TOKENIZE_* flags */
  603. const char *pText, int nText,
  604. const char *pLocale, int nLocale,
  605. int (*xToken)(
  606. void *pCtx, /* Copy of 2nd argument to xTokenize() */
  607. int tflags, /* Mask of FTS5_TOKEN_* flags */
  608. const char *pToken, /* Pointer to buffer containing token */
  609. int nToken, /* Size of token in bytes */
  610. int iStart, /* Byte offset of token within input text */
  611. int iEnd /* Byte offset of end of token within input text */
  612. )
  613. );
  614. };
  615. /*
  616. ** New code should use the fts5_tokenizer_v2 type to define tokenizer
  617. ** implementations. The following type is included for legacy applications
  618. ** that still use it.
  619. */
  620. typedef struct fts5_tokenizer fts5_tokenizer;
  621. struct fts5_tokenizer {
  622. int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
  623. void (*xDelete)(Fts5Tokenizer*);
  624. int (*xTokenize)(Fts5Tokenizer*,
  625. void *pCtx,
  626. int flags, /* Mask of FTS5_TOKENIZE_* flags */
  627. const char *pText, int nText,
  628. int (*xToken)(
  629. void *pCtx, /* Copy of 2nd argument to xTokenize() */
  630. int tflags, /* Mask of FTS5_TOKEN_* flags */
  631. const char *pToken, /* Pointer to buffer containing token */
  632. int nToken, /* Size of token in bytes */
  633. int iStart, /* Byte offset of token within input text */
  634. int iEnd /* Byte offset of end of token within input text */
  635. )
  636. );
  637. };
  638. /* Flags that may be passed as the third argument to xTokenize() */
  639. #define FTS5_TOKENIZE_QUERY 0x0001
  640. #define FTS5_TOKENIZE_PREFIX 0x0002
  641. #define FTS5_TOKENIZE_DOCUMENT 0x0004
  642. #define FTS5_TOKENIZE_AUX 0x0008
  643. /* Flags that may be passed by the tokenizer implementation back to FTS5
  644. ** as the third argument to the supplied xToken callback. */
  645. #define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
  646. /*
  647. ** END OF CUSTOM TOKENIZERS
  648. *************************************************************************/
  649. /*************************************************************************
  650. ** FTS5 EXTENSION REGISTRATION API
  651. */
  652. typedef struct fts5_api fts5_api;
  653. struct fts5_api {
  654. int iVersion; /* Currently always set to 3 */
  655. /* Create a new tokenizer */
  656. int (*xCreateTokenizer)(
  657. fts5_api *pApi,
  658. const char *zName,
  659. void *pUserData,
  660. fts5_tokenizer *pTokenizer,
  661. void (*xDestroy)(void*)
  662. );
  663. /* Find an existing tokenizer */
  664. int (*xFindTokenizer)(
  665. fts5_api *pApi,
  666. const char *zName,
  667. void **ppUserData,
  668. fts5_tokenizer *pTokenizer
  669. );
  670. /* Create a new auxiliary function */
  671. int (*xCreateFunction)(
  672. fts5_api *pApi,
  673. const char *zName,
  674. void *pUserData,
  675. fts5_extension_function xFunction,
  676. void (*xDestroy)(void*)
  677. );
  678. /* APIs below this point are only available if iVersion>=3 */
  679. /* Create a new tokenizer */
  680. int (*xCreateTokenizer_v2)(
  681. fts5_api *pApi,
  682. const char *zName,
  683. void *pUserData,
  684. fts5_tokenizer_v2 *pTokenizer,
  685. void (*xDestroy)(void*)
  686. );
  687. /* Find an existing tokenizer */
  688. int (*xFindTokenizer_v2)(
  689. fts5_api *pApi,
  690. const char *zName,
  691. void **ppUserData,
  692. fts5_tokenizer_v2 **ppTokenizer
  693. );
  694. };
  695. /*
  696. ** END OF REGISTRATION API
  697. *************************************************************************/
  698. #ifdef __cplusplus
  699. } /* end of the 'extern "C"' block */
  700. #endif
  701. #endif /* _FTS5_H */