Elasticsearch, on disk
An Elasticsearch index is split into shards, and each shard is a Lucene index plus a translog. Lucene does the storing: immutable segments, each a small search index of its own, listed by a commit point. Every example here is read from the sample, two single-shard indexes written by Elasticsearch 9.2.2 (Lucene 10.3.2).
A shard's directory
A node's data path holds indices/<index UUID>/<shard number>/ for every shard it has. Inside, index/ is the Lucene index, translog/ the log of recent writes, and _state/ a little metadata. The sample's albums index has 199 documents and tracks 1,561: the same music catalogue the other engines' samples hold, as JSON.
Segments and the commit point
Lucene never changes a file it has written. Indexed documents collect in memory and are written out as a new segment on a refresh or a flush; a delete only marks a document as gone; and merges combine small segments into bigger ones, leaving out deleted documents. The commit point, segments_N, lists the segments that make up the index at one moment, and a new commit writes segments_N+1. The sample's tracks shard, at segments_5:
| Segment | Documents | Soft-deleted | Files | Written by |
|---|---|---|---|---|
| _3 | 1,800 | 248 | 29 separate files | merge |
| _4 | 8 | 0 | compound (25 files in one) | flush |
The first came from a force merge of everything loaded, updated and deleted; the second from a later update. A segment written by a flush uses a compound file: its files concatenated into one .cfs, with a .cfe of where each starts, to save file handles. A merged segment is compound too unless it is large; the sample's tracks index turns compound files off for merges, so that its big segment shows the separate files. The commit also carries user data, strings the application chooses; Elasticsearch stores where the commit stands in its history: here max_seq_no 1807, local_checkpoint 1807 and the UUID of the translog that goes with it.
Every file has a header and a checksum
Every Lucene file starts with a header that names its format and version and repeats the segment's random ID, so a file from another segment cannot be mixed in by mistake, and ends with a 16-byte footer holding a CRC-32 of the file. _3.si, the segment info, is 691 bytes:
| Offset | Part | Bytes |
|---|---|---|
| 0 | magic number 0x3fd76c17 | 3f d7 6c 17 |
| 4 | codec name: length 19, "Lucene90SegmentInfo" | 13 4c 75 63 65 6e 65 39 30 53 65 67 6d 65 6e 74 49 6e 66 6f |
| 24 | format version 0 | 00 00 00 00 |
| 28 | the segment's 16-byte random ID | 8b 7f 5a df 69 5a f2 b9 b2 3a f8 41 1e 81 b8 eb |
| 44 | suffix: length 0 | 00 |
| 675 | footer magic (the header magic, inverted) | c0 28 93 e8 |
| 679 | checksum algorithm: 0, CRC-32 | 00 00 00 00 |
| 683 | CRC-32 of every byte before it, as an 8-byte number | 00 00 00 00 27 87 77 2d |
The header's numbers are big-endian; since Lucene 9 everything else is little-endian. Between them, the segment info holds the document count (1,800), the Lucene version (10.3.2), whether the segment is compound, the list of its files and diagnostics such as why it was written (merge).
The files of a segment
Each structure is its own file, usually a data file with a small metadata file beside it; a segment may have more than one set when fields use different formats (Elasticsearch puts _id under a bloom filter, so its postings get their own files):
| Extension | Holds |
|---|---|
| Segment | |
| .si | segment info: The segment's document count, the Lucene version that wrote it, whether it uses a compound file, the files it is made of, and diagnostics (why and how it was written). |
| .cfs | compound file: The segment's other files concatenated into one, so that small segments do not use dozens of file handles. |
| .cfe | compound entries: Where each file starts inside the compound file, and its length. |
| .fnm | field infos: Every field: its number, how it is indexed (documents only, frequencies, positions), its doc values type, point dimensions, and the per-field format each structure uses. |
| Deletes | |
| .liv | live documents: A bitset of the documents not deleted. Written as a new generation whenever deletes are committed; the segment itself never changes. |
| Stored fields | |
| .fdt | stored fields data: The stored field values of every document, in compressed chunks of several documents. Elasticsearch stores _id and _source here. |
| .fdx | stored fields index: The first document of each chunk and where the chunk starts, packed. |
| .fdm | stored fields meta: The chunk size, the number of chunks, and how the index is packed. |
| Inverted index | |
| .tim | terms dictionary: Every term of every indexed field, sorted, in blocks that share a prefix, with each term's statistics and where its postings start. |
| .tip | terms index: A trie over term prefixes that leads to the right block of the terms dictionary, small enough to keep in memory. |
| .tmd | terms meta: Per field: the number of terms, document and frequency totals, the smallest and largest term, and where its trie starts. |
| .doc | postings: documents: For each term, the documents that contain it and how often, in blocks of 128 bit-packed deltas, with skip data to jump over blocks. |
| .pos | postings: positions: For each term and document, the positions where it occurs, for phrase queries. |
| .pay | postings: payloads and offsets: Character offsets (for highlighting) and payloads, when a field stores them. |
| .psm | postings meta: Statistics for the postings files. |
| Doc values | |
| .dvd | doc values data: Column-oriented values, one column per field, for sorting, aggregations and scripts. |
| .dvm | doc values meta: How each column is encoded and where its parts are in the data file. |
| Norms | |
| .nvd | norms data: A small number per document and text field that encodes the field's length, for scoring. |
| .nvm | norms meta: How each field's norms are stored. |
| Points | |
| .kdd | points data: The leaves of each field's BKD tree: numbers, dates and addresses with the documents that have them, for range queries. |
| .kdi | points index: The inner nodes of each BKD tree: split values and pointers to the leaves. |
| .kdm | points meta: Per field: dimensions, bytes per dimension, bounds and counts. |
| Term vectors | |
| .tvd | term vectors data: Per-document inverted indexes, for fields that store term vectors. |
| .tvx | term vectors index: Where each document's term vectors start. |
| .tvm | term vectors meta: How the term vectors are packed. |
| Vectors | |
| .vec | vector data: Raw dense vectors (dense_vector fields). |
| .vex | vector graph: The HNSW graph over the vectors, for nearest-neighbour search. |
| .vem | vector meta: Per field: dimensions, similarity and where the graph is. |
| .veq | quantized vectors: Vectors quantized to bytes or bits, to make search cheaper. |
| Elasticsearch | |
| .bfi | bloom filter: An Elasticsearch bloom filter over _id, so that looking up a document by ID can skip segments that do not have it. |
| .bfm | bloom filter meta: Which fields have a bloom filter, and its size. |
| .tlog | translog: Elasticsearch's write-ahead log: every operation since the last Lucene commit, replayed when the shard opens. |
| .ckp | translog checkpoint: How far the current translog file is valid, and the sequence numbers it holds. |
Documents: _id and _source
A JSON document becomes a Lucene document with Elasticsearch's own fields added. Its ID is kept in _id in a compact binary form: "1" is fe 1f (0xfe, then two decimal digits per byte), and an ID that is valid URL-safe base64, like 1-0, is stored as the bytes it decodes to: d7 ed. The JSON sent is kept, unchanged, in _source: 213 bytes for album 1. These are stored fields, returned with search hits but not searchable.
Stored fields are kept in compressed chunks of several documents, so that similar documents compress well together. Segment _0 of albums uses Lucene90StoredFieldsFastData (LZ4 with a preset dictionary): 1 chunk holding 48.6 KB of documents in 12.3 KB. Each chunk is cut into blocks that are compressed with the chunk's first part as a shared dictionary, so a single document can be read without decompressing the whole chunk. Chunks are at most 80.0 KB; a small index in .fdx says where each starts.
The inverted index
Search needs the opposite of stored fields: for each term, the documents that contain it. A text field like name is analyzed into lowercase words, and each word is a term; a keyword field like tags is one term per value. In _3, name has 1,604 distinct terms over 1,760 documents.
The terms dictionary
Terms are sorted and grouped into blocks of about 25 to 48 that share a prefix; each entry stores only its suffix, its statistics (how many documents, how many occurrences) and where its postings start, and a block may point to sub-blocks of terms with a longer prefix. name takes 45 blocks. A trie over the prefixes, the terms index (.tip), is small enough to keep in memory and leads to the one block that can hold a term. Terms in only one document need no postings at all: that document's number is kept in the dictionary entry.
Postings
A term's postings are its documents, as differences from the previous one, with how often it occurs in each, and for text fields the positions where it occurs, for phrase queries. They are written in blocks of 128: the differences packed at the fewest bits that hold the largest, and frequencies the same way with a few exceptions allowed above the width. Before each block, skip data says where the block ends and its last document, so a query can jump past blocks. What is left after the last full block is written as variable-length integers. The sample's terms show the choices Lucene makes:
trackis in all 1,760 documents: 12 blocks of 128 consecutive documents, which need no data at all, 1 block of 128 as a bitset and the last 96 as variable-length integers.live, in every third track's name, is in 588 documents: 1 block of 128 as a bitset, 3 blocks of 128 as 2-bit deltas and the last 76 as variable-length integers.lais in 161 documents, four times in each: in document 0 at positions 4, 5, 6, 7.
Doc values
Sorting and aggregating need, for each document, its value: a column store. Doc values keep one column per field, packed. Elasticsearch's own _seq_no column is stored as differences from 0, 12 bits each. plays takes 1 bit per document in this segment: each value minus the smallest, 0. A keyword field such as path stores each document's ordinal, its position in a sorted dictionary of the distinct values, which is compressed with LZ4 in blocks of 64. A column that only some documents have keeps a separate set of which documents those are: a list of document numbers when there are few, a bitmap when there are many.
Points
Numbers, dates and IP addresses are indexed as points in a BKD tree, for range queries. Each value is stored as bytes that sort like the number (big-endian, with the sign bit flipped), the tree splits the range of values in halves until each leaf holds at most 512, and a range query visits only the leaves that can match. The albums' year field in _0: 200 points of 4 bytes in 1 leaf, from 1960 to 2019. Within a leaf, the prefix all values share is stored once and runs of equal bytes once: the 200 years take 529 bytes with their document numbers.
Deletes, updates and soft deletes
A segment cannot change, so a delete is recorded outside it. Lucene's own deletes are a bitset of live documents in a .liv file of a new generation. Elasticsearch instead uses soft deletes: the old document stays and gets a value in the doc values field __soft_deletes, written as a doc values update (new .fnm, .dvm and .dvd files with the generation in their names). An update is a soft delete of the old version and a new document; a delete adds a tombstone, a document with only an ID and sequence number. The kept history lets a replica catch up by replaying operations instead of copying files, until merges drop documents older than the retention. In the sample's tracks shard, 248 of _3's 1,800 documents are soft-deleted, 40 of them tombstones.
The translog
A Lucene commit writes and fsyncs every new file, far too slow for each request. Elasticsearch acknowledges a write once it is in the translog, its own write-ahead log, and makes Lucene commits (a flush) now and then. Each operation is its size, the operation (index, delete or no-op, with ID, source, sequence number, primary term and version) and a CRC-32; translog.ckp says how much of the current file is valid. When a shard opens, operations after the commit's local checkpoint are replayed. The sample's node was killed with 3 operations only in the translogs: index of 1 (sequence number 201), delete of 200 (sequence number 202), index of 1-8 (sequence number 1808); album 1's new price is in the translog, while _0 still has the old one.
A refresh, once a second by default, makes indexed documents searchable by writing them to a new segment without a commit; that is why recent writes are visible in search before they are durable in Lucene, and durable in the translog before either.
Reading it back
The viewer does all of this in the browser: it reads the commit, opens every segment and checks every checksum, decodes stored fields, the terms dictionary and postings, doc values, points and norms, then takes each segment's live documents and replays the translog over them. On the sample, the documents it rebuilds match, ID for ID, version for version, what Elasticsearch itself returns after replaying the same translog.