# Copy\_to field in an update script with moving a object field to a nested field?

**URL:** <https://discuss.elastic.co/t/copy-to-field-in-an-update-script-with-moving-a-object-field-to-a-nested-field/217247>\
**Category:** Elasticsearch\
**Created:** [January 30, 2020, 4:57pm UTC](https://discuss.elastic.co/t/copy-to-field-in-an-update-script-with-moving-a-object-field-to-a-nested-field/217247 "2020-01-30T16:57:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JPVay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpvay/32/61768_2.png) [@JPVay](https://discuss.elastic.co/u/JPVay)\
**Post date:** [January 30, 2020, 4:57pm UTC](https://discuss.elastic.co/t/copy-to-field-in-an-update-script-with-moving-a-object-field-to-a-nested-field/217247/1 "2020-01-30T16:57:34Z")

</div>

version : 7.5  
Using JS API but same with REST API

making an update like :

> {  
> index : "myindex",  
> id : pid,  
> refresh : true,  
> body : {  
> "script" : {  
> "source" : " ctx.\_source.Histo.add(ctx.\_source.Actual); ctx.\_source.Actual = params;",  
> "params" : data  
> }  
> }  
> }

Actual is an object like

> {  
> name (copy\_to searchField),  
> email (copy\_to searchField),  
> searchField (type text && store)  
> }.

**Histo** is a nested object ( array of objects like the **Actual** one).

The update script add the **Actual** object to the **Histo** nested array.  
Everything is fine. I get a good result for a query like ... **match { Actual.searchField : "new name is John" }**... return this :

> {  
> Actual: {  
> name : "new name is John",  
> email : "new email" },  
> Histo : [  
> {  
> name : "my name is Peter",  
> email : "previous name"  
> } ,  
> {  
> name : "my veru first name was Kank",  
> email : "previous previous name"  
> }  
> ]  
> }

That's good, but a query like .... match : { Histo.searchField : "my name is Peter" } return nothing.  
The Histo.searchField is not copied from the Actual object ?

Any solution ?  
Thank you

---

<div class="post-metadata">

**Author:** ![JPVay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpvay/32/61768_2.png) [@JPVay](https://discuss.elastic.co/u/JPVay)\
**Post date:** [February 1, 2020, 11:30am UTC](https://discuss.elastic.co/t/copy-to-field-in-an-update-script-with-moving-a-object-field-to-a-nested-field/217247/2 "2020-02-01T11:30:17Z")

</div>

Resolved using "include\_in\_root": true for the nested Histo property

---

<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:** [February 29, 2020, 11:30am UTC](https://discuss.elastic.co/t/copy-to-field-in-an-update-script-with-moving-a-object-field-to-a-nested-field/217247/3 "2020-02-29T11:30:23Z")

</div>

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