Quick revision
The short answer
Lucene keeps three different structures per field for three different jobs: the inverted index maps terms to documents for searching, doc values store each document's value in a columnar on-disk format for sorting, aggregations and scripts, and stored fields keep the original document, the _source in Elasticsearch, compressed in blocks for returning results. All three live in files that are read through the operating system's page cache, so we keep the JVM heap moderate, usually no more than half of RAM and below the compressed oops threshold, and leave the rest of memory to the cache. Disabling doc values or _source saves disk but removes aggregations on that field or the ability to reindex and update from source.
What to remember
In this answer, we saw that Lucene stores data in three shapes: the inverted index for finding documents by term, columnar doc values for sorting, aggregations and scripts, and compressed stored fields, the _source, for returning and reprocessing documents. Because all three are files served by the page cache, the heap stays at most about half of RAM and below the compressed oops threshold. Disk sizing follows from storing data several times, and disabling doc values or _source saves space only by giving up aggregations or reindexing and updates from source.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Lucene keeps three different structures per field for three different jobs: the inverted index maps terms to documents for searching, doc values store each document's value in a columnar on-disk format for sorting, aggregations and scripts, and stored fields keep the original document, the _source in Elasticsearch, compressed in blocks for returning results. All three live in files that are read through the operating system's page cache, so we keep the JVM heap moderate, usually no more than half of RAM and below the compressed oops threshold, and leave the rest of memory to the cache. Disabling doc values or _source saves disk but removes aggregations on that field or the ability to reindex and update from source.
Why this question is asked
Sizing an Elasticsearch or OpenSearch node puzzles many teams: why not give the JVM all 64 GB of RAM, and why does the index take more disk than the raw JSON? The answers come straight from how Lucene stores each field. Interviewers ask this at senior level because it connects internals to decisions with real cost, such as hardware, mappings and what features we can offer. In this answer, we will look at each of the three structures, see which operations use which, and then use that to explain memory and disk sizing and the trade-offs of turning structures off.
1Three Structures for Three Questions
'Which documents contain this term?' is answered by the inverted index, which is organised by term. 'What is this document's price?' for a million matching documents at once, to sort or aggregate them, is answered by doc values, which are organised by document. 'What did this document look like when it was indexed?' to show it in results is answered by stored fields. Each needs data arranged differently, so Lucene stores it more than once.
For a typical keyword field in Elasticsearch, all three are on by default: the field is indexed, has doc values, and is part of _source. A text field is indexed and is in _source but has no doc values, because analysed text is not something we sort by.
| Structure | Organised by | Used for | Read from |
|---|---|---|---|
| Inverted index | term -> doc ids | match, filter, full-text search | files via page cache |
| Doc values | doc id -> value (columnar) | sort, aggregations, scripts | files via page cache |
| Stored fields / _source | doc id -> original JSON | returning hits, reindex, update | compressed blocks on disk |
2The Inverted Index
The inverted index holds a term dictionary, the sorted list of all terms in a field, and for each term a postings list of the documents containing it, optionally with positions for phrase queries and frequencies for scoring. A query for category:kitchen looks up kitchen and reads its postings. This is fast for finding documents and useless for the reverse question. To find the price of document 17 from an inverted index, we would have to scan every term to see which one lists document 17.
3Doc Values: a Column Store on Disk
Doc values solve the reverse question. For each field, they store the values of all documents in a segment one after another, in doc id order, like a column in a column-oriented database. Numbers are packed with compression such as storing differences from a minimum, and keyword values are stored as ordinals, small integers pointing into a sorted dictionary of the distinct values. To sort a million hits by price, Lucene reads the price column for those doc ids, sequentially and cheaply.
Doc values live in files on disk, not on the JVM heap. When an aggregation reads them repeatedly, the operating system keeps those file pages in its page cache, the part of free RAM the kernel uses to cache file contents. So aggregations are fast when the doc values fit in the page cache and slow when they must be read from disk on every request. This is the first key fact for sizing.
4Stored Fields and _source
Stored fields keep values exactly as they were indexed so they can be returned. Elasticsearch stores the whole original JSON document as one stored field called _source. Stored fields are grouped into blocks of several documents and each block is compressed, LZ4 by default and a stronger codec with index.codec set to best_compression, which uses more CPU to read. Fetching the ten hits on a results page decompresses only the blocks containing those ten documents.
_source is used for more than displaying results. The update API reads it to apply partial updates, the reindex API copies documents from it, and highlighting usually reads from it. That is why disabling it is a serious decision.
5How This Explains Memory Sizing
The JVM heap holds the things that must be objects in memory: cluster state, indexing buffers, caches of query results, per-shard overhead and the working memory of searches and aggregations. The bulk of the data, the inverted index, doc values and stored fields, is in files and is served by the page cache outside the heap. If we gave the heap 90 percent of RAM, there would be almost nothing left for the page cache, and every search would hit the disk.
That is the reason for the usual guidance: the heap should be at most about half of the machine's RAM, and below the compressed oops threshold, the point near 32 GB above which the JVM can no longer use compressed 32-bit object pointers and every reference doubles in size. Many teams settle around 26 to 30 GB at most, and recent Elasticsearch versions size the heap automatically from the node's role and memory. On a 64 GB node, about 30 GB of heap and 34 GB for the page cache is a typical split. If searches and aggregations are slow while heap usage looks fine, the page cache is too small for the hot data, and the fix is more RAM or less data per node, not a bigger heap.
- JVM heap, about 30 GBcluster state, buffers, caches, aggregation working memory
- Page cache, the rest of RAMhot segment files: postings, doc values, stored fields
- Diskall segment files, read into the page cache on demand
3 layers, from the top down. Step through them one at a time.
Heap below the compressed oops threshold; the page cache does the heavy lifting for files.
6How This Explains Disk Sizing, and What Turning Things Off Costs
Since every keyword field may be stored up to three times, an index often takes as much disk as the raw JSON, or more, before replicas, and each replica doubles it again. Deleted documents waiting for merges, and the temporary space merges need, add more. So disk sizing starts from measured index size per million documents, times the number of copies, plus headroom of 20 to 30 percent for merges and growth.
We can save disk by switching structures off, but each one removes a feature. Setting doc_values to false on a field saves its column, and that field can no longer be sorted, aggregated or used efficiently in scripts. Setting index to false saves postings, and the field can no longer be searched (on recent versions some field types can still be queried slowly from doc values; check your version). Disabling _source saves the most, often a large share of the index, but we lose the update API, reindexing from the index itself, and showing the original document, so a mapping change then requires re-feeding everything from the source system. Recent Elasticsearch versions offer a synthetic _source that rebuilds documents from doc values to save space; its availability depends on version and licence.
7Follow-up questions
Why are text fields not aggregatable by default?
Because text fields have no doc values; their analysed terms are not a single value per document. Aggregating on them would require fielddata, an in-heap structure built from the inverted index, which is disabled by default because it can use huge amounts of heap. The usual solution is a keyword sub-field, such as title.keyword, which has doc values.
What happens if the page cache is too small?
Searches and aggregations still work, but they read segment files from disk instead of memory, so latency rises sharply and disk I/O becomes the bottleneck. The node may look healthy from the JVM's point of view. Adding RAM, adding nodes so each holds less data, or reducing the data that queries touch fixes it.
8Conclusion
In this answer, we saw that Lucene stores data in three shapes: the inverted index for finding documents by term, columnar doc values for sorting, aggregations and scripts, and compressed stored fields, the _source, for returning and reprocessing documents. Because all three are files served by the page cache, the heap stays at most about half of RAM and below the compressed oops threshold. Disk sizing follows from storing data several times, and disabling doc values or _source saves space only by giving up aggregations or reindexing and updates from source.
Open with
Inside Lucene, each field can be stored in up to three structures. The inverted index maps terms to postings lists of documents, and it serves matching and filtering.
Then, if they want more
Doc values are a columnar, on-disk store of each document's value per field, so they serve sorting, aggregations and scripts, which need to go from a document to its value.
Stored fields keep the original values, and in Elasticsearch the whole document is kept as the _source stored field, compressed in blocks, which is what we return in hits and what update and reindex read. All of these are files on disk, and they are fast only when the operating system page cache holds the hot parts.
That is why I keep the JVM heap moderate, at most about half of RAM and below the compressed oops threshold near 32 GB, typically around 30 GB on a 64 GB node, and leave the rest to the page cache. If aggregations are slow while heap looks fine, I suspect the page cache, not the heap.
For disk, the same field may exist three times plus replicas, plus deleted documents and merge headroom, so I size from measured index size.
I can disable doc values on fields I never sort or aggregate, or disable _source to save a lot of disk, but then I lose aggregations on those fields, or the update and reindex APIs, so I only do that for data I can re-feed from the source system.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Elasticsearch documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how doc values, stored fields and the inverted index differ inside Lucene, and...": https://scrivio-interview-library.netlify.app/databases/search-engines/028ea0bcca/