Clone of PhatAC @ https://github.com/floaterxk/PhatAC

plugin_ftparser.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  12. #ifndef _my_plugin_ftparser_h
  13. #define _my_plugin_ftparser_h
  14. #include "plugin.h"
  15. /*************************************************************************
  16. API for Full-text parser plugin. (MYSQL_FTPARSER_PLUGIN)
  17. */
  18. /* Parsing modes. Set in MYSQL_FTPARSER_PARAM::mode */
  19. enum enum_ftparser_mode
  20. {
  21. /*
  22. Fast and simple mode. This mode is used for indexing, and natural
  23. language queries.
  24. The parser is expected to return only those words that go into the
  25. index. Stopwords or too short/long words should not be returned. The
  26. 'boolean_info' argument of mysql_add_word() does not have to be set.
  27. */
  28. MYSQL_FTPARSER_SIMPLE_MODE= 0,
  29. /*
  30. Parse with stopwords mode. This mode is used in boolean searches for
  31. "phrase matching."
  32. The parser is not allowed to ignore words in this mode. Every word
  33. should be returned, including stopwords and words that are too short
  34. or long. The 'boolean_info' argument of mysql_add_word() does not
  35. have to be set.
  36. */
  37. MYSQL_FTPARSER_WITH_STOPWORDS= 1,
  38. /*
  39. Parse in boolean mode. This mode is used to parse a boolean query string.
  40. The parser should provide a valid MYSQL_FTPARSER_BOOLEAN_INFO
  41. structure in the 'boolean_info' argument to mysql_add_word().
  42. Usually that means that the parser should recognize boolean operators
  43. in the parsing stream and set appropriate fields in
  44. MYSQL_FTPARSER_BOOLEAN_INFO structure accordingly. As for
  45. MYSQL_FTPARSER_WITH_STOPWORDS mode, no word should be ignored.
  46. Instead, use FT_TOKEN_STOPWORD for the token type of such a word.
  47. */
  48. MYSQL_FTPARSER_FULL_BOOLEAN_INFO= 2
  49. };
  50. /*
  51. Token types for boolean mode searching (used for the type member of
  52. MYSQL_FTPARSER_BOOLEAN_INFO struct)
  53. FT_TOKEN_EOF: End of data.
  54. FT_TOKEN_WORD: Regular word.
  55. FT_TOKEN_LEFT_PAREN: Left parenthesis (start of group/sub-expression).
  56. FT_TOKEN_RIGHT_PAREN: Right parenthesis (end of group/sub-expression).
  57. FT_TOKEN_STOPWORD: Stopword.
  58. */
  59. enum enum_ft_token_type
  60. {
  61. FT_TOKEN_EOF= 0,
  62. FT_TOKEN_WORD= 1,
  63. FT_TOKEN_LEFT_PAREN= 2,
  64. FT_TOKEN_RIGHT_PAREN= 3,
  65. FT_TOKEN_STOPWORD= 4
  66. };
  67. /*
  68. This structure is used in boolean search mode only. It conveys
  69. boolean-mode metadata to the MySQL search engine for every word in
  70. the search query. A valid instance of this structure must be filled
  71. in by the plugin parser and passed as an argument in the call to
  72. mysql_add_word (the callback function in the MYSQL_FTPARSER_PARAM
  73. structure) when a query is parsed in boolean mode.
  74. type: The token type. Should be one of the enum_ft_token_type values.
  75. yesno: Whether the word must be present for a match to occur:
  76. >0 Must be present
  77. <0 Must not be present
  78. 0 Neither; the word is optional but its presence increases the relevance
  79. With the default settings of the ft_boolean_syntax system variable,
  80. >0 corresponds to the '+' operator, <0 corrresponds to the '-' operator,
  81. and 0 means neither operator was used.
  82. weight_adjust: A weighting factor that determines how much a match
  83. for the word counts. Positive values increase, negative - decrease the
  84. relative word's importance in the query.
  85. wasign: The sign of the word's weight in the query. If it's non-negative
  86. the match for the word will increase document relevance, if it's
  87. negative - decrease (the word becomes a "noise word", the less of it the
  88. better).
  89. trunc: Corresponds to the '*' operator in the default setting of the
  90. ft_boolean_syntax system variable.
  91. position: Start position in bytes of the word in the document, used by InnoDB FTS.
  92. */
  93. typedef struct st_mysql_ftparser_boolean_info
  94. {
  95. enum enum_ft_token_type type;
  96. int yesno;
  97. int weight_adjust;
  98. char wasign;
  99. char trunc;
  100. int position;
  101. /* These are parser state and must be removed. */
  102. char prev;
  103. char *quot;
  104. } MYSQL_FTPARSER_BOOLEAN_INFO;
  105. /*
  106. The following flag means that buffer with a string (document, word)
  107. may be overwritten by the caller before the end of the parsing (that is
  108. before st_mysql_ftparser::deinit() call). If one needs the string
  109. to survive between two successive calls of the parsing function, she
  110. needs to save a copy of it. The flag may be set by MySQL before calling
  111. st_mysql_ftparser::parse(), or it may be set by a plugin before calling
  112. st_mysql_ftparser_param::mysql_parse() or
  113. st_mysql_ftparser_param::mysql_add_word().
  114. */
  115. #define MYSQL_FTFLAGS_NEED_COPY 1
  116. /*
  117. An argument of the full-text parser plugin. This structure is
  118. filled in by MySQL server and passed to the parsing function of the
  119. plugin as an in/out parameter.
  120. mysql_parse: A pointer to the built-in parser implementation of the
  121. server. It's set by the server and can be used by the parser plugin
  122. to invoke the MySQL default parser. If plugin's role is to extract
  123. textual data from .doc, .pdf or .xml content, it might extract
  124. plaintext from the content, and then pass the text to the default
  125. MySQL parser to be parsed.
  126. mysql_add_word: A server callback to add a new word. When parsing
  127. a document, the server sets this to point at a function that adds
  128. the word to MySQL full-text index. When parsing a search query,
  129. this function will add the new word to the list of words to search
  130. for. The boolean_info argument can be NULL for all cases except
  131. when mode is MYSQL_FTPARSER_FULL_BOOLEAN_INFO.
  132. ftparser_state: A generic pointer. The plugin can set it to point
  133. to information to be used internally for its own purposes.
  134. mysql_ftparam: This is set by the server. It is used by MySQL functions
  135. called via mysql_parse() and mysql_add_word() callback. The plugin
  136. should not modify it.
  137. cs: Information about the character set of the document or query string.
  138. doc: A pointer to the document or query string to be parsed.
  139. length: Length of the document or query string, in bytes.
  140. flags: See MYSQL_FTFLAGS_* constants above.
  141. mode: The parsing mode. With boolean operators, with stopwords, or
  142. nothing. See enum_ftparser_mode above.
  143. */
  144. typedef struct st_mysql_ftparser_param
  145. {
  146. int (*mysql_parse)(struct st_mysql_ftparser_param *,
  147. char *doc, int doc_len);
  148. int (*mysql_add_word)(struct st_mysql_ftparser_param *,
  149. char *word, int word_len,
  150. MYSQL_FTPARSER_BOOLEAN_INFO *boolean_info);
  151. void *ftparser_state;
  152. void *mysql_ftparam;
  153. const struct charset_info_st *cs;
  154. char *doc;
  155. int length;
  156. int flags;
  157. enum enum_ftparser_mode mode;
  158. } MYSQL_FTPARSER_PARAM;
  159. /*
  160. Full-text parser descriptor.
  161. interface_version is, e.g., MYSQL_FTPARSER_INTERFACE_VERSION.
  162. The parsing, initialization, and deinitialization functions are
  163. invoked per SQL statement for which the parser is used.
  164. */
  165. struct st_mysql_ftparser
  166. {
  167. int interface_version;
  168. int (*parse)(MYSQL_FTPARSER_PARAM *param);
  169. int (*init)(MYSQL_FTPARSER_PARAM *param);
  170. int (*deinit)(MYSQL_FTPARSER_PARAM *param);
  171. };
  172. #endif