# How does document update work under the hood?

**URL:** <https://discuss.elastic.co/t/how-does-document-update-work-under-the-hood/328392>\
**Category:** Elasticsearch\
**Created:** [March 23, 2023, 8:02pm UTC](https://discuss.elastic.co/t/how-does-document-update-work-under-the-hood/328392 "2023-03-23T20:02:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![egalpin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egalpin/32/79772_2.png) [@egalpin](https://discuss.elastic.co/u/egalpin)\
**Post date:** [March 23, 2023, 8:02pm UTC](https://discuss.elastic.co/t/how-does-document-update-work-under-the-hood/328392/1 "2023-03-23T20:02:17Z")

</div>

Hi all! I’m curious to learn about how the process of document update (and upsert/partial upsert) works under the hood.

I know that Lucene segments are immutable and that “deleting” a doc is a soft delete by way of tombstone marker. But how is a prior doc with the same doc ID _found_ in order to set its tombstone bit? Upon ingestion of a document with a given ID, is an actual query issued to the cluster to find the doc with that ID? Or is there an additional data structure of some kind used to help keep these kinds of lookups even faster?

I’m keen to gain a better understanding! Thanks!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 23, 2023, 8:10pm UTC](https://discuss.elastic.co/t/how-does-document-update-work-under-the-hood/328392/2 "2023-03-23T20:10:12Z")

</div>

The document ID decides which shard of the index the document resides in, so the update can directly be sent to the correct shard. Within the shard the document need to be found and the source retrieved before the update can be applied. The old document is then marked as replaced and the new updated document is written to the transaction log and later into a new segment.

---

<div class="post-metadata">

**Author:** ![egalpin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egalpin/32/79772_2.png) [@egalpin](https://discuss.elastic.co/u/egalpin)\
**Post date:** [March 23, 2023, 9:10pm UTC](https://discuss.elastic.co/t/how-does-document-update-work-under-the-hood/328392/3 "2023-03-23T21:10:32Z")

</div>

Thanks @Christian_Dahlqvist !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 20, 2023, 9:11pm UTC](https://discuss.elastic.co/t/how-does-document-update-work-under-the-hood/328392/4 "2023-04-20T21:11:12Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
