# How does es fetch the right source with stale docIDs, after lucene merged queried segments

**URL:** <https://discuss.elastic.co/t/how-does-es-fetch-the-right-source-with-stale-docids-after-lucene-merged-queried-segments/261511>\
**Category:** Elasticsearch\
**Created:** [January 19, 2021, 8:34am UTC](https://discuss.elastic.co/t/how-does-es-fetch-the-right-source-with-stale-docids-after-lucene-merged-queried-segments/261511 "2021-01-19T08:34:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsop\_479](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@vsop\_479](https://discuss.elastic.co/u/vsop_479)\
**Post date:** [January 19, 2021, 8:34am UTC](https://discuss.elastic.co/t/how-does-es-fetch-the-right-source-with-stale-docids-after-lucene-merged-queried-segments/261511/1 "2021-01-19T08:34:49Z")

</div>

Deleted documents are dropped when segments are merged, which will change the follows' docID.  
so how does es fetch the right source with stale docIDs.

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [January 19, 2021, 5:09pm UTC](https://discuss.elastic.co/t/how-does-es-fetch-the-right-source-with-stale-docids-after-lucene-merged-queried-segments/261511/2 "2021-01-19T17:09:31Z")

</div>

It keeps hold of the old segments until all the searches that are using them have finished.

---

<div class="post-metadata">

**Author:** ![vsop\_479](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@vsop\_479](https://discuss.elastic.co/u/vsop_479)\
**Post date:** [January 20, 2021, 2:57am UTC](https://discuss.elastic.co/t/how-does-es-fetch-the-right-source-with-stale-docids-after-lucene-merged-queried-segments/261511/3 "2021-01-20T02:57:43Z")

</div>

How to hold？  
I don't find any filter to skip segments using by searches in findMerges source code.

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [January 20, 2021, 8:09am UTC](https://discuss.elastic.co/t/how-does-es-fetch-the-right-source-with-stale-docids-after-lucene-merged-queried-segments/261511/4 "2021-01-20T08:09:19Z")

</div>

It works because `org.apache.lucene.index.IndexReader` is ref-counted, and we use the same `IndexReader` for all phases of a search.

---

<div class="post-metadata">

**Author:** ![vsop\_479](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@vsop\_479](https://discuss.elastic.co/u/vsop_479)\
**Post date:** [January 21, 2021, 2:13am UTC](https://discuss.elastic.co/t/how-does-es-fetch-the-right-source-with-stale-docids-after-lucene-merged-queried-segments/261511/5 "2021-01-21T02:13:32Z")

</div>

Thanks, it's resolved my puzzle.

---

<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:** [February 18, 2021, 2:13am UTC](https://discuss.elastic.co/t/how-does-es-fetch-the-right-source-with-stale-docids-after-lucene-merged-queried-segments/261511/6 "2021-02-18T02:13:33Z")

</div>

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