# Are nested docs efficiently skipped?

**URL:** <https://discuss.elastic.co/t/are-nested-docs-efficiently-skipped/271847>\
**Category:** Elasticsearch\
**Created:** [May 1, 2021, 1:51am UTC](https://discuss.elastic.co/t/are-nested-docs-efficiently-skipped/271847 "2021-05-01T01:51:30Z")\
**Posts on this page:** 6\
**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:** [May 1, 2021, 1:51am UTC](https://discuss.elastic.co/t/are-nested-docs-efficiently-skipped/271847/1 "2021-05-01T01:51:30Z")

</div>

Hi all,

I’m curious if anyone is able to comment on the handling of nested documents at query time. Are nested documents skipped over in the `advance` or `nextDoc` step, or must they also be considered if, for example, nested docs share some field names with the parent documents?

And somewhat relatedly, how does the ratio of root:nested documents affect performance?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [May 5, 2021, 10:26pm UTC](https://discuss.elastic.co/t/are-nested-docs-efficiently-skipped/271847/2 "2021-05-05T22:26:31Z")

</div>

Nested docs should not share the same field names as a parent doc, unless you do `include_in_parent` and `include_in_root`, which we for a long time are thinking to [deprecate](https://github.com/elastic/elasticsearch/issues/12461).

But if you ended up using these parameters, and have shared fields, then if you are not using `nested` query, the query will be run only on top level docs as we are internally rewriting this query to add an additional filter that will skip nested docs.

---

<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:** [May 6, 2021, 1:04am UTC](https://discuss.elastic.co/t/are-nested-docs-efficiently-skipped/271847/3 "2021-05-06T01:04:42Z")

</div>

Thanks for your reply and the info @mayya. In hindsight, I think my true question would have been more clear if I had omitted the part about shared fields.

To follow up: when searching an index with nested fields, can nested docs be efficiently skipped if there are no `nested` queries (i.e. the nested fields are not part of the query)? To what degree will performance be affected if the number of nested documents hugely outnumbers the number of parent documents?

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [May 6, 2021, 10:24am UTC](https://discuss.elastic.co/t/are-nested-docs-efficiently-skipped/271847/4 "2021-05-06T10:24:01Z")

</div>

I am not super clear what you mean by "efficiently skipped", but considering that you mentioned `advance` and `nextDoc`, I think you are talking about a postings list. If you are searching with query on a field "X", the postings list for this field will contain only documents that have this field. In your 2nd scenario this will be only be parent documents, so the number of nested documents is not relevant here, and doesn't have an impact on the speed of this query.

---

<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:** [May 6, 2021, 10:44am UTC](https://discuss.elastic.co/t/are-nested-docs-efficiently-skipped/271847/5 "2021-05-06T10:44:35Z")

</div>

Thanks @mayya, sorry about the confusing use of the wrong terminology. You’ve answered my question 🙂

---

<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:** [June 3, 2021, 10:45am UTC](https://discuss.elastic.co/t/are-nested-docs-efficiently-skipped/271847/6 "2021-06-03T10:45:25Z")

</div>

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