除了我们在 本课程 中介绍的简单示例之外,另一个常见的功能是提供错误输入单词的更正建议。
Manticore Search 具有从索引字典中获取单词建议的功能。
这可以通过启用 infixing 选项来完成。 not only infixing 允许通配符搜索,它还会从索引单词中创建 n-gram 哈希。
N-grams(或仅是长度为 N 个字符的单词部分)用于找到彼此接近的单词(作为普通文本,而不是语言学意义)。结合建议候选词与原始词之间的 Levenshtein 距离,我们可以提供适合作为错误单词更正的建议。此功能由 CALL SUGGEST 和 CALL QSUGGEST 函数提供的( 阅读更多 在文档中)。
现在让我们开始审查它在 Manticore Search 中是如何工作的,或者您可以在我们的 互动课程 中自己尝试。
首先,我们应该在我们的索引中启用 infixing。
index movies
{
type = plain
path = /var/lib/manticore/data/movies
source = movies
min_infix_len = 3
}
CALL SUGGEST 使用方法
当用户执行查询但返回无结果时,用户可能会输入错误。
让我们连接到 Manticore 并举例说明(注意“revenge”中的拼写错误):
mysql -P9306 -h0
root@didyoumean-b85fb586f-2nvh2:/tutorial# mysql -P9306 -h0
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 3.2.0 e526a014@191017 release
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
并快速示范一个单词建议:
CALL SUGGEST('rvenge','movies');
MySQL [(none)]> CALL SUGGEST('rvenge','movies');
+---------+----------+------+
| suggest | distance | docs |
+---------+----------+------+
| revenge | 1 | 77 |
| range | 2 | 5 |
| avenger | 2 | 3 |
| avenged | 2 | 1 |
| event | 3 | 9 |
+---------+----------+------+
5 rows in set (0.00 sec)
输出包含 3 列:建议、计算的 Levenshtein 距离和索引中建议的文档命中数。
第一个建议与我们的输入距离为 1,实际期望被建议的单词就是它。
通常,这是最理想的情况,因为我们在最小距离处得到单个建议,这很可能是我们寻找的。
即使距离为 1 也可能有多个建议:
CALL SUGGEST('aprentice','movies');
MySQL [(none)]> CALL SUGGEST('aprentice','movies');
+------------+----------+------+
| suggest | distance | docs |
+------------+----------+------+
| apprentice | 1 | 6 |
| prentice | 1 | 1 |
| practice | 3 | 5 |
| argentine | 3 | 1 |
| prestige | 3 | 1 |
+------------+----------+------+
5 rows in set (0.00 sec)
当它们共享相同的距离时,建议会根据文档命中数重新排序。
在这个例子中,‘apprentice’ 更有可能是用户想要的,因为它的命中数比 ‘prentice’ 多。
当然,当输入的单词实际在我们的索引中找到时,它将作为第一个建议出现,且距离为 0。
CALL SUGGEST('revenge','movies');
MySQL [(none)]> CALL SUGGEST('revenge','movies');
+----------+----------+------+
| suggest | distance | docs |
+----------+----------+------+
| revenge | 0 | 77 |
| reverse | 2 | 2 |
| revelle | 2 | 1 |
| seven | 3 | 11 |
| berenger | 3 | 9 |
+----------+----------+------+
5 rows in set (0.01 sec)
如果我们想增加建议的数量,可以添加 limit 参数:
CALL SUGGEST('aprentice','movies', 10 as limit);
MySQL [(none)]> CALL SUGGEST('aprentice','movies', 10 as limit);
+------------+----------+------+
| suggest | distance | docs |
+------------+----------+------+
| apprentice | 1 | 6 |
| prentice | 1 | 1 |
| practice | 3 | 5 |
| argentine | 3 | 1 |
| prestige | 3 | 1 |
| adventure | 4 | 894 |
| lawrence | 4 | 43 |
| laurence | 4 | 10 |
| terence | 4 | 9 |
| prejudice | 4 | 9 |
+------------+----------+------+
10 rows in set (0.00 sec)
如果我们希望限制建议,可以降低最大 Levenshtein 距离(默认值为 4)和最大单词长度(默认值为 3):
CALL SUGGEST('aprentice','movies', 10 as limit,3 as max_edits,2 as delta_len);
MySQL [(none)]> CALL SUGGEST('aprentice','movies', 10 as limit,3 as max_ dits,2 as delta_len);
+------------+----------+------+
| suggest | distance | docs |
+------------+----------+------+
| apprentice | 1 | 6 |
| prentice | 1 | 1 |
| practice | 3 | 5 |
| argentine | 3 | 1 |
| prestige | 3 | 1 |
+------------+----------+------+
5 rows in set (0.00 sec)
下一步,我们需要退出 MySQL 客户端。
exit;
MySQL [(none)]> exit;
Bye
工作示例
在我们的 Web 面板 中可以看到一个 ‘Did you mean’ 的简单工作示例。
PHP 脚本提供了一个简单的搜索页面结果。
如果输入字符串找不到结果,脚本会对每个单词使用 ‘CALL SUGGEST’ 并尝试构建一个新的查询字符串。
如果新的查询字符串匹配,它的结果集将被提供。
可以通过 cat /html/index.php
查看脚本。
root@didyoumean-b85fb586f-2nvh2:/tutorial# cat /html/index.php