# What is way to approach "INSERT ON DUPLICATE KEY UPDATE" in ES

**URL:** https://discuss.elastic.co/t/what-is-way-to-approach-insert-on-duplicate-key-update-in-es/10426
**Category:** Elasticsearch
**Created:** [January 20, 2013, 2:43pm UTC](https://discuss.elastic.co/t/what-is-way-to-approach-insert-on-duplicate-key-update-in-es/10426 "2013-01-20T14:43:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![haizaar](https://avatars.discourse-cdn.com/v4/letter/h/3d9bf3/32.png) [@haizaar](https://discuss.elastic.co/u/haizaar)
#### Post date: [January 20, 2013, 2:43pm UTC](https://discuss.elastic.co/t/what-is-way-to-approach-insert-on-duplicate-key-update-in-es/10426/1 "2013-01-20T14:43:14Z")

</div>

Good day,

I have the following "article" document:  
{  
"article\_text" : { "type": "string" },  
"found\_at\_urls": { "type": "string" },  
}

I generate \_id myself by hashing the article\_text.

Article can appear at different urls and I would like to store all of the  
urls it appeared at in the "found\_at\_urls" field (its usually more then 2-5  
urls).

In SQL I would do INSERT... ON DUPLICATE KEY UPDATE...

How can approach this problem in ElasticSearch? Preferably in bulk mode and  
support within PyES.

Thank you,  
Zaar

--

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [January 20, 2013, 5:31pm UTC](https://discuss.elastic.co/t/what-is-way-to-approach-insert-on-duplicate-key-update-in-es/10426/2 "2013-01-20T17:31:49Z")

</div>

The default behavior in Elasticsearch is to re-insert a document if the id  
is already found. This operation is not an update: the previous document  
with that id will be deleted and the new one will be inserted  
(atomically). See operation type:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Sounds like you want the update API:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update.html) However, the  
bulk API does not allow updates  
[using bulk API with update (using scripts) in elasticsearch · Issue #1985 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1985) Since an update  
is still an atomic delete-create, there is no benefit than re-adding a  
document. Hopefully your workflow allows you access to create an entire  
document.

Cheers,

Ivan

On Sun, Jan 20, 2013 at 6:43 AM, Zaar Hai [haizaar@gmail.com](mailto:haizaar@gmail.com) wrote:

> Good day,
> 
> I have the following "article" document:  
> {  
> "article\_text" : { "type": "string" },  
> "found\_at\_urls": { "type": "string" },  
> }
> 
> I generate \_id myself by hashing the article\_text.
> 
> Article can appear at different urls and I would like to store all of the  
> urls it appeared at in the "found\_at\_urls" field (its usually more then 2-5  
> urls).
> 
> In SQL I would do INSERT... ON DUPLICATE KEY UPDATE...
> 
> How can approach this problem in Elasticsearch? Preferably in bulk mode  
> and support within PyES.
> 
> Thank you,  
> Zaar
> 
> --

--

---

<div class="post-metadata">

### Author: ![haizaar](https://avatars.discourse-cdn.com/v4/letter/h/3d9bf3/32.png) [@haizaar](https://discuss.elastic.co/u/haizaar)
#### Post date: [January 20, 2013, 5:59pm UTC](https://discuss.elastic.co/t/what-is-way-to-approach-insert-on-duplicate-key-update-in-es/10426/3 "2013-01-20T17:59:36Z")

</div>

So if I have 10 millions of articles to index, and I can not calculate  
their URLs in advance, I need to use update API (with upserts obviously)  
and there is currently no way to do it in a bulk mode. Right?

Thanks,  
Zaar

On Sunday, January 20, 2013 7:31:49 PM UTC+2, Ivan Brusic wrote:

> The default behavior in Elasticsearch is to re-insert a document if the id  
> is already found. This operation is not an update: the previous document  
> with that id will be deleted and the new one will be inserted  
> (atomically). See operation type:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html)
> 
> Sounds like you want the update API:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update.html) However, the  
> bulk API does not allow updates  
> [using bulk API with update (using scripts) in elasticsearch · Issue #1985 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1985) Since an  
> update is still an atomic delete-create, there is no benefit than re-adding  
> a document. Hopefully your workflow allows you access to create an entire  
> document.
> 
> Cheers,
> 
> Ivan
> 
> On Sun, Jan 20, 2013 at 6:43 AM, Zaar Hai \<[hai...@gmail.com](mailto:hai...@gmail.com) \<javascript:\>\>wrote:
> 
> > Good day,
> > 
> > I have the following "article" document:  
> > {  
> > "article\_text" : { "type": "string" },  
> > "found\_at\_urls": { "type": "string" },  
> > }
> > 
> > I generate \_id myself by hashing the article\_text.
> > 
> > Article can appear at different urls and I would like to store all of the  
> > urls it appeared at in the "found\_at\_urls" field (its usually more then 2-5  
> > urls).
> > 
> > In SQL I would do INSERT... ON DUPLICATE KEY UPDATE...
> > 
> > How can approach this problem in Elasticsearch? Preferably in bulk mode  
> > and support within PyES.
> > 
> > Thank you,  
> > Zaar
> > 
> > --

--

---

<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:55am UTC](https://discuss.elastic.co/t/what-is-way-to-approach-insert-on-duplicate-key-update-in-es/10426/4 "2017-07-06T02:55:29Z")

</div>


