# How We Added Autocomplete to Our GitHub Search Demo

Discover how we implemented autocomplete in our GitHub Issue Search demo using Manticore Search. Learn the basics, advanced features like fuzzy matching and multi-language support, and see it in action with a live demo.

## 引言

你是否注意到像Google这样的搜索引擎在你输入时会提供文本建议？这就是自动补全功能——一种通过实时提供建议来帮助用户更快找到所需内容的功能。

在本文中，我们将展示如何使用Manticore Search在我们的GitHub Issue Search演示中实现自动补全功能。无论你是在构建类似功能还是仅仅对这个过程感兴趣，本指南都将逐步讲解基础知识。

![image](./github-demo-how-we-added-autocomplete/github-demo-autocomplete.png)

## 你需要的准备

- Manticore Search 7.0.0 或更新版本
- [Manticore Search PHP客户端](https://github.com/manticoresoftware/manticoresearch-php)
- 基础的PHP知识

## 基础实现

虽然自动补全功能需要客户端JavaScript来实现用户界面（我们在此不涉及），但后端组件的实现非常简单。只需创建一个端点，通过官方[PHP SDK](https://github.com/manticoresoftware/manticoresearch-php)与Manticore Search通信，以获取并返回建议。

```php
$client = new Client();
$result = $client->autocomplete([
    'body' => [
        'table' => 'your_table',
        'query' => 'user_input',
    ],
]);
```

就是这样！这个端点接收用户输入，查询Manticore Search，并返回建议。将其与前端的JavaScript结合，实时显示结果，你就能获得流畅的自动补全体验。

点击此处查看演示[here](https://github.manticoresearch.com/manticoresoftware/manticoresearch)。

## 高级功能

虽然自动补全功能开箱即用效果良好，但你可以通过模糊匹配和键盘布局支持等特性来增强它。以下是使用Manticore Search的PHP客户端实现这些功能的方法。

### 添加模糊匹配

用户会犯拼写错误——这是常有的事。通过模糊匹配，即使查询未完全正确，你也可以返回相关建议。以下是启用它的方法：

```php
$result = $client->autocomplete([
    'body' => [
        'table' => 'your_table',
        'query' => 'user_input',
        'options' => [
            'fuzziness' => 1
        ],
    ],
]);
```

### 使用键盘布局的多语言支持

如果你的应用程序面向不同语言的用户，键盘布局支持会带来显著差异。以下是添加对美式英语、乌克兰语和俄语布局支持的示例：

```php
$result = $client->autocomplete([
    'body' => [
        'table' => 'your_table',
        'query' => 'user_input',
        'options' => [
            'fuzziness' => 1,
            'layouts' => ['us', 'ru', 'ua']
        ],
    ],
]);
```

这确保用户无论使用哪种键盘布局都能获得准确的建议。

## 我们是如何实现的：GitHub Issue Search演示

在我们的GitHub Issue Search演示中，我们启用了对问题和评论的搜索功能。我们实现了一个单一端点，该端点向Manticore Search查询两次并将结果合并为统一响应。有关实现细节，请查看[源代码](https://github.com/manticoresoftware/manticore-github-issue-search/blob/47955a502cdb6d5ba6de90cdc6d50af5758c623e/app/src/component/Search.php#L328-L375)。

前端实现使用了原生JavaScript，通过防抖事件监听器实现高效的查询处理。查看完整的[实现代码](https://github.com/manticoresoftware/manticore-github-issue-search/blob/47955a502cdb6d5ba6de90cdc6d50af5758c623e/app/src/component/Search.php#L328-L375)。

## 结论

使用Manticore Search的PHP客户端实现自动补全功能既简单又灵活。模糊匹配和键盘布局支持的结合使其在实际应用中尤为有效。

要了解实际案例，请查看我们的[GitHub Issue Search演示](https://github.com/manticoresoftware/manticore-github-issue-search)，我们在生产环境中实现了这些功能。
