# When doing an update, create the object if it doesn't exist

**URL:** <https://discuss.elastic.co/t/when-doing-an-update-create-the-object-if-it-doesnt-exist/8624>\
**Category:** Elasticsearch\
**Created:** [August 3, 2012, 10:26am UTC](https://discuss.elastic.co/t/when-doing-an-update-create-the-object-if-it-doesnt-exist/8624 "2012-08-03T10:26:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_A](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_a/32/2788_2.png) [@Chris\_A](https://discuss.elastic.co/u/Chris_A)\
**Post date:** [August 3, 2012, 10:26am UTC](https://discuss.elastic.co/t/when-doing-an-update-create-the-object-if-it-doesnt-exist/8624/1 "2012-08-03T10:26:59Z")

</div>

Hi all

We are using ElasticSearch to track a counter. When an update arrives in  
our system we want to increment or decrement a field on a specific object.

It currently works well for removals, as we use the scripting functionality  
to decrement the counter then remove the object if it hits zero.

However, for increments, we have the issue where if we issue an update for  
an object that _does_ exist, it works correctly (and increments the value);  
however there seems to be no way to specify creating an object with that  
name if it doesn't exist.

Our current workaround is to do a create (with the op\_type=create argument)  
before each update, but it seems like a waste to do two requests when it  
would ideally be accomplished with only one.

Please see this gist with how we are performing the update and  
create/update at the moment: [https://gist.github.com/3246473](https://gist.github.com/3246473)

Does anyone have any thoughts on how it would be possible to do this update  
and "create if not exists" in one request? The update endpoint doesn't  
currently support PUT and I couldn't find anything in the docs.

Thanks

Chris

---

<div class="post-metadata">

**Author:** ![Benjamin\_Deveze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_deveze/32/1577_2.png) [@Benjamin\_Deveze](https://discuss.elastic.co/u/Benjamin_Deveze)\
**Post date:** [August 3, 2012, 10:31am UTC](https://discuss.elastic.co/t/when-doing-an-update-create-the-object-if-it-doesnt-exist/8624/2 "2012-08-03T10:31:30Z")

</div>

Hi,

the upsert feature has already been added to incoming ES 0.20 see  
[https://github.com/elasticsearch/elasticsearch/issues/2008](https://github.com/elasticsearch/elasticsearch/issues/2008) for more  
information

---

<div class="post-metadata">

**Author:** ![Chris\_A](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_a/32/2788_2.png) [@Chris\_A](https://discuss.elastic.co/u/Chris_A)\
**Post date:** [August 3, 2012, 10:34am UTC](https://discuss.elastic.co/t/when-doing-an-update-create-the-object-if-it-doesnt-exist/8624/3 "2012-08-03T10:34:50Z")

</div>

Excellent thanks.

Chris

On 3 August 2012 11:31, Benjamin Devèze [benjamin.deveze@gmail.com](mailto:benjamin.deveze@gmail.com) wrote:

> Hi,
> 
> the upsert feature has already been added to incoming ES 0.20 see  
> [Update API: Allow to upsert, provide a doc and index it if the doc does not exists · Issue #2008 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2008) for more  
> information

---

<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:17am UTC](https://discuss.elastic.co/t/when-doing-an-update-create-the-object-if-it-doesnt-exist/8624/4 "2017-07-06T03:17:53Z")

</div>


