# Determine Nested Object Count in Elasticsearch Document

**URL:** <https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648>\
**Category:** Elastic Search\
**Tags:** datastreams, elastic-site-search\
**Created:** [March 4, 2024, 1:25pm UTC](https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648 "2024-03-04T13:25:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaushal](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@Kaushal](https://discuss.elastic.co/u/Kaushal)\
**Post date:** [March 4, 2024, 1:25pm UTC](https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648/1 "2024-03-04T13:25:58Z")

</div>

Hello, Elasticsearch community,

I'm currently working with Elasticsearch and have a scenario where I need to identify the count of nested objects within a document. The document structure includes nested fields, and I'm looking for an efficient way to retrieve the number of nested objects.

Could anyone please provide guidance or share any Elasticsearch queries or approaches that can help me achieve this? I want to accurately determine the count of nested objects within a given document.

Thank you in advance for your assistance!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 4, 2024, 4:02pm UTC](https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648/2 "2024-03-04T16:02:07Z")

</div>

Welcome!

The best thing to do IMO is to compute that on your side before indexing the document:

```auto
{
  "foo": [{"bar":"baz"},{"bar":"baz"}],
  "foo_length": 2
}

```

If you can't do that at index time, you can use runtime fields and write a script which computes that. See [Runtime fields | Elasticsearch Guide [8.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html)

But this will be slower than the 1st approach.

---

<div class="post-metadata">

**Author:** ![Kaushal](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@Kaushal](https://discuss.elastic.co/u/Kaushal)\
**Post date:** [March 5, 2024, 2:32pm UTC](https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648/3 "2024-03-05T14:32:25Z")

</div>

Thanks!  
It won't be possible on our end before indexing the document.  
Can we check the nested object count for an existing document?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 5, 2024, 7:20pm UTC](https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648/4 "2024-03-05T19:20:00Z")

</div>

It's that for a single document? Would you like to search on that field? Sort by that field? Or just display that information?

---

<div class="post-metadata">

**Author:** ![Kaushal](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@Kaushal](https://discuss.elastic.co/u/Kaushal)\
**Post date:** [March 11, 2024, 12:32pm UTC](https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648/5 "2024-03-11T12:32:16Z")

</div>

Yes, it is for a single document, and we would like to search on that field and display that information.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 11, 2024, 12:58pm UTC](https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648/6 "2024-03-11T12:58:54Z")

</div>

So it's something you want to have for every document in the index. Not a single one.

As you can't modify the existing dataset, I'm pasting here the answer I did already which I hope could help you:

> [@dadoonet](#):
>
> If you can't do that at index time, you can use runtime fields and write a script which computes that. See [Runtime fields | Elasticsearch Guide [8.12] | Elastic ](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html)

But again, please be aware that this will be slower than then indexing the size of the array.

I created a video in the past which covers that topic. You might want to look at it:

[![](https://us1.discourse-cdn.com/elastic/original/3X/8/f/8fb84e0caace5fa2473f42b31daca8c6ac19a24c.jpeg "Runtime fields - Daily Elastic Byte S05E04") ](https://www.youtube.com/watch?v=HpjxfS5rrhI)

The demo code is available at

> **[GitHub - dadoonet/s0504-runtime-fields: Demo scripts used for Elastic Daily Bytes S05 -...](https://github.com/dadoonet/s0504-runtime-fields)**
>
> Demo scripts used for Elastic Daily Bytes S05 - Runtime fields

---

<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 8, 2024, 12:59pm UTC](https://discuss.elastic.co/t/determine-nested-object-count-in-elasticsearch-document/354648/7 "2024-04-08T12:59:10Z")

</div>

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