# Annoy（软件）

**Annoy**（Approximate Nearest Neighbors Oh Yeah，近似最近邻，没错）是一个带 Python 绑定的 C++ 库，用于近似最近邻搜索，即在高维空间中找出与给定查询点相近的点，而不必将查询与每一个已存储条目逐一穷举比较<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup>。它由[埃里克·伯恩哈德松](https://www.edgepedia.org/zh/erik-bernhardsson)（Erik Bernhardsson）在 Spotify 开发，于 2013 年开源，此后十年间支撑着 Spotify 的个性化、推荐和搜索系统，包括“每周发现”（Discover Weekly）和“首页”（Home）等功能<sup>[2](https://engineering.atspotify.com/2023/10/introducing-voyager-spotifys-new-nearest-neighbor-search-library)</sup>。

| 关键事实 | 详情 |
|---|---|
| 全名 | Approximate Nearest Neighbors Oh Yeah（近似最近邻，没错）<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup> |
| 起源 | 由埃里克·伯恩哈德松在 Spotify 开发，2013 年开源；在 Spotify 黑客周期间用几个下午写成<sup>[2](https://engineering.atspotify.com/2023/10/introducing-voyager-spotifys-new-nearest-neighbor-search-library)</sup><sup> • </sup><sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup> |
| 索引类型 | 随机投影树森林<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup> |
| 内存模型 | 索引采用内存映射（mmap）；在支持的平台上，加载和保存时默认对索引进行预取，可通过 `prefault` 设置禁用<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup> |
| 距离度量 | 角距离（余弦）、欧几里得距离、曼哈顿距离、汉明距离和点积<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup> |
| 最新版本 | 1.17.3，2023 年 6 月 14 日发布，采用 Apache License 2.0<sup>[3](https://pypi.org/project/annoy/)</sup> |
| Spotify 的继任者 | Voyager，基于 hnswlib，旨在取代 Annoy 用于生产环境；自 2022 年起承载 Spotify 生产流量<sup>[2](https://engineering.atspotify.com/2023/10/introducing-voyager-spotifys-new-nearest-neighbor-search-library)</sup> |

## 工作原理

Annoy 使用随机投影构建索引，将其组织为一棵树。在树的每个中间节点，一个随机超平面将空间分成两个子空间；该超平面通过从该节点对应的子集中采样两个点、取与它们等距的超平面来确定。此过程重复进行，构建出一片树森林，树的数量 k 由用户调节，以在精度和性能之间权衡<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup>。该方法源自随机投影树——一种使用随机方向分割数据的树状向量量化方法<sup>[4](https://arxiv.org/abs/0805.1390)</sup>。

查询通过遍历森林来回答，因此搜索成本的增长远慢于精确搜索所需的线性扫描。由于结果是近似的，评估结果用召回率衡量，即算法在前 k 个结果中返回的真实最近邻所占的比例<sup>[5](https://ann-benchmarks.com/index.html?trk=public_post-text)</sup>。

**内存映射。** 索引存储为静态文件并进行内存映射，因此多个进程可以共享同一份磁盘上的索引，而无需各自加载副本。在支持的平台上，索引在 `load` 和 `save` 期间会被预取，即文件被抢先读入内存。将 `prefault` 设为 `False` 则把页面留在磁盘上按需缓存，这可能会显著增加早期搜索时间，但适合内存相对于索引规模较小的系统、查询量少的工作负载，或大片区域不太可能被查询的索引<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup>。

**距离度量。** 除标准的角距离、欧几里得距离和曼哈顿距离外，Annoy 还包含两种专门实现。[汉明距离](https://www.edgepedia.org/zh/hamming-distance)由马丁·奥米勒（Martin Aumüller）贡献，将数据打包为 64 位整数，利用内置的位计数原语提速，所有分裂均轴对齐。点积距离由彼得·索博特（Peter Sobot）和帕维尔·科罗博夫（Pavel Korobov）添加，将输入的向量从点积空间变换到更适合查询的余弦空间；它依赖于[微软研究院](https://www.edgepedia.org/zh/microsoft-research)巴赫拉赫（Bachrach）及其同事 2014 年的一项技术。<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup>

## 在 Spotify 的使用

伯恩哈德松最初的公告描述了动机用例：Spotify 使用 Annoy 将数百万首曲目放入 40 维空间，然后查询最相似的曲目<sup>[6](https://erikbern.com/2013/04/12/annoy.html)</sup>。该库随后支撑了“每周发现”、“首页”及其他 Spotify 功能约十年之久<sup>[2](https://engineering.atspotify.com/2023/10/introducing-voyager-spotifys-new-nearest-neighbor-search-library)</sup>。近似搜索在此场景中很重要，因为它让系统能够推荐相似的曲目、艺人或专辑，而无需实时运行缓慢且昂贵的机器学习算法<sup>[2](https://engineering.atspotify.com/2023/10/introducing-voyager-spotifys-new-nearest-neighbor-search-library)</sup>。

除官方 Python 绑定外，社区维护的移植版本将该库扩展到其他语言：安迪·斯隆（Andy Sloane）提供了 Java 版本，仅限余弦距离且为只读；吉姆·康（Jim Kang）在 2016 年 Spotify 黑客周的部分时间里编写了 Node 绑定<sup>[1](https://github.com/spotify/annoy?ref=jeremyjordan.me)</sup>。Annoy 也是 ANN-Benchmarks 收录的库之一，后者是近似最近邻算法的标准基准测试环境<sup>[5](https://ann-benchmarks.com/index.html?trk=public_post-text)</sup>；scikit-learn 文档记载了将其封装为 t-SNE 中近似最近邻的替代转换器<sup>[7](https://scikit-learn.org/1.1/auto_examples/neighbors/approximate_nearest_neighbors.html)</sup>。

## 在当前生态中的地位

自 2013 年以来，最近邻搜索的最新技术已有长足进步。在 2023 年对自家库的一次评估中，Spotify 判断 Annoy 如今已稳居中游，因为其他系统能在相同运行时间内达到两倍的精度，或在十分之一的运行时间内达到相当的精度<sup>[2](https://engineering.atspotify.com/2023/10/introducing-voyager-spotifys-new-nearest-neighbor-search-library)</sup>。该领域已转向基于图的索引，如 HNSW（Hierarchical Navigable Small World，分层可导航小世界），基准测试工作也记录了基于图的方法与 Annoy 这类随机投影森林方法之间的一般性能差异<sup>[8](https://doi.org/10.1016/j.is.2019.02.006)</sup>。

实测延迟反映了这一差距。在一项已发表的、针对 30 万条目工作负载的相似性搜索方法比较中，Annoy 和 NGT 每次查询均超过 24 至 26 毫秒，慢于 HNSW 的 14 至 17 毫秒和基于 LSH 的方法的 16 至 19 毫秒<sup>[9](https://link.springer.com/article/10.1186/s13677-025-00833-x)</sup>。

Spotify 的回应是 Voyager，一个基于 hnswlib 的新库，旨在接替 Annoy 成为公司推荐用于生产环境的最近邻库。Voyager 结合了 HNSW 的精度和速度与生产就绪的 Java 和 Python 绑定，在 2023 年 10 月发布公告之前，自 2022 年起已承载 Spotify 生产流量<sup>[2](https://engineering.atspotify.com/2023/10/introducing-voyager-spotifys-new-nearest-neighbor-search-library)</sup>。Annoy 本身仍然可用：其最终列出的版本 1.17.3 修复了一个 OS X 构建问题，于 2023 年 6 月 14 日出现在 PyPI 上<sup>[3](https://pypi.org/project/annoy/)</sup>；此前的 1.17.2 修复了内存泄漏，1.17.0 增加了多线程索引构建<sup>[10](https://github.com/spotify/annoy/releases)</sup>。

## References

1. [spotify/annoy README, GitHub](https://github.com/spotify/annoy?ref=jeremyjordan.me)
2. [Introducing Voyager: Spotify's New Nearest-Neighbor Search Library, Spotify Engineering (October 2023)](https://engineering.atspotify.com/2023/10/introducing-voyager-spotifys-new-nearest-neighbor-search-library)
3. [annoy on PyPI](https://pypi.org/project/annoy/)
4. [Random projection trees for vector quantization, arXiv](https://arxiv.org/abs/0805.1390)
5. [ANN-Benchmarks](https://ann-benchmarks.com/index.html?trk=public_post-text)
6. [Annoy, Erik Bernhardsson (April 12, 2013)](https://erikbern.com/2013/04/12/annoy.html)
7. [Approximate nearest neighbors in t-SNE, scikit-learn 1.1 documentation](https://scikit-learn.org/1.1/auto_examples/neighbors/approximate_nearest_neighbors.html)
8. [ANN-Benchmarks: A benchmarking tool for approximate nearest neighbor algorithms, Information Systems](https://doi.org/10.1016/j.is.2019.02.006)
9. [Edge computing-enabled scalable health training log analysis with locality-sensitive hashing for similarity search, Journal of Cloud Computing](https://link.springer.com/article/10.1186/s13677-025-00833-x)
10. [Releases of spotify/annoy, GitHub](https://github.com/spotify/annoy/releases)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database engines and systems › Vector databases and similarity-search stores

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/annoy-software

English original: https://www.edgepedia.org/annoy-software
