# Upsert Examples

**URL:** https://discuss.elastic.co/t/upsert-examples/10115
**Category:** Elasticsearch
**Created:** [December 19, 2012, 5:11am UTC](https://discuss.elastic.co/t/upsert-examples/10115 "2012-12-19T05:11:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![avin](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@avin](https://discuss.elastic.co/u/avin)
#### Post date: [December 19, 2012, 5:11am UTC](https://discuss.elastic.co/t/upsert-examples/10115/1 "2012-12-19T05:11:02Z")

</div>

Newbie here.  
I am trying to create an index using upsert

curl -XPOST 'localhost:9200/metadata/load/ind1/\_update' -d '{

> "script" : "ctx.op = "none"",  
> "upsert" : {  
> doc: {"Key1" : "Value1"}  
> }  
> }'

Output:  
{"ok":true,"\_index":"metadata","\_type":"load","\_id":"ind1","\_version":1}

Now what I actually want to do is to add another Value to the index.

curl -XPOST 'localhost:9200/metadata/load/ind1/\_update' -d '{

> "script" : "ctx.op = "none"",  
> "upsert" : {  
> doc: {"Key2" : "Value2"}  
> }  
> }'

However, the source doc doesn't get updated with these new values.

What am I doing wrong here.

--

---

<div class="post-metadata">

### Author: ![avin](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@avin](https://discuss.elastic.co/u/avin)
#### Post date: [December 19, 2012, 5:14am UTC](https://discuss.elastic.co/t/upsert-examples/10115/2 "2012-12-19T05:14:06Z")

</div>

When I try this:

curl -XPOST 'localhost:9200/metadata/load/ind1/\_update' -d '{  
doc: {"Key3" : "Value3"},  
"upsert" : {  
}  
}'

I find that the source actually got updated.

"\_source" : {"doc":{"Key1":"Value1"},"Key2":"Value2","Key3":"Value3"}}]}}

On Tuesday, December 18, 2012 9:11:02 PM UTC-8, [avins...@gmail.com](mailto:avins...@gmail.com) wrote:

> Newbie here.  
> I am trying to create an index using upsert
> 
> curl -XPOST 'localhost:9200/metadata/load/ind1/\_update' -d '{
> 
> > "script" : "ctx.op = "none"",  
> > "upsert" : {  
> > doc: {"Key1" : "Value1"}  
> > }  
> > }'
> 
> Output:  
> {"ok":true,"\_index":"metadata","\_type":"load","\_id":"ind1","\_version":1}
> 
> Now what I actually want to do is to add another Value to the index.
> 
> curl -XPOST 'localhost:9200/metadata/load/ind1/\_update' -d '{
> 
> > "script" : "ctx.op = "none"",  
> > "upsert" : {  
> > doc: {"Key2" : "Value2"}  
> > }  
> > }'
> 
> However, the source doc doesn't get updated with these new values.
> 
> What am I doing wrong here.

--

---

<div class="post-metadata">

### Author: ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)
#### Post date: [December 19, 2012, 8:02am UTC](https://discuss.elastic.co/t/upsert-examples/10115/3 "2012-12-19T08:02:47Z")

</div>

Hello,

AFAIK, what you put in "upsert" is done only if the document doesn't exist.

For example, if you simply want to add a new field to an existing doc, this  
should work:

curl -XPOST localhost:9200/test/test/1/\_update -d '  
{  
"script": "ctx.\_source.field1 = "value1"",  
}'

But that would fail if the document is missing. So just in case it's not  
there, you can add an initial doc in the upsert section. For example:

curl -XPOST localhost:9200/test/test/3/\_update -d '  
{  
"script": "ctx.\_source.field1 = "value1"",  
"upsert": {  
"field1": "value1",  
"field2": "value2"  
}  
}'

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Wed, Dec 19, 2012 at 7:14 AM, [avinsemail@gmail.com](mailto:avinsemail@gmail.com) wrote:

> When I try this:
> 
> curl -XPOST 'localhost:9200/metadata/load/ind1/\_update' -d '{  
> doc: {"Key3" : "Value3"},  
> "upsert" : {  
> }  
> }'
> 
> I find that the source actually got updated.
> 
> "\_source" : {"doc":{"Key1":"Value1"},"Key2":"Value2","Key3":"Value3"}}]}}
> 
> On Tuesday, December 18, 2012 9:11:02 PM UTC-8, [avins...@gmail.com](mailto:avins...@gmail.com) wrote:
> 
> > Newbie here.  
> > I am trying to create an index using upsert
> > 
> > curl -XPOST 'localhost:9200/metadata/load/\*\*ind1/\_update' -d '{
> > 
> > > "script" : "ctx.op = "none"",  
> > > "upsert" : {  
> > > doc: {"Key1" : "Value1"}  
> > > }  
> > > }'
> > 
> > Output:  
> > {"ok":true,"\_index":"metadata" **,"\_type":"load","\_id":"ind1","**  
> > \_version":1}
> > 
> > Now what I actually want to do is to add another Value to the index.
> > 
> > curl -XPOST 'localhost:9200/metadata/load/\*\*ind1/\_update' -d '{
> > 
> > > "script" : "ctx.op = "none"",  
> > > "upsert" : {  
> > > doc: {"Key2" : "Value2"}  
> > > }  
> > > }'
> > 
> > However, the source doc doesn't get updated with these new values.
> > 
> > What am I doing wrong here.
> > 
> > --

--

---

<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, 2:59am UTC](https://discuss.elastic.co/t/upsert-examples/10115/4 "2017-07-06T02:59:19Z")

</div>


