# Using source filtering to get nested object count

**URL:** https://discuss.elastic.co/t/using-source-filtering-to-get-nested-object-count/70221
**Category:** Elasticsearch
**Created:** [December 29, 2016, 3:48pm UTC](https://discuss.elastic.co/t/using-source-filtering-to-get-nested-object-count/70221 "2016-12-29T15:48:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![stevenmwall](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevenmwall/32/12199_2.png) [@stevenmwall](https://discuss.elastic.co/u/stevenmwall)
#### Post date: [December 29, 2016, 3:48pm UTC](https://discuss.elastic.co/t/using-source-filtering-to-get-nested-object-count/70221/1 "2016-12-29T15:48:40Z")

</div>

I have an index with documents like this:

> ```
> {
> "firstName": "Fred",
> "lastName": "Flinstone",
> "children": [
> {
> "firstName": "Pebbles",
> "lastName": "Flinstone",
> "age": 1
> }
> ]
> }
> 
> ```

When I do my search query, I want to get back just 3 fields: firstName, lastName and then the number of children. I am currently using Elasticsearch 5 and source filtering to include the firstName and lastName. The first two fields are not a problem, but how do I get a count of the nested children object to be returned?

In the end, I want my search result hits \_source to be something like this:

> ```
> "_source": {
> "firstName": "Fred",
> "lastName": "Flinstone",
> "numberOfChildren": 1
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [December 30, 2016, 8:52am UTC](https://discuss.elastic.co/t/using-source-filtering-to-get-nested-object-count/70221/2 "2016-12-30T08:52:22Z")

</div>

Hey,

source filtering is not a scripting language, but really just allows you to include or exclude parts of the JSON document - no more, no less.

You could either use scripting on query time to return the number of children or just add a field during indexing that contains the number of children.

If you however want to execute searches on the children and only get back those children that matched, you should take a look at the [inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-request-inner-hits.html) feature.

--Alex

---

<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: [January 27, 2017, 8:52am UTC](https://discuss.elastic.co/t/using-source-filtering-to-get-nested-object-count/70221/3 "2017-01-27T08:52:37Z")

</div>

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