扩展 MySQL 8.0  / 第 6 章 向 MySQL 添加函数  /  12.10 全文搜索功能

12.10 全文搜索功能

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 中的全文索引是 类型的索引 FULLTEXT

  • 全文索引只能用于 InnoDBMyISAM表,并且只能为CHARVARCHARTEXT列创建。

  • FULLTEXT索引定义可以在创建表时在语句中给出 ,CREATE TABLE或者稍后使用 ALTER TABLEor 添加CREATE INDEX

  • FULLTEXT对于大型数据集,将数据加载到没有索引的表中然后创建索引比将数据加载到具有现有索引的表中 要快得多FULLTEXT

全文搜索是使用 MATCH() AGAINST()语法执行的。 MATCH()采用逗号分隔的列表来命名要搜索的列。 AGAINST接受一个要搜索的字符串,以及一个可选的修饰符,指示要执行的搜索类型。搜索字符串必须是在查询评估期间保持不变的字符串值。例如,这排除了表列,因为每行可能不同。

全文搜索分为三种类型:

  • 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 12.10.4, “Full-Text Stopwords”.

    Full-text searches are natural language searches if the IN NATURAL LANGUAGE MODE modifier is given or if no modifier is given. For more information, see Section 12.10.1, “Natural Language Full-Text Searches”.

  • 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. The IN BOOLEAN MODE modifier specifies a boolean search. For more information, see Section 12.10.2, “Boolean Full-Text Searches”.

  • 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. The IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION or WITH QUERY EXPANSION modifier specifies a query expansion search. For more information, see Section 12.10.3, “Full-Text Searches with Query Expansion”.

For information about FULLTEXT query performance, see Section 8.3.4, “Column Indexes”.

有关InnoDB FULLTEXT索引的更多信息,请参阅 第 14.6.2.3 节,“InnoDB 全文索引”

全文搜索的约束在 第 12.10.5 节,“全文限制”中列出。

myisam_ftdump实用程序转储MyISAM全文索引的内容。这可能有助于调试全文查询。请参阅 第 4.6.2 节,“myisam_ftdump — 显示全文索引信息”