# Flatten nested documents as part of re-index process (using painless)

**URL:** <https://discuss.elastic.co/t/flatten-nested-documents-as-part-of-re-index-process-using-painless/162450>\
**Category:** Elasticsearch\
**Created:** [December 30, 2018, 4:47pm UTC](https://discuss.elastic.co/t/flatten-nested-documents-as-part-of-re-index-process-using-painless/162450 "2018-12-30T16:47:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_es](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_es/32/53584_2.png) [@tom\_es](https://discuss.elastic.co/u/tom_es)\
**Post date:** [December 30, 2018, 4:47pm UTC](https://discuss.elastic.co/t/flatten-nested-documents-as-part-of-re-index-process-using-painless/162450/1 "2018-12-30T16:47:38Z")

</div>

Hi... we have a nested mapping, which we'd like to reindex into a non-nested structure -  
i.e. each nested object + the 'parent' document becomes a new document.

Is this possible by reindexing this structure using **painless**?

I tried to follow the guidelines from the URL below, but the problem is that by iterating the nested inner documents array within the parent, and assigning "ctx\_source" fields the relevant values from the inner object, each document overrides the previous, so that at the end of the update, we only get a single document with the values from the last nested document it holds.

here is an example of a painless code as we tried to use it -

> ```
> if (ctx._source.nestedObject instanceof List) {
> for (int i = 0; i < ctx._source.nestedObject.size(); i++) {
> ctx._source.text=ctx._source.nestedObject[i].text;
> ctx._source.lineNumber=ctx._source.nestedObjectinstanceof [i].lineNumber;
> ctx._source.someProperty=ctx._source.nestedObject [i].someProperty;
> }
> 
> ```

Is there a way to flatten the nested documents as we want? so that if for example, we have a 1 outer document with 10 nested documents inside, at the end of the reindex process, we will have 10 flatten documents?

> [@Possible to 'flatten' nested mapping with reindex?](https://discuss.elastic.co/t/possible-to-flatten-nested-mapping-with-reindex/134313):
>
> Hi... I have a nested mapping, which I'd like to reindex into a non-nested structure - i.e. each nested object + the 'parent' document becomes a new document. Is this possible by reindexing? I'd like to avoid reloading from the source ideally. I've had a good play about and a google but haven't come across any winning combination yet... Existing mapping is: { "mappings" : { "properties" : { "wsid" : { "type" : "keyword" }, "date" : { "type" : "date", "index" : false …

---

<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, 2019, 4:47pm UTC](https://discuss.elastic.co/t/flatten-nested-documents-as-part-of-re-index-process-using-painless/162450/2 "2019-01-27T16:47:44Z")

</div>

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