⚠️ 此页面为自动翻译,翻译可能不完美。
blog-post

你是想说

除了自动完成功能,我们在 本课程 中介绍了一个简单的示例,另一个人们常常添加到搜索应用中的常见功能是显示错误输入单词的更正。

Manticore Search 提供了一项功能,可以从索引字典中获取单词的建议。

这可以通过启用中间匹配选项来实现。中间匹配不仅允许通配符搜索,还会从索引单词中创建 n-gram 哈希。

N-gram(或仅是长度为 N 个字符的单词部分)用于查找彼此接近的单词(作为纯文本,而非语言学意义上的)。结合建议候选单词与原始单词之间的 Levenshtein 距离,我们可以提供适合作为错误单词更正的建议。此功能由 CALL SUGGEST 和 CALL QSUGGEST 函数提供( 在文档中阅读更多 )。

所以让我们开始回顾 Manticore Search 中它是如何工作的,或者你可以在我们的 互动课程 中自己尝试。

首先,我们应该在我们的索引中启用中间匹配。

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 面板中可以看到一个简单的 '你是想说' 工作示例。

PHP 脚本提供了一个简单的搜索页面结果。

如果输入字符串没有找到结果,脚本会使用 'CALL SUGGEST' 测试每个单词,并尝试构建一个新的查询字符串。

如果新的查询字符串匹配,则提供其结果集。

可以通过 cat /html/index.php 查看脚本

root@didyoumean-b85fb586f-2nvh2:/tutorial# cat /html/index.php

如果你觉得有什么缺失,尝试一下 课程 ,阅读 文档 或在 社区 中询问。

安装Manticore Search

安装Manticore Search