MySQL全文搜索“ + this + week”无法正常工作

时间:2019-03-03 00:10:48

标签: mysql sql full-text-search

我正在尝试对数据库使用全文搜索,以加快搜索结果,但似乎无法正常工作:

例如,以下查询不返回结果

select * FROM data WHERE MATCH (name) AGAINST ('+this +week' IN BOOLEAN MODE)

此结果返回时:

select * FROM data WHERE name like '%this%week%'

我在做什么错了?

编辑:名称以'this week'开头的行很多。

1 个答案:

答案 0 :(得分:3)

两个表达式都是等价的:

  • 全文功能搜索单词,而带有LIKE特殊字符的%则基本上搜索字段的任何部分

  • 另一个区别是全文搜索允许按任意顺序排列单词,而LIKE要求单词遵循模式中给出的顺序

在使用全文搜索时,MySQL会故意忽略非常常见的单词的预定义列表,称为 stopwords :您可能会怀疑this是其中之一。这可能是阻止您的价值匹配的原因。您需要从搜索中删除该单词。有关MySQL全文停用词的列表,请参见this link(或仅查询INFORMATION_SCHEMA.INNODB_FT_DEFAULT_STOPWORD)。


考虑以下数据集:

select * from mytable;

| id  | name                    |
| --- | ----------------------- |
| 1   | this happened last week |
| 2   | last week this happened |
| 3   | thishappenedlastweek    |
| 4   | this happens this week  |

使用LIKE进行过滤时:

select * from mytable where name like '%this%week%';

| id  | name                    |
| --- | ----------------------- |
| 1   | this happened last week |
| 3   | thishappenedlastweek    |
| 4   | this happens this week  |

使用全文本搜索和停用词'this',不会返回任何内容:

select *
FROM mytable
WHERE MATCH (name) AGAINST ('+this +week' IN BOOLEAN MODE);

There are no results to be displayed.

这将在删除停用词时起作用:

select *
FROM mytable
WHERE MATCH (name) AGAINST ('+week' IN BOOLEAN MODE);

| id  | name                    |
| --- | ----------------------- |
| 1   | this happened last week |
| 2   | last week this happened |
| 4   | this happens this week  |

不包含停用词的组合也可以使用:

select *
FROM mytable
WHERE MATCH (name) AGAINST ('+happened +week' IN BOOLEAN MODE);

| id  | name                    |
| --- | ----------------------- |
| 1   | this happened last week |
| 2   | last week this happened |

Demo on DB Fiddle