Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How do Elasticsearch and OpenSearch make newly indexed documents searchable, and what do refresh, flush and the translog do? Why does a document written with a 200 OK not appear in search immediately?

A newly indexed document first goes into an in-memory indexing buffer and is appended to the translog; it becomes searchable only when a refresh turns the buffer into a new Lucene segment, by default about every second on indices that are being searched. The 200 OK means the write is durable in the translog, not that it is visible to search, although a GET by id can read it in real time. A flush performs a Lucene commit to disk and trims the translog, refresh=wait_for makes a write wait until it is visible, and raising refresh_interval during bulk loads speeds up indexing.

Why this question is asked

Developers who move from a relational database to Elasticsearch or OpenSearch often write a test that indexes a document and immediately searches for it, and the test fails at random. The behaviour is not a bug; it is the design that makes these engines fast at both writing and searching. Interviewers ask about refresh, flush and the translog because they explain near-real-time search, durability and indexing performance in one story. In this answer, we will recap Lucene segments, follow a document from the write to visibility, see what the translog and flush guarantee, and look at the options for waiting and for bulk loading.

1A Quick Recap of Lucene Segments

Each Elasticsearch or OpenSearch shard is a Lucene index, and a Lucene index is made of segments. A segment is a small, self-contained, immutable inverted index. Immutable means it is never changed after it is written: new documents go into new segments, deletes are recorded as markers, and a background process merges small segments into bigger ones.

Searching means searching every segment that the shard's current reader can see. Making a new segment visible requires opening a new reader, which is not free, so Lucene does it periodically rather than for every document. This is the root of near-real-time search.

2Following One Document From Write to Search

When we index a document, the primary shard parses and analyses it, adds it to an in-memory indexing buffer, and appends the operation to the translog, a write-ahead log on disk. The operation is then sent to the replica shards, which do the same. Once the required copies have it, the client gets its 200 OK or 201 Created.

Step throughIndexing and refresh
ClientPrimary shardReplica shardSearcher
index doc 42
add to buffer, append to translog
replicate operation
ack
201 Created
search for doc 42
no hits yet
refresh: buffer -> new segment
search again
doc 42 found

10 messages in order. Play them, or step through them one at a time.

The write is acknowledged before the refresh that makes it searchable.

Notice the gap between the acknowledgement and visibility. The document is in the buffer and the translog, but no searchable segment contains it yet. A refresh writes the buffer out as a new segment, usually into the filesystem cache rather than with a full fsync, and opens a new reader. By default refresh runs every second, but only on indices that have received a search in the last 30 seconds; an index that is not being searched skips periodic refreshes until the next search arrives (the search-idle behaviour, check your version).

3Seeing the Delay in Practice

We can reproduce the surprise with two requests sent back to back, and see the difference between search and a get by id.

Java
PUT /products/_doc/42
{ "name": "Wireless mouse" }
// 201 Created, "result": "created"
GET /products/_search?q=name:wireless
// "hits": { "total": { "value": 0 } } -> not refreshed yet
GET /products/_doc/42
// "found": true, "_source": { "name": "Wireless mouse" } -> get is real time
// about a second later
GET /products/_search?q=name:wireless
// "hits": { "total": { "value": 1 } }

The get by id succeeds because GET is real time by default: if the document is not yet in a searchable segment, the engine makes sure it can read the latest version, from the translog or by refreshing as needed. Search has no such shortcut, because it must look through the inverted index, and the document is not in any visible segment yet.

4What the Translog and Flush Guarantee

A refresh makes segments searchable, but it does not durably commit them. If a node crashed after a refresh, the newest segments might not be safely on disk. The translog covers this gap. By default (index.translog.durability: request) it is fsynced before the write is acknowledged, so every acknowledged operation can be replayed from it after a crash.

A flush performs a Lucene commit: it fsyncs all segments to disk and records them in a commit point, and then the translog entries those segments cover are no longer needed, so the translog is trimmed. Elasticsearch and OpenSearch flush automatically when the translog grows large, so we rarely call it by hand. In short: refresh is about visibility, the translog is about durability between commits, and flush is about making the commit and keeping the translog small.

5Waiting for Visibility, and Loading in Bulk

Sometimes a user must see their own write immediately, for example a newly created listing in 'my listings'. The write request accepts a refresh parameter for that.

Java
PUT /products/_doc/43?refresh=wait_for
{ "name": "Wired keyboard" }
// returns after the next scheduled refresh: the doc is now searchable
PUT /products/_doc/44?refresh=true
// forces an immediate refresh of the affected shards: expensive under load
PUT /products/_settings
{ "index": { "refresh_interval": "30s" } } // during a big bulk load; "-1" disables it
// after the load, set it back to "1s" (or null for the default)

refresh=wait_for keeps the normal refresh schedule and just delays the response, so it costs latency and throughput for that request. refresh=true forces a new segment on every such write, which creates many tiny segments and extra merge work, so it should be rare. For bulk loads, the opposite is useful: refreshing less often means fewer, larger segments and much faster indexing. Teams often also set replicas to 0 during an initial load and add them afterwards.

6Follow-up questions

Does a 200 OK mean my document is safe if the node crashes?

With the default translog durability of request, yes: the operation is fsynced to the translog before acknowledgement and is replayed on recovery. With durability set to async, the translog is fsynced on an interval, so a crash can lose the last few seconds of acknowledged writes.

Why not refresh after every write to make search real time?

Each refresh creates a segment and opens a new reader. Thousands of tiny segments make searches slower and merges heavier, so throughput drops sharply. One second is a balance between freshness and efficiency.

How should integration tests handle this?

Index test data with refresh=wait_for or call the _refresh API after setup, then search. Never add sleeps; they are slow and still flaky.

7Conclusion

In this answer, we followed a document through Elasticsearch or OpenSearch. It goes into an in-memory buffer and the translog, is acknowledged, and becomes searchable only when a refresh writes a new segment, about every second on actively searched indices, while a GET by id reads it in real time. The translog keeps acknowledged writes durable between Lucene commits, a flush performs the commit and trims the translog, refresh=wait_for waits for visibility while refresh=true forces it at high cost, and a longer refresh_interval speeds up bulk loads.

What to say in the interviewAbout 90 seconds aloud

Open with

When I index a document, the shard analyses it, puts it in an in-memory indexing buffer and appends the operation to the translog, then replicates it, and only then returns 200 or 201.

Then, if they want more

But search only sees Lucene segments that a reader has opened, and the document is not in a segment yet. A refresh writes the buffer out as a new searchable segment; by default that happens every second on indices that are actively being searched. So for up to about a second a search will not find it.

A get by id is different, it is real time, so it can return the document right away. Durability comes from the translog: by default it is fsynced before the write is acknowledged, so after a crash the engine replays it.

A flush is a Lucene commit, fsyncing segments to disk and then trimming the translog, and it happens automatically when the translog grows.

If a user must see their write immediately, I can index with refresh=wait_for, which waits for the next refresh at some latency cost. refresh=true forces a refresh and creates lots of small segments, so I avoid it under load.

For bulk loads I go the other way: set refresh_interval to 30 seconds or minus one, maybe replicas to zero, and restore them afterwards.

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 "How do Elasticsearch and OpenSearch make newly indexed documents searchable, and what do...": https://scrivio-interview-library.netlify.app/databases/search-engines/ca1da3ef28/