- 14.9.1 自然语言全文搜索14.9.1 Natural Language Full-Text Searches
- 14.9.2 布尔全文搜索14.9.2 Boolean Full-Text Searches
- 14.9.3 带查询扩展的全文搜索14.9.3 Full-Text Searches with Query Expansion
- 14.9.4 全文停用词14.9.4 Full-Text Stopwords
- 14.9.5 全文限制14.9.5 Full-Text Restrictions
- 14.9.6 微调 MySQL 全文搜索14.9.6 Fine-Tuning MySQL Full-Text Search
- 14.9.7 为全文索引添加用户定义的排序规则14.9.7 Adding a User-Defined Collation for Full-Text Indexing
- 14.9.8 ngram 全文解析器14.9.8 ngram Full-Text Parser
- 14.9.9 MeCab 全文解析器插件14.9.9 MeCab Full-Text Parser Plugin
MATCH
(
col1,col2,...)
AGAINST (expr
[search_modifier])
search_modifier:
{
IN NATURAL LANGUAGE MODE
| IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
| IN BOOLEAN MODE
| WITH QUERY EXPANSION
}
MySQL 支持全文索引和搜索:MySQL has support for full-text indexing and searching:
MySQL 中的全文索引是一种类型为
FULLTEXT的索引。A full-text index in MySQL is an index of typeFULLTEXT.全文索引只能用于
InnoDB或MyISAM表,并且只能为CHAR、VARCHAR或TEXT列创建。Full-text indexes can be used only withInnoDBorMyISAMtables, and can be created only forCHAR,VARCHAR, orTEXTcolumns.MySQL 提供了一个内置的全文本 ngram 解析器,支持中文、日文和韩文(CJK),以及一个可安装的用于日文的 MeCab 全文本解析器插件。解析差异概述见 第 14.9.8 节,“ngram 全文本解析器”,以及 第 14.9.9 节,“MeCab 全文本解析器插件”。MySQL provides a built-in full-text ngram parser that supports Chinese, Japanese, and Korean (CJK), and an installable MeCab full-text parser plugin for Japanese. Parsing differences are outlined in Section 14.9.8, “ngram Full-Text Parser”, and Section 14.9.9, “MeCab Full-Text Parser Plugin”.
在创建表时,可以在
CREATE TABLE语句中给出FULLTEXT索引定义,也可以稍后使用ALTER TABLE或CREATE INDEX添加。AFULLTEXTindex definition can be given in theCREATE TABLEstatement when a table is created, or added later usingALTER TABLEorCREATE INDEX.对于大型数据集,将数据加载到没有
FULLTEXT索引的表中,然后再创建索引,比将数据加载到已有FULLTEXT索引的表中要快得多。For large data sets, it is much faster to load your data into a table that has noFULLTEXTindex and then create the index after that, than to load data into a table that has an existingFULLTEXTindex.
全文搜索是使用 MATCH() AGAINST() 语法执行的。MATCH() 接受一个逗号分隔的列表,列出要搜索的列。AGAINST 接受一个要搜索的字符串,以及一个可选的修饰符,用于指示执行哪种类型的搜索。搜索字符串必须是在查询评估期间保持不变的字符串值。这排除了例如表列,因为它可以因每一行而不同。Full-text searching is performed using
MATCH() AGAINST() syntax.
MATCH() takes a comma-separated
list that names the columns to be searched.
AGAINST takes a string to search for, and an
optional modifier that indicates what type of search to perform.
The search string must be a string value that is constant during
query evaluation. This rules out, for example, a table column
because that can differ for each row.
MySQL 不允许在 MATCH() 中使用汇总列;更具体地说,任何匹配此处列出的所有条件的查询都会被拒绝,并返回 ER_FULLTEXT_WITH_ROLLUP 错误:MySQL does not permit the use of a rollup column with
MATCH(); more specifically, any query matching
all of the criteria listed here is rejected with
ER_FULLTEXT_WITH_ROLLUP:
MATCH()出现在查询块的SELECT列表、GROUP BY子句、HAVING子句或ORDER BY子句中。MATCH()appears in theSELECTlist,GROUP BYclause,HAVINGclause, orORDER BYclause of a query block.查询块中包含
GROUP BY ... WITH ROLLUP子句。The query block contains aGROUP BY ... WITH ROLLUPclause.对
MATCH()函数的调用参数是分组列之一。The argument of the call to theMATCH()function is one of the grouping columns.
这里显示了一些此类查询的示例:Some examples of such queries are shown here:
# MATCH() in SELECT list...
SELECT MATCH (a) AGAINST ('abc') FROM t GROUP BY a WITH ROLLUP;
SELECT 1 FROM t GROUP BY a, MATCH (a) AGAINST ('abc') WITH ROLLUP;
# ...in HAVING clause...
SELECT 1 FROM t GROUP BY a WITH ROLLUP HAVING MATCH (a) AGAINST ('abc');
# ...and in ORDER BY clause
SELECT 1 FROM t GROUP BY a WITH ROLLUP ORDER BY MATCH (a) AGAINST ('abc');
在 WHERE 子句中允许将 MATCH() 用于汇总列。The use of MATCH() with a rollup column in the
WHERE clause is permitted.
全文搜索有三种类型:There are three types of full-text searches:
自然语言搜索将搜索字符串解释为自然人类语言中的短语(自由文本中的短语)。没有特殊操作符,唯一的例外是双引号 (") 字符。停用词列表适用。有关停用词列表的更多信息,请参阅 第 14.9.4 节,“全文停用词”。A natural language search interprets the search string as a phrase in natural human language (a phrase in free text). There are no special operators, with the exception of double quote (") characters. The stopword list applies. For more information about stopword lists, see Section 14.9.4, “Full-Text Stopwords”.
如果给出了
IN NATURAL LANGUAGE MODE修饰符,或者没有给出修饰符,全文搜索就是自然语言搜索。更多信息,请参见 第 14.9.1 节,“自然语言全文搜索”。Full-text searches are natural language searches if theIN NATURAL LANGUAGE MODEmodifier is given or if no modifier is given. For more information, see Section 14.9.1, “Natural Language Full-Text Searches”.布尔搜索使用特殊查询语言的规则来解释搜索字符串。字符串包含要搜索的单词。它还可以包含指定要求的运算符,例如某个单词必须存在或不存在于匹配行中,或者它的权重应该比通常更高或更低。某些常见词(停用词)会从搜索索引中省略,如果出现在搜索字符串中,它们不会匹配。
IN BOOLEAN MODE修饰符指定布尔搜索。更多信息,请参见 第 14.9.2 节,“布尔全文搜索”。A boolean search interprets the search string using the rules of a special query language. The string contains the words to search for. It can also contain operators that specify requirements such that a word must be present or absent in matching rows, or that it should be weighted higher or lower than usual. Certain common words (stopwords) are omitted from the search index and do not match if present in the search string. TheIN BOOLEAN MODEmodifier specifies a boolean search. For more information, see Section 14.9.2, “Boolean Full-Text Searches”.查询扩展搜索是对自然语言搜索的一种修改。搜索字符串用于执行自然语言搜索。然后,将搜索返回的最相关行中的词添加到搜索字符串中,并再次进行搜索。查询返回第二次搜索的行。
IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION或WITH QUERY EXPANSION修饰符指定查询扩展搜索。有关更多信息,请参见 第 14.9.3 节,“全文搜索与查询扩展”。A query expansion search is a modification of a natural language search. The search string is used to perform a natural language search. Then words from the most relevant rows returned by the search are added to the search string and the search is done again. The query returns the rows from the second search. TheIN NATURAL LANGUAGE MODE WITH QUERY EXPANSIONorWITH QUERY EXPANSIONmodifier specifies a query expansion search. For more information, see Section 14.9.3, “Full-Text Searches with Query Expansion”.
关于 FULLTEXT 查询性能的信息,请参见 第 10.3.5 节,“列索引”。For information about FULLTEXT query
performance, see Section 10.3.5, “Column Indexes”.
关于 InnoDB FULLTEXT 索引的更多信息,请参见 第 17.6.2.4 节,“InnoDB 全文索引”。For more information about InnoDB
FULLTEXT indexes, see
Section 17.6.2.4, “InnoDB Full-Text Indexes”.
全文搜索的限制列在 第 14.9.5 节,“全文限制”。Constraints on full-text searching are listed in Section 14.9.5, “Full-Text Restrictions”.
myisam_ftdump 工具会导出 MyISAM 全文索引的内容。这对于调试全文查询可能会有帮助。参见 第 6.6.3 节,“myisam_ftdump — 显示全文索引信息”。The myisam_ftdump utility dumps the contents of
a MyISAM full-text index. This may be helpful
for debugging full-text queries. See
Section 6.6.3, “myisam_ftdump — Display Full-Text Index information”.