Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What are Lucene segments, and why are deletes and updates not applied in place?

A Lucene index is a set of immutable segments, each a small, complete inverted index of the documents written into it. Because a segment is never changed after it is written, a delete only marks the document as dead in a live-docs bitset, and an update is a delete of the old copy plus a new copy in a new segment. Background merges later combine segments and drop the dead documents; until then, deleted documents still cost disk space and search time.

Why this question is asked

Elasticsearch and OpenSearch are built on Lucene, so many things we see in production, such as disk usage that grows after a big update job, refreshes that make new data visible, or merges that use I/O in the background, come from how Lucene stores data. Interviewers ask about segments because the answer explains why search engines behave so differently from a database table. In this answer, we will recap the inverted index, see what a segment is, follow a delete and an update, look at merging, and understand why this design was chosen and what it costs us.

1A Quick Recap of the Inverted Index

Search needs to answer 'which documents contain the word kettle?' quickly. A table scan would read every document. Instead, Lucene builds an inverted index: for every term, a word after analysis such as lowercasing, it stores the sorted list of ids of the documents that contain it, called a postings list. Searching for a term means looking it up in the term dictionary and reading its postings list.

Inside Lucene, each document gets an internal doc id, a small integer such as 0, 1, 2. Postings lists are lists of these ids, compressed tightly because they are sorted. That compression, and the speed that comes with it, depends on the lists not changing. Keep that in mind, because it explains the rest of the design.

2What a Segment Is

Lucene does not maintain one giant inverted index that it edits. New documents are buffered in memory, and when the buffer is flushed, Lucene writes them as a new segment: a self-contained mini index with its own term dictionary, postings, stored fields and doc values. An index is simply the list of its current segments, recorded in a small commit file. A search runs over every segment and combines the results.

Once written, a segment's files are never modified. In Elasticsearch, a refresh, every second by default, flushes the buffer into a new segment and opens it for searching, which is why new documents become visible within about a second. So an active index is constantly gaining small new segments.

An index made of segments
SegmentDocsDeleted (live-docs bitset)State
_01,000,00012,000large, from an earlier merge
_750,000800medium
_91,2000small, from a recent refresh
_a3000newest, written one second ago
Each segment is a complete inverted index; searches visit all of them and skip deleted docs.

3Following a Delete and an Update

Suppose we delete the product with id 42, which lives in segment _0. Lucene cannot remove doc 42 from _0's postings lists without rewriting the whole segment. Instead, it records the deletion in a live-docs bitset for that segment: one bit per document, set to 1 for live documents and cleared to 0 for deleted ones. The bitset is written as a small separate file next to the segment. Every search still finds the document in the postings, checks the bit, and skips it.

An update works the same way, because Lucene has no 'change this field' operation. Updating a document means marking the old copy deleted and indexing the full new copy, which goes into the in-memory buffer and then into a new segment. In Elasticsearch, even a partial update through the update API reads the whole document from _source, applies the change and indexes a complete new copy.

Java
Term id42 = new Term("id", "42");
writer.updateDocument(id42, newDoc); // marks the old doc deleted, adds newDoc to the buffer
writer.deleteDocuments(new Term("id", "7")); // only clears a bit in a live-docs bitset
DirectoryReader r = DirectoryReader.open(writer);
System.out.println(r.numDocs()); // live documents, e.g. 1,038,700
System.out.println(r.maxDoc()); // live plus deleted, e.g. 1,051,500
System.out.println(r.numDeletedDocs()); // 12,800: still on disk until merged away

Notice that maxDoc() keeps counting deleted documents. They still take disk space, still appear in postings lists that every query must walk, and still affect term statistics used for scoring until they are removed.

4Merging: Where Deleted Documents Disappear

If nothing else happened, an index would collect thousands of tiny segments and an ever-growing number of deleted documents. A background process called merging fixes both. The merge policy, by default the tiered merge policy, picks a few segments of similar size, reads their live documents and writes them into one new, larger segment. Deleted documents are simply not copied, so this is the moment their space is reclaimed. When the new segment is ready, Lucene switches the index to it and deletes the old files once no reader uses them.

Merges are where the cost of immutability is paid. They read and rewrite data, which uses disk I/O and CPU, and they need temporary extra disk space for the new segment while the old ones still exist. The merge policy also favours segments with many deletions, so heavily updated segments are cleaned up sooner.

Step throughFrom small segments to one clean segment
  1. Refresh writes small segments_9, _a, _b
  2. Updates mark old copies deletedbits cleared in live-docs
  3. Merge policy picks similar segmentsprefers ones with many deletes
  4. Copy only live docs into a new segmentdeleted docs are dropped here
  5. Switch readers, delete old filesspace is reclaimed

5 steps in order. Play them, or step through them one at a time.

5Why Immutability Is Worth It

The design looks wasteful until we see what it buys. Because segment files never change, readers need no locks: a search can read a segment while new documents are being written elsewhere, and a reader opened at one point sees a consistent view for as long as it lives. Caches stay valid too, both the operating system's page cache, which keeps recently read files in memory, and Lucene's own per-segment caches such as cached filter results, because the data under them cannot change.

Immutability also allows heavy compression, since the files can be laid out optimally once, and makes crash recovery simple: a segment is either fully written and listed in a commit, or it is ignored.

6What Many Updates Cost Us

The flip side shows up in update-heavy indices. A job that updates every product's stock count every few minutes produces a steady stream of deleted documents. Until merges catch up, the index is larger on disk than its live data, queries walk longer postings lists, and merges run more often, competing with searches and indexing for I/O. A common sign is an index whose docs.deleted count in the _cat/indices output is a large share of docs.count.

We reduce this by not updating documents that have not changed, by moving fast-changing fields into a separate small index, or by batching updates. A force merge, which merges an index down to a few segments, removes deletions completely, but it should only be used on indices that are no longer written to, such as last month's logs, because a huge merged segment on a live index will collect deletes that the policy merges away only slowly.

7Follow-up questions

Does a refresh make data durable?

No. A refresh makes new documents searchable by writing a new segment, but that segment may still be only in the operating system's file cache. Durability in Elasticsearch comes from the translog, a write-ahead log that is synced to disk before a write is acknowledged by default, and from Lucene commits, which Elasticsearch calls a flush.

Why can't Lucene just overwrite the bits of a field in place?

Because postings lists are compressed, sorted and shared across fields and terms, changing one document's terms would mean rewriting large parts of the segment, and readers would need locks while that happens. Lucene does support in-place updates of numeric doc values in a limited form, but the normal path for any change is delete plus re-add.

8Conclusion

In this answer, we saw that a Lucene index is a list of immutable segments, each a small inverted index. Deletes only clear a bit in a live-docs bitset, updates are a delete plus a new document in a new segment, and background merges combine segments and drop deleted documents. Immutability gives lock-free reads, stable caches and strong compression, while update-heavy workloads pay for it with extra disk, slower searches and merge I/O until merges catch up.

What to say in the interviewAbout 80 seconds aloud

Open with

A Lucene index is made of segments, and each segment is a complete, small inverted index with its own term dictionary and postings lists. Segments are immutable: once Lucene writes one, it never changes the files.

Then, if they want more

New documents are buffered and written as new segments, and in Elasticsearch a refresh every second does that and makes them searchable. Because segments cannot change, a delete just clears the document's bit in a live-docs bitset for its segment, and searches skip it.

An update is a delete of the old version plus indexing the full new version into a new segment, even for a partial update. The deleted documents are only really removed when background merges combine several segments into a new one and copy only the live documents.

The reason for this design is that immutable files need no locks for readers, give each reader a consistent view, let the page cache and per-segment caches stay valid, and allow heavy compression.

The cost is that many updates leave many deleted documents behind, so the index uses more disk and queries walk longer postings lists until merges run, and merges themselves use I/O. So for update-heavy data I avoid needless updates and only force merge indices that are no longer written.

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 "What are Lucene segments, and why are deletes and updates not applied in place?": https://scrivio-interview-library.netlify.app/databases/search-engines/f91bc595eb/