MySQL 全文搜索主题标签(包括索引中的 # 符号)

标签 mysql full-text-search myisam hashtag

我非常确定应该有一种方法可以使用 MyISAM 表中的全文索引来搜索主题标签。默认设置将执行以下操作:

textfield 
hashtag
#hashtag
#two #hashtag #hashtag

SELECT * FROM table WHERE MATCH(textfield) AGAINST ('#hashtag')
> | hashtag                |
> | #hashtag               |
> | #two #hashtag #hashtag |

虽然它应该只返回第二行和第三行。看起来主题标签被视为单词分隔符,因此在搜索开始之前它被“删除”。我应该怎样做才能启用索引并搜索包含 # 作为单词一部分的术语?

最佳答案

Fine-Tuning MySQL Full-Text Search 下所述:

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:

  • Modify the MySQL source: In storage/myisam/ftdefs.h, see the true_word_char() and misc_word_char() macros. Add '-' to one of those macros and recompile MySQL.

  • Modify a character set file: This requires no recompilation. The true_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 your FULLTEXT indexes. For information about the <ctype><map> array format, see Section 10.3.1, “Character Definition Arrays”.

  • 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 10.4, “Adding a Collation to a Character Set”. For an example specific to full-text indexing, see Section 12.9.7, “Adding a Collation for Full-Text Indexing”.

关于MySQL 全文搜索主题标签(包括索引中的 # 符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21296870/

相关文章:

java - 如何使用需要 2 个参数且仅其中一个参数存储在列表中的查询进行批量更新

mysql - SQL比较数据对

python - 检查另一个字符串中存在的匹配字符串的有效方法

postgresql - 短语搜索运算符 <-> 是否适用于 JSONB 文档或仅适用于关系表?

MySql InnoDB - 在表的 "Not Null"列中插入

mysql - 没有 InnoDB 的 Magento

php - jQuery 数据表 MySQL 日期到 PHP 日期

mysql - SQL 从一列中选择总和

ElasticSearch - cross_fields 多重匹配与模糊搜索

innodb - 使用 MyISAM 和 InnoDB 表备份 MySQL 数据库