# Script for adding new object

**URL:** <https://discuss.elastic.co/t/script-for-adding-new-object/7677>\
**Category:** Elasticsearch\
**Created:** [May 14, 2012, 9:30am UTC](https://discuss.elastic.co/t/script-for-adding-new-object/7677 "2012-05-14T09:30:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerome](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Jerome](https://discuss.elastic.co/u/Jerome)\
**Post date:** [May 14, 2012, 9:30am UTC](https://discuss.elastic.co/t/script-for-adding-new-object/7677/1 "2012-05-14T09:30:20Z")

</div>

Hi everyone,

I've a little question about scripting i've try to make a script who  
adds a new object if it doesn't exist but doesn't works.

{  
object {  
tag : value,  
tag2 : value  
},  
tag : value,

```
object2 { tag : value }

```

}

I want update this things. I want create a new object if doesn't exist  
and add the new tag/value into but if object exist i just want add the  
tag : value.

For example i've : $myObject = "foo"; $tag = "bar"; $value = "val";

I want create the sructure foo { bar : val } and add it into my  
index.

I've try this script but doesn't works :

if(ctx.\_source.$myObject){  
if(ctx.\_source.$myObject["$tag"] == null){  
ctx.\_source.$myObject["$tag"] = "$value"  
}  
else {  
ctx.\_source.$myObject["$tag"] += " $value"  
}  
}  
else {  
ctx.\_source.$myObject["$tag"] = "$value"  
}

So i don't understand why it doesn't work. Someone ?

---

<div class="post-metadata">

**Author:** ![Jerome](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Jerome](https://discuss.elastic.co/u/Jerome)\
**Post date:** [May 14, 2012, 9:34am UTC](https://discuss.elastic.co/t/script-for-adding-new-object/7677/2 "2012-05-14T09:34:33Z")

</div>

Ah, wrong backslash into this code, but it's not the problem.

On 14 mai, 11:30, Jérome [pivertjer...@gmail.com](mailto:pivertjer...@gmail.com) wrote:

> Hi everyone,
> 
> I've a little question about scripting i've try to make a script who  
> adds a new object if it doesn't exist but doesn't works.
> 
> {  
> object {  
> tag : value,  
> tag2 : value  
> },  
> tag : value,
> 
> ```
> object2 { tag : value }
> 
> ```
> 
> }
> 
> I want update this things. I want create a new object if doesn't exist  
> and add the new tag/value into but if object exist i just want add the  
> tag : value.
> 
> For example i've : $myObject = "foo"; $tag = "bar"; $value = "val";
> 
> I want create the sructure foo { bar : val } and add it into my  
> index.
> 
> I've try this script but doesn't works :
> 
> if(ctx.\_source.$myObject){  
> if(ctx.\_source.$myObject["$tag"] == null){  
> ctx.\_source.$myObject["$tag"] = "$value"  
> }  
> else {  
> ctx.\_source.$myObject["$tag"] += " $value"  
> }}
> 
> else {  
> ctx.\_source.$myObject["$tag"] = "$value"
> 
> }
> 
> So i don't understand why it doesn't work. Someone ?

---

<div class="post-metadata">

**Author:** ![Jerome](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Jerome](https://discuss.elastic.co/u/Jerome)\
**Post date:** [May 14, 2012, 2:18pm UTC](https://discuss.elastic.co/t/script-for-adding-new-object/7677/3 "2012-05-14T14:18:35Z")

</div>

I want to know if it's possible first ^^

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 15, 2012, 8:49pm UTC](https://discuss.elastic.co/t/script-for-adding-new-object/7677/4 "2012-05-15T20:49:44Z")

</div>

Yes, it should be possible. If you have problems with the default mvel  
lang, install the javascript plugin and use it instead.

On Mon, May 14, 2012 at 5:18 PM, Jérome [pivertjerome@gmail.com](mailto:pivertjerome@gmail.com) wrote:

> I want to know if it's possible first ^^

---

<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 6, 2017, 3:28am UTC](https://discuss.elastic.co/t/script-for-adding-new-object/7677/5 "2017-07-06T03:28:39Z")

</div>


