# 倒排索引

倒排索引是信息检索中使用的一种数据结构，它将每个词项映射到包含该词项的文档列表，颠倒了文档列出自身词汇的通常关系。它是文本搜索系统的核心数据结构，从图书馆数据库到网络搜索引擎皆然；它由两部分组成：一个词项词典，以及每个词项对应的、包含该词项的文档的记录列表。<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html)</sup><sup> • </sup><sup>[2](https://web.stanford.edu/~jurafsky/slp3/ed3book_Jan25.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 结构 | 一个词项词典（也称词汇表或词库），每个词项指向一个文档ID的记录列表，可选地附带词频和词位置<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html)</sup><sup> • </sup><sup>[3](https://www.elastic.co/docs/solutions/search/full-text/how-full-text-works)</sup> |
| 为何可扩展 | 对于100万篇各含1000词、共有50万个不同词项的文档，完整的词项-文档矩阵将有五千亿个单元格，其中至少99.8%为零；倒排索引只记录实际出现的位置<sup>[4](https://nlp.stanford.edu/IR-book/pdf/01bool.pdf)</sup> |
| 查询成本 | 对两个长度分别为m和n的记录列表求交集需要O(m + n)次操作；跳指针可以进一步降低这一成本<sup>[5](https://nlp.stanford.edu/IR-book/html/htmledition/faster-postings-list-intersection-via-skip-pointers-1.html)</sup> |
| 压缩 | 1:4的压缩比被描述为易于实现，可能将索引存储成本降低75%<sup>[6](https://nlp.stanford.edu/IR-book/html/htmledition/index-compression-1.html)</sup> |
| 数据库应用 | PostgreSQL的GIN（广义倒排索引）存储（键，记录列表）对，是全文本搜索的首选索引类型<sup>[7](https://www.postgresql.org/docs/current/gin.html)</sup><sup> • </sup><sup>[8](https://www.postgresql.org/docs/12/textsearch-indexes.html)</sup> |
| 搜索引擎应用 | Lucene的核心是一个倒排索引，其记录将词项映射到包含该词项的有序文档列表；Elasticsearch在将文本分析为词元后以同样的方式构建索引<sup>[9](https://lucene.apache.org/core/8_0_0/core/org/apache/lucene/index/package-summary.html)</sup><sup> • </sup><sup>[3](https://www.elastic.co/docs/solutions/search/full-text/how-full-text-works)</sup> |
| 历史 | 杰拉德·萨尔顿于1961年启动的SMART系统使用了大型倒排索引，其中为每个关键词包含被该关键词索引的所有文档的地址列表<sup>[10](https://citeseerx.ist.psu.edu/document?doi=ca56e647e65a3475d365cfb0422c3f46e61d1414&repid=rep1&type=pdf)</sup> |

## 结构与术语

索引有两个组成部分。词典是一个词项列表，为高效访问而设计，每个词项指向一个记录列表。记录列表记录与某个词项关联的文档ID，还可以携带诸如该词项在每个文档中的词频或该词项出现的确切位置等信息。<sup>[2](https://web.stanford.edu/~jurafsky/slp3/ed3book_Jan25.pdf)</sup> 每条记录某词项出现在某文档中的条目，按惯例称为一个记录项，所有记录列表合在一起称为记录。<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html)</sup>

正如该领域的标准教科书所指出的，这个名称是冗余的：索引总是从词项映射回文档中它们出现的位置。尽管如此，“倒排索引”，有时是“倒排文件”，已成为信息检索中的标准术语。<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html)</sup> 在非位置索引中，一个记录项只是一个文档ID；当存储位置时，带有位置数据的（词项，文档ID）对支持短语查询和邻近查询。<sup>[4](https://nlp.stanford.edu/IR-book/pdf/01bool.pdf)</sup>

## 为何需要倒排

信息检索中的基本搜索问题是找出集合中所有包含查询词项的文档；任何不包含查询词项的文档得分为零，可以忽略。<sup>[2](https://web.stanford.edu/~jurafsky/slp3/ed3book_Jan25.pdf)</sup> 朴素的替代方案——词项-文档矩阵，在词项出现于文档处填1——在大规模下失效。对于N = 100万篇各约1000词的文档、M = 50万个不同词项的集合，矩阵有五千亿个单元格，内存无法容纳；而且由于每篇文档只有1000词长，至少99.8%的单元格为零。<sup>[4](https://nlp.stanford.edu/IR-book/pdf/01bool.pdf)</sup> 只记录1的位置——这正是倒排索引所做的——将不可行的稠密结构转变为紧凑的稀疏结构。

**构建索引。** 索引构建的过程是收集文档，将文本分词为词元列表，然后生成记录项。为了在检索时获得速度优势，索引必须预先构建。<sup>[4](https://nlp.stanford.edu/IR-book/pdf/01bool.pdf)</sup> 在生产系统中，文档在索引之前经过一条分析流水线：字符过滤器去除标记，分词器将字符串拆分为单个词项，词元过滤器应用小写化、词干提取（将“running”归约为“run”）和停用词去除。<sup>[11](https://www.elastic.co/what-is/full-text-search)</sup> 同样的分析应用于查询文本，以便查询的词元可以在索引中查找。<sup>[3](https://www.elastic.co/docs/solutions/search/full-text/how-full-text-works)</sup>

## 查询处理

一个布尔查询，如Brutus AND Caesar AND NOT Calpurnia，通过获取每个词项的记录列表、对NOT词项取补集、然后对列表求交集来回答。<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html)</sup> 标准的交集算法同时遍历两个有序列表，时间与条目总数成线性关系，对于长度为m和n的列表为O(m + n)。<sup>[5](https://nlp.stanford.edu/IR-book/html/htmledition/faster-postings-list-intersection-via-skip-pointers-1.html)</sup> 这就是记录列表按文档ID排序存储的原因。<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html)</sup>

**跳指针。** 交集可以通过在记录列表中增加跳指针来加速，跳指针是一种捷径，允许引擎跳过不可能出现在结果中的条目。一个在实践中效果良好的简单启发式方法是在长度为P的列表上均匀放置√P个跳指针；更多跳指针意味着更短的跨度和更多的跳过机会，但也意味着更多空间和更多指针比较。跳指针有助于AND查询，但无助于OR查询，而且当索引相对静态时最容易构建。<sup>[5](https://nlp.stanford.edu/IR-book/html/htmledition/faster-postings-list-intersection-via-skip-pointers-1.html)</sup>

**排序。** 除布尔匹配之外，现代系统对结果进行相关性评分。Elasticsearch的默认相似度算法是[Okapi BM25](https://www.edgepedia.org/zh/okapi-bm25)，它是TF-IDF的一种变体，根据词频、文档频率和文档长度计算得分；记录列表提供词频和文档频率输入。<sup>[3](https://www.elastic.co/docs/solutions/search/full-text/how-full-text-works)</sup>

## 压缩与工程

由于大型索引是称为倒排列表的有序整数序列的集合，压缩对大规模搜索引擎至关重要。它减少磁盘空间，1:4的压缩比被描述为易于实现；它还改善缓存：压缩后的记录使更多索引能放入内存，因此频繁的单词项查询可以完全从内存中回答，只需很小的解压成本。<sup>[6](https://nlp.stanford.edu/IR-book/html/htmledition/index-compression-1.html)</sup><sup> • </sup><sup>[12](https://arxiv.org/abs/1908.10598)</sup> 最优编码取决于硬件平衡：当CPU较慢时，重度压缩不值得；但在CPU快、磁盘慢的情况下，减小记录列表大小占主导地位；全内存引擎又改变了这种权衡。<sup>[5](https://nlp.stanford.edu/IR-book/html/htmledition/faster-postings-list-intersection-via-skip-pointers-1.html)</sup>

**数据库实现。** [PostgreSQL](https://www.edgepedia.org/zh/postgresql)提供GIN（广义倒排索引）作为其首选的文本搜索索引类型。GIN索引存储一组（键，记录列表）对，其中记录列表保存键出现的行ID；每个键值只存储一次，这使得同一键多次出现时索引保持紧凑。GIN的“广义”在于访问方法不需要知道它加速的具体操作，并且存在用于文本搜索、数组、JSONB和三字符组的操作符类。<sup>[7](https://www.postgresql.org/docs/current/gin.html)</sup> 更新GIN索引往往较慢，这是倒排索引的本质所致：插入一行可能引起多次索引插入，每从该行提取一个键就插入一次，因此PostgreSQL可以将更新暂存在待处理条目列表中，待清理操作时再写入主索引。<sup>[7](https://www.postgresql.org/docs/current/gin.html)</sup> 与有损的GiST替代方案相比，GIN查找经测量约快三倍，但更新约慢十倍，因此GIN更适合静态数据，GiST更适合频繁更新的集合。<sup>[13](https://www.postgresql.org/docs/8.3/textsearch-indexes.html)</sup>

**搜索引擎实现。** [Apache Lucene](https://www.edgepedia.org/zh/apache-lucene)的核心是倒排索引，由将词项映射到包含该词项的有序文档列表的记录组成；编解码器可以在记录旁记录影响值，以便在搜索时跳过低分文档。记录无法在给定文档的情况下检索词项，除非扫描整个索引；存储字段服务于相反的方向。<sup>[9](https://lucene.apache.org/core/8_0_0/core/org/apache/lucene/index/package-summary.html)</sup> Lucene索引由独立的段组成，新文档创建新段，段随时间合并。<sup>[14](https://lucene.apache.org/core/3_0_3/fileformats.pdf)</sup>

## 历史与现状

到20世纪70年代，倒排文件组织已经是快速响应复杂多键查询的传统结构，使用指针或位串表示。<sup>[15](https://doi.org/10.1145/800237.807128)</sup> 杰拉德·萨尔顿是康奈尔大学计算机科学教授、1961年启动的SMART检索系统的创造者，他描述了构建大型倒排索引，其中为每个允许的关键词包含被该关键词索引的所有文档的地址列表，从而支持快速的布尔查询处理。<sup>[10](https://citeseerx.ist.psu.edu/document?doi=ca56e647e65a3475d365cfb0422c3f46e61d1414&repid=rep1&type=pdf)</sup>

这一结构延续到了神经检索中。SPLADE等学习型稀疏模型产生的文档和查询表示继承了词袋模型的优良特性，包括精确词项匹配和倒排索引的效率，因此现代排序流水线中的第一阶段检索仍然运行在倒排索引基础设施上。<sup>[16](https://arxiv.org/abs/2109.10086)</sup>

## References

1. [An example information retrieval problem, Introduction to Information Retrieval (Manning, Raghavan, Schütze), Stanford](https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html)
2. [Speech and Language Processing, 3rd ed., Chapter 14: Information Retrieval (Jurafsky & Martin), Stanford](https://web.stanford.edu/~jurafsky/slp3/ed3book_Jan25.pdf)
3. [How full-text search works, Elastic Docs](https://www.elastic.co/docs/solutions/search/full-text/how-full-text-works)
4. [Introduction to Information Retrieval, Chapter 1: Boolean retrieval (PDF), Stanford](https://nlp.stanford.edu/IR-book/pdf/01bool.pdf)
5. [Faster postings list intersection via skip pointers, Introduction to Information Retrieval, Stanford](https://nlp.stanford.edu/IR-book/html/htmledition/faster-postings-list-intersection-via-skip-pointers-1.html)
6. [Index compression, Introduction to Information Retrieval, Stanford](https://nlp.stanford.edu/IR-book/html/htmledition/index-compression-1.html)
7. [PostgreSQL Documentation: 65.4. GIN Indexes](https://www.postgresql.org/docs/current/gin.html)
8. [PostgreSQL 12 Documentation: 12.9. Preferred Index Types for Text Search](https://www.postgresql.org/docs/12/textsearch-indexes.html)
9. [org.apache.lucene.index package summary, Lucene 8.0.0 API](https://lucene.apache.org/core/8_0_0/core/org/apache/lucene/index/package-summary.html)
10. [Gerard Salton, Full Text Information Processing Using the Smart System](https://citeseerx.ist.psu.edu/document?doi=ca56e647e65a3475d365cfb0422c3f46e61d1414&repid=rep1&type=pdf)
11. [What is full-text search? A guide to FTS, Elastic](https://www.elastic.co/what-is/full-text-search)
12. [Cataldo et al., Techniques for Inverted Index Compression, arXiv:1908.10598](https://arxiv.org/abs/1908.10598)
13. [PostgreSQL 8.3 Documentation: GiST and GIN Index Types](https://www.postgresql.org/docs/8.3/textsearch-indexes.html)
14. [Apache Lucene Index File Formats (3.0.3)](https://lucene.apache.org/core/3_0_3/fileformats.pdf)
15. [On the use of bit maps for multiple key retrieval, ACM SIGPLAN Notices, 1976](https://doi.org/10.1145/800237.807128)
16. [Formal et al., SPLADE v2: Sparse Lexical and Expansion Model for Information Retrieval, arXiv:2109.10086](https://arxiv.org/abs/2109.10086)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database theory and data modeling › Indexing and physical data organization

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/inverted-index

English original: https://www.edgepedia.org/inverted-index
