# Paginate nested objects of a document

**URL:** <https://discuss.elastic.co/t/paginate-nested-objects-of-a-document/52696>\
**Category:** Elasticsearch\
**Created:** [June 14, 2016, 7:36am UTC](https://discuss.elastic.co/t/paginate-nested-objects-of-a-document/52696 "2016-06-14T07:36:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmishra002](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@pmishra002](https://discuss.elastic.co/u/pmishra002)\
**Post date:** [June 14, 2016, 7:36am UTC](https://discuss.elastic.co/t/paginate-nested-objects-of-a-document/52696/1 "2016-06-14T07:36:53Z")

</div>

Is it possible to get the nested sub elements in a document in a paginated fashion. Example we have a document as below and we would know the document id

{  
"\_index": "index1",  
"\_type": "type1",  
"\_id": "81cabf3c-51f5-442a-b78d-4324c4344f51",  
"\_version": 1,  
"found": true,  
"\_source": {  
"chiild1" : "child1 value"  
"names": [  
{  
"name": "John"  
},  
{  
"name": "Walter"  
},  
{  
"name": "Justin"  
},  
{  
"name": "Cramer"  
}  
]  
}  
}

Can we run a query like below (It does not work but are there other options ?)

GET index1/type1/81cabf3c-51f5-442a-b78d-4324c4344f51?\_source\_include=[names.name](http://names.name)&start=0&size=2

1. 

{  
"\_index": "index1",  
"\_type": "type1",  
"\_id": "81cabf3c-51f5-442a-b78d-4324c4344f51",  
"\_version": 1,  
"found": true,  
"\_source": {  
"names": [  
{  
"name": "John"  
},  
{  
"name": "Walter"  
}  
]  
}  
}

1. 

GET index1/type1/81cabf3c-51f5-442a-b78d-4324c4344f51?\_source\_include=[names.name](http://names.name)&start=2&size=2

{  
"\_index": "index1",  
"\_type": "type1",  
"\_id": "81cabf3c-51f5-442a-b78d-4324c4344f51",  
"\_version": 1,  
"found": true,  
"\_source": {  
"names": [  
{  
"name": "Justin"  
},  
{  
"name": "Cramer"  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 14, 2016, 7:39am UTC](https://discuss.elastic.co/t/paginate-nested-objects-of-a-document/52696/2 "2016-06-14T07:39:11Z")

</div>

> [@pmishra002](#):
>
> Is it possible to get the nested sub elements in a document in a paginated fashion.

No as it's still a single document.  
Interesting idea though, maybe raise a FR on github?

---

<div class="post-metadata">

**Author:** ![pmishra002](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@pmishra002](https://discuss.elastic.co/u/pmishra002)\
**Post date:** [June 14, 2016, 8:15am UTC](https://discuss.elastic.co/t/paginate-nested-objects-of-a-document/52696/3 "2016-06-14T08:15:12Z")

</div>

Thanks for the response. I have created a FR on github - [https://github.com/elastic/elasticsearch/issues/18850](https://github.com/elastic/elasticsearch/issues/18850)

---

<div class="post-metadata">

**Author:** ![pmishra002](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@pmishra002](https://discuss.elastic.co/u/pmishra002)\
**Post date:** [June 14, 2016, 8:17am UTC](https://discuss.elastic.co/t/paginate-nested-objects-of-a-document/52696/4 "2016-06-14T08:17:01Z")

</div>

To add to the information the sub elements are of nested type.

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [June 14, 2016, 8:22am UTC](https://discuss.elastic.co/t/paginate-nested-objects-of-a-document/52696/5 "2016-06-14T08:22:01Z")

</div>

There are two ways to get access to nested objects via inner hits or using  
aggregations (using top\_hits aggregation inside a nested aggregation).

1. Via inner hits you can get access to the nested objects on a per  
document basis:

curl -XGET "[http://localhost:9200/\_search](http://localhost:9200/_search)" -d'  
{  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"\_id": "81cabf3c-51f5-442a-b78d-4324c4344f51"  
}  
},  
{  
"nested": {  
"path": "names",  
"query": {  
"match\_all": {}  
},  
"inner\_hits" : {  
"from" : 0,  
"size" : 5  
}  
}  
}  
]  
}  
}  
}'

[https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-inner-hits.html#nested-inner-hits](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-inner-hits.html#nested-inner-hits)

1. Via aggregations you can get access to nested documents too, but as  
individual documents:

curl -XGET "[http://localhost:9200/\_search](http://localhost:9200/_search)" -d'  
{  
"query": {  
"match": {  
"\_id": "81cabf3c-51f5-442a-b78d-4324c4344f51"  
}  
},  
"aggs": {  
"to-names": {  
"nested": {  
"path": "names"  
},  
"aggs": {  
"names": {  
"top\_hits": {  
"from": 0,  
"size": 5  
}  
}  
}  
}  
}  
}'

If the query on the id field wasn't specified inner objects of other  
documents would be returned too.

[https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-metrics-top-hits-aggregation.html#\_top\_hits\_support\_in\_a\_nested\_or\_reverse\_nested\_aggregator](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-metrics-top-hits-aggregation.html#_top_hits_support_in_a_nested_or_reverse_nested_aggregator)

---

<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:** [July 5, 2017, 10:44pm UTC](https://discuss.elastic.co/t/paginate-nested-objects-of-a-document/52696/6 "2017-07-05T22:44:02Z")

</div>


