MySQL 的全文搜索功能具有少量可调参数。如果您拥有 MySQL 源代码分发版,您可以对全文搜索行为进行更多控制,因为某些更改需要修改源代码。请参阅 第 2.8 节,“从源代码安装 MySQL”。MySQL's full-text search capability has few user-tunable parameters. You can exert more control over full-text searching behavior if you have a MySQL source distribution because some changes require source code modifications. See Section 2.8, “Installing MySQL from Source”.
全文搜索经过精心调优以提高效果。在大多数情况下,修改默认行为实际上可能降低效果。除非您知道自己在做什么,否则不要修改 MySQL 源代码。Full-text search is carefully tuned for effectiveness. Modifying the default behavior in most cases can actually decrease effectiveness. Do not alter the MySQL sources unless you know what you are doing.
本节描述的大多数全文变量必须在服务器启动时设置。更改它们需要重启服务器;在服务器运行时无法修改它们。Most full-text variables described in this section must be set at server startup time. A server restart is required to change them; they cannot be modified while the server is running.
某些变量的更改要求您重新构建表中的 FULLTEXT 索引。本节后面会给出执行此操作的说明。Some variable changes require that you rebuild the
FULLTEXT indexes in your tables. Instructions
for doing so are given later in this section.
要索引的单词的最小长度和最大长度由 innodb_ft_min_token_size 和 innodb_ft_max_token_size 定义用于 InnoDB 搜索索引,而 ft_min_word_len 和 ft_max_word_len 则用于 MyISAM 索引。The minimum and maximum lengths of words to be indexed are
defined by the
innodb_ft_min_token_size and
innodb_ft_max_token_size for
InnoDB search indexes, and
ft_min_word_len and
ft_max_word_len for
MyISAM ones.
最小和最大词长的全文参数不适用于使用 ngram 解析器创建的 FULLTEXT 索引。ngram 令牌大小由 ngram_token_size 选项定义。Minimum and maximum word length full-text parameters do not
apply to FULLTEXT indexes created using
the ngram parser. ngram token size is defined by the
ngram_token_size option.
更改这些选项中的任何一个后,需要重建您的 FULLTEXT 索引以使更改生效。例如,要使两字符的单词可搜索,可以将以下行放入选项文件中:After changing any of these options, rebuild your
FULLTEXT indexes for the change to take
effect. For example, to make two-character words searchable,
you could put the following lines in an option file:
[mysqld] innodb_ft_min_token_size=2 ft_min_word_len=2
然后重启服务器并重建你的 FULLTEXT 索引。对于 MyISAM 表,请注意在下面关于 myisamchk 的说明中关于重建 MyISAM 全文索引的备注。Then restart the server and rebuild your
FULLTEXT indexes. For
MyISAM tables, note the remarks regarding
myisamchk in the instructions that follow
for rebuilding MyISAM full-text indexes.
对于 MyISAM 搜索索引,自然语言搜索的 50% 阈值是由所选的特定加权方案决定的。要禁用它,请在 storage/myisam/ftdefs.h 中查找以下行:For MyISAM search indexes, the 50%
threshold for natural language searches is determined by the
particular weighting scheme chosen. To disable it, look for
the following line in
storage/myisam/ftdefs.h:
#define GWS_IN_USE GWS_PROB
将那一行改成这个:Change that line to this:
#define GWS_IN_USE GWS_FREQ
然后重新编译 MySQL。在这种情况下无需重建索引。Then recompile MySQL. There is no need to rebuild the indexes in this case.
通过进行此更改,您会严重降低 MySQL 为 MATCH() 函数提供适当相关性值的能力。如果您确实需要搜索这些常见词,最好改为使用 IN
BOOLEAN MODE 进行搜索,因为它不遵循 50% 的阈值。By making this change, you severely
decrease MySQL's ability to provide adequate relevance
values for the MATCH()
function. If you really need to search for such common
words, it would be better to search using IN
BOOLEAN MODE instead, which does not observe the
50% threshold.
要更改用于MyISAM表的布尔全文搜索的运算符,请设置ft_boolean_syntax系统变量。(InnoDB没有相应的设置。)此变量可以在服务器运行时更改,但您必须拥有设置全局系统变量的足够权限(参见第 7.1.9.1 节,“系统变量权限”)。在这种情况下无需重建索引。To change the operators used for boolean full-text searches on
MyISAM tables, set the
ft_boolean_syntax system
variable. (InnoDB does not have an
equivalent setting.) This variable can be changed while the
server is running, but you must have privileges sufficient to
set global system variables (see
Section 7.1.9.1, “System Variable Privileges”). No rebuilding
of indexes is necessary in this case.
对于内置的全文解析器,你可以通过几种方式更改被视为单词字符的字符集,如下列清单所述。修改后,需要重新构建包含任何 FULLTEXT 索引的每个表的索引。假设你想将连字符('-')视为单词字符,请使用以下方法之一:For the built-in full-text parser, you can change the set of
characters that are considered word characters in several
ways, as described in the following list. After making the
modification, rebuild the indexes for each table that contains
any FULLTEXT indexes. Suppose that you want
to treat the hyphen character ('-') as a word character. Use
one of these methods:
修改 MySQL 源代码:在
storage/innobase/handler/ha_innodb.cc(针对InnoDB)或storage/myisam/ftdefs.h(针对MyISAM)中,查看true_word_char()和misc_word_char()宏。在这些宏之一中添加'-'并重新编译 MySQL。Modify the MySQL source: Instorage/innobase/handler/ha_innodb.cc(forInnoDB), or instorage/myisam/ftdefs.h(forMyISAM), see thetrue_word_char()andmisc_word_char()macros. Add'-'to one of those macros and recompile MySQL.修改字符集文件:这不需要重新编译。
true_word_char()宏使用一个 “字符类型” 表来区分字母和数字与其他字符。你可以编辑某个字符集 XML 文件中<ctype><map>数组的内容,将'-'指定为 “字母”。然后为你的FULLTEXT索引使用该字符集。有关<ctype><map>数组格式的信息,请参阅 第 12.13.1 节,“字符定义数组”。Modify a character set file: This requires no recompilation. Thetrue_word_char()macro uses a “character type” table to distinguish letters and numbers from other characters. . You can edit the contents of the<ctype><map>array in one of the character set XML files to specify that'-'is a “letter.” Then use the given character set for yourFULLTEXTindexes. For information about the<ctype><map>array format, see Section 12.13.1, “Character Definition Arrays”.为索引列使用的字符集添加一个新的校对规则,并更改这些列以使用该校对规则。有关添加校对规则的一般信息,请参见 第 12.14 节,“将校对规则添加到字符集”。有关全文索引特定示例,请参见 第 14.9.7 节,“为全文索引添加用户定义的校对规则”。Add a new collation for the character set used by the indexed columns, and alter the columns to use that collation. For general information about adding collations, see Section 12.14, “Adding a Collation to a Character Set”. For an example specific to full-text indexing, see Section 14.9.7, “Adding a User-Defined Collation for Full-Text Indexing”.
要使更改生效,在修改以下任何全文索引变量后,必须重建 FULLTEXT 索引:innodb_ft_min_token_size;innodb_ft_max_token_size;innodb_ft_server_stopword_table;innodb_ft_user_stopword_table;innodb_ft_enable_stopword;ngram_token_size。修改 innodb_ft_min_token_size、innodb_ft_max_token_size 或 ngram_token_size 需要重启服务器。For the changes to take effect, FULLTEXT
indexes must be rebuilt after modifying any of the following
full-text index variables:
innodb_ft_min_token_size;
innodb_ft_max_token_size;
innodb_ft_server_stopword_table;
innodb_ft_user_stopword_table;
innodb_ft_enable_stopword;
ngram_token_size. Modifying
innodb_ft_min_token_size,
innodb_ft_max_token_size, or
ngram_token_size requires
restarting the server.
要重建 FULLTEXT 索引的 InnoDB 表,请使用带有 DROP INDEX 和 ADD INDEX 选项的 ALTER TABLE 来删除并重新创建每个索引。To rebuild FULLTEXT indexes for an
InnoDB table, use
ALTER TABLE with the
DROP INDEX and ADD INDEX
options to drop and re-create each index.
在具有全文索引的表上运行 OPTIMIZE TABLE 会重建全文索引,移除已删除的文档 ID,并尽可能合并同一单词的多个条目。Running OPTIMIZE TABLE on a
table with a full-text index rebuilds the full-text index,
removing deleted Document IDs and consolidating multiple
entries for the same word, where possible.
要优化全文本索引,请启用 innodb_optimize_fulltext_only 并运行 OPTIMIZE TABLE。To optimize a full-text index, enable
innodb_optimize_fulltext_only
and run OPTIMIZE TABLE.
mysql> set GLOBAL innodb_optimize_fulltext_only=ON; Query OK, 0 rows affected (0.01 sec) mysql> OPTIMIZE TABLE opening_lines; +--------------------+----------+----------+----------+ | Table | Op | Msg_type | Msg_text | +--------------------+----------+----------+----------+ | test.opening_lines | optimize | status | OK | +--------------------+----------+----------+----------+ 1 row in set (0.01 sec)
为了避免在大表上对全文索引进行完全重建时耗时过长,可以使用 innodb_ft_num_word_optimize 选项分阶段执行优化。innodb_ft_num_word_optimize 选项定义了每次运行 OPTIMIZE TABLE 时优化的单词数量。默认设置为 2000,这意味着每次运行 OPTIMIZE TABLE 时会优化 2000 个单词。后续的 OPTIMIZE TABLE 操作会从前一次 OPTIMIZE
TABLE 操作结束的地方继续执行。To avoid lengthy rebuild times for full-text indexes on large
tables, you can use the
innodb_ft_num_word_optimize
option to perform the optimization in stages. The
innodb_ft_num_word_optimize option defines
the number of words that are optimized each time
OPTIMIZE TABLE is run. The
default setting is 2000, which means that 2000 words are
optimized each time OPTIMIZE
TABLE is run. Subsequent
OPTIMIZE TABLE operations
continue from where the preceding
OPTIMIZE TABLE operation ended.
如果你修改影响索引的全文变量(ft_min_word_len、ft_max_word_len 或 ft_stopword_file),或者如果你更改了停止词文件本身,那么在修改并重启服务器后,必须重建你的 FULLTEXT 索引。If you modify full-text variables that affect indexing
(ft_min_word_len,
ft_max_word_len, or
ft_stopword_file), or if you
change the stopword file itself, you must rebuild your
FULLTEXT indexes after making the changes
and restarting the server.
要为 MyISAM 表重建 FULLTEXT 索引,仅需执行一次 QUICK 修复操作即可:To rebuild the FULLTEXT indexes for a
MyISAM table, it is sufficient to do a
QUICK repair operation:
mysql> REPAIR TABLE tbl_name QUICK;
或者,如上所述使用 ALTER TABLE。在某些情况下,这可能比修复操作更快。Alternatively, use ALTER TABLE
as just described. In some cases, this may be faster than a
repair operation.
每个包含任何 FULLTEXT 索引的表都必须按照上述方法进行修复。否则,对表的查询可能会产生不正确的结果,并且对表的修改会导致服务器认为该表已损坏,需要修复。Each table that contains any FULLTEXT index
must be repaired as just shown. Otherwise, queries for the
table may yield incorrect results, and modifications to the
table causes the server to see the table as corrupt and in
need of repair.
If you use myisamchk to perform an
operation that modifies MyISAM table
indexes (such as repair or analyze), the
FULLTEXT indexes are rebuilt using the
默认 full-text parameter values for
minimum word length, maximum word length, and stopword file
unless you specify otherwise. This can result in queries
failing.If you use myisamchk to perform an
operation that modifies MyISAM table
indexes (such as repair or analyze), the
FULLTEXT indexes are rebuilt using the
default full-text parameter values for
minimum word length, maximum word length, and stopword file
unless you specify otherwise. This can result in queries
failing.
这个问题的发生是因为这些参数只有服务器知道。它们没有存储在MyISAM索引文件中。为了避免这个问题,如果你修改了服务器使用的最小或最大词长或停用词文件值,请为你用于myisamchk的mysqld指定相同的ft_min_word_len、ft_max_word_len和ft_stopword_file值。例如,如果你将最小词长设置为 3,你可以这样使用myisamchk来修复表:The problem occurs because these parameters are known only by
the server. They are not stored in MyISAM
index files. To avoid the problem if you have modified the
minimum or maximum word length or stopword file values used by
the server, specify the same
ft_min_word_len,
ft_max_word_len, and
ft_stopword_file values for
myisamchk that you use for
mysqld. For example, if you have set the
minimum word length to 3, you can repair a table with
myisamchk like this:
myisamchk --recover --ft_min_word_len=3 tbl_name.MYI
为了确保 myisamchk 和服务器使用相同的全文本参数值,请将每个参数同时放在选项文件的 [mysqld] 和 [myisamchk] 部分:To ensure that myisamchk and the server use
the same values for full-text parameters, place each one in
both the [mysqld] and
[myisamchk] sections of an option file:
[mysqld] ft_min_word_len=3 [myisamchk] ft_min_word_len=3
使用myisamchk for
MyISAM table index modification is to use
the REPAIR TABLE,
ANALYZE TABLE,
OPTIMIZE TABLE,或ALTER TABLE statements. These
statements are performed by the server, which knows the proper
full-text parameter values to use.An alternative to using myisamchk for
MyISAM table index modification is to use
the REPAIR TABLE,
ANALYZE TABLE,
OPTIMIZE TABLE, or
ALTER TABLE statements. These
statements are performed by the server, which knows the proper
full-text parameter values to use.