# Did you mean

除了自动补全功能，我们在[这门课程](https://play.manticoresearch.com/simpleautocomplete/)中已经覆盖了一个简单示例，另一个人们常添加到搜索应用中的常见功能是显示错误拼写单词的更正建议。

Manticore Search 提供了一个功能，允许从索引字典中获取单词的建议。

这可以通过启用前缀匹配选项来实现。前缀匹配不仅允许通配符搜索，还会从索引的单词中生成 n-gram 哈希。

n-gram（或长度为 N 的单词部分）用于查找彼此接近的单词（作为纯文本，而非语言学意义上的接近）。结合建议候选词与原始词之间的编辑距离，我们可以提供适合作为错误单词更正的建议。此功能由 CALL SUGGEST 和 CALL QSUGGEST 函数提供（[了解更多](https://docs.manticoresearch.com/latest/html/sphinxql_reference/call_qsuggest_syntax.html) 请参阅文档）。

因此，下面我们将开始回顾 Manticore Search 中它是如何工作的，或者您可以在我们的 [交互式课程](https://play.manticoresearch.com/didyoumean/) 中亲自尝试。

首先，我们需要在索引中启用前缀匹配。

```ini
index movies
 {
    type            = plain
    path            = /var/lib/manticore/data/movies
    source          = movies
    min_infix_len   = 3
 }
```


## CALL SUGGEST 用法

---

当用户执行一个返回无结果的查询时，可能是用户输入有误。

让我们连接到 Manticore 并以一个例子（注意 'revenge' 中的拼写错误）：

` mysql -P9306 -h0`
```bash
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');`

```sql
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 列：建议、计算出的 [编辑距离](https://en.wikipedia.org/wiki/Levenshtein_distance) 和建议在索引中的文档命中数。

第一个建议与我们的输入相比距离为 1，它就是预期要建议的实际单词。

这通常是当我们获得最小距离的单一建议时的最佳场景，因为它最有可能是我们要找的。
即使距离为 1，也可能有多个建议：

` CALL SUGGEST('aprentice','movies');`

```sql
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');`

```sql
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)

```

如果我们想增加建议的数量，可以添加限制参数：

` CALL SUGGEST('aprentice','movies', 10 as limit);`

```sql
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)
```

如果我们想限制建议，可以降低最大编辑距离（默认为 4）和最大单词长度（默认为 3）：

` CALL SUGGEST('aprentice','movies', 10 as limit,3 as max_edits,2 as delta_len);`

```bash
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;`

```bash
MySQL [(none)]> exit;
Bye

```


## 工作示例

---

“你是指”的一个简单工作示例可以在我们的交互式课程的 [Web 面板](https://play.manticoresearch.com/didyoumean/) 中看到。

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

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

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

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

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

如果您觉得缺少了什么，请尝试 [课程](https://play.manticoresearch.com/didyoumean/)，阅读 [文档](https://docs.manticoresearch.com/latest/html/sphinxql_reference/call_qsuggest_syntax.html) 或在 [社区](https://manticoresearch.com/about/#community) 中提问。
