# Can I perform the equivalent of a MySQL transaction in ElasticSearch?

**URL:** <https://discuss.elastic.co/t/can-i-perform-the-equivalent-of-a-mysql-transaction-in-elasticsearch/9835>\
**Category:** Elasticsearch\
**Created:** [November 27, 2012, 5:53am UTC](https://discuss.elastic.co/t/can-i-perform-the-equivalent-of-a-mysql-transaction-in-elasticsearch/9835 "2012-11-27T05:53:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbrianjones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbrianjones/32/1402_2.png) [@tbrianjones](https://discuss.elastic.co/u/tbrianjones)\
**Post date:** [November 27, 2012, 5:53am UTC](https://discuss.elastic.co/t/can-i-perform-the-equivalent-of-a-mysql-transaction-in-elasticsearch/9835/1 "2012-11-27T05:53:44Z")

</div>

I have a document property in an ElasticSearch index called 'processing.'  
It's a boolean value that records whether or not an external program is  
running that is working with that document. I am running many external  
processors, but documents cannot be processed by multiple programs at once.

Am I able to select a document that is not being processed and update the  
'processing' property to TRUE without another processor sneaking in and  
choosing the document? Is there an UPDATE WHERE type operation I can  
perform that will return the \_id of the document that was updated? Can I  
perform transactions with ElasticSearch?

Is there an alternative or best practice way to accomplish this with  
ElasticSearch?

--

---

<div class="post-metadata">

**Author:** ![Klaus\_Brunner](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Klaus\_Brunner](https://discuss.elastic.co/u/Klaus_Brunner)\
**Post date:** [November 27, 2012, 6:27am UTC](https://discuss.elastic.co/t/can-i-perform-the-equivalent-of-a-mysql-transaction-in-elasticsearch/9835/2 "2012-11-27T06:27:35Z")

</div>

On Tuesday, 27 November 2012 06:53:44 UTC+1, Brian Jones wrote:

> Am I able to select a document that is not being processed and update the  
> 'processing' property to TRUE without another processor sneaking in and  
> choosing the document? Is there an UPDATE WHERE type operation I can  
> perform that will return the \_id of the document that was updated? Can I  
> perform transactions with Elasticsearch?

Elasticsearch's versioning includes a CAS-like functionality which should  
help with this: see  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html) ("Versioning")  
and [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/blog/2011/02/08/versioning.html)

Klaus

--

---

<div class="post-metadata">

**Author:** ![tbrianjones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbrianjones/32/1402_2.png) [@tbrianjones](https://discuss.elastic.co/u/tbrianjones)\
**Post date:** [November 28, 2012, 3:15am UTC](https://discuss.elastic.co/t/can-i-perform-the-equivalent-of-a-mysql-transaction-in-elasticsearch/9835/3 "2012-11-28T03:15:40Z")

</div>

I've tried implementing this but it doesn't seem like the Elasticsearch API  
supports versioning on updates. The \_update below should have failed  
because the document was at version 5 when the update was performed with a  
version of 3 in the POST data.

REQUEST: curl -XPOST  
'host:9200/companies/company/43/\_update?version=3&0pretty=true' -d '{  
"script" : "ctx.\_source.processing = true" }'

RESPONSE: {"ok":true,"\_index":"companies","\_type":"company","\_id":"43","\_version":6}

Is versioning available with updates or only with PUTs when replacing the  
entire document? Perhaps I'm not writing my scripts correctly for this  
kind of update. The scripting documentation is very confusing for me.

Perhaps there is a simpler way to update a single property of a document  
that I'm missing.

On Monday, November 26, 2012 10:27:35 PM UTC-8, Klaus Brunner wrote:

> On Tuesday, 27 November 2012 06:53:44 UTC+1, Brian Jones wrote:
> 
> > Am I able to select a document that is not being processed and update the  
> > 'processing' property to TRUE without another processor sneaking in and  
> > choosing the document? Is there an UPDATE WHERE type operation I can  
> > perform that will return the \_id of the document that was updated? Can I  
> > perform transactions with Elasticsearch?
> 
> Elasticsearch's versioning includes a CAS-like functionality which should  
> help with this: see  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html(%22Versioning%22)) and  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/blog/2011/02/08/versioning.html)
> 
> Klaus

--

---

<div class="post-metadata">

**Author:** ![tbrianjones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbrianjones/32/1402_2.png) [@tbrianjones](https://discuss.elastic.co/u/tbrianjones)\
**Post date:** [November 28, 2012, 3:29am UTC](https://discuss.elastic.co/t/can-i-perform-the-equivalent-of-a-mysql-transaction-in-elasticsearch/9835/4 "2012-11-28T03:29:11Z")

</div>

Some further research leads me to believe that \_updates request the full  
document, update the property specified and then replace the old document  
with the new document, meaning \_update may save on sending large documents  
over the network, but don't save on a request to the cluster to get the  
document, so if the client already has the document, I should just update  
the data client side and then replace the old document with a put request.  
Am I correct here?

On Tuesday, November 27, 2012 7:15:40 PM UTC-8, Brian Jones wrote:

> I've tried implementing this but it doesn't seem like the Elasticsearch  
> API supports versioning on updates. The \_update below should have failed  
> because the document was at version 5 when the update was performed with a  
> version of 3 in the POST data.
> 
> REQUEST: curl -XPOST  
> 'host:9200/companies/company/43/\_update?version=3&0pretty=true' -d '{  
> "script" : "ctx.\_source.processing = true" }'
> 
> RESPONSE: {"ok":true,"\_index":"companies","\_type":"company","\_id":"43","\_version":6}
> 
> Is versioning available with updates or only with PUTs when replacing the  
> entire document? Perhaps I'm not writing my scripts correctly for this  
> kind of update. The scripting documentation is very confusing for me.
> 
> Perhaps there is a simpler way to update a single property of a document  
> that I'm missing.
> 
> On Monday, November 26, 2012 10:27:35 PM UTC-8, Klaus Brunner wrote:
> 
> > On Tuesday, 27 November 2012 06:53:44 UTC+1, Brian Jones wrote:
> > 
> > > Am I able to select a document that is not being processed and update  
> > > the 'processing' property to TRUE without another processor sneaking in and  
> > > choosing the document? Is there an UPDATE WHERE type operation I can  
> > > perform that will return the \_id of the document that was updated? Can I  
> > > perform transactions with Elasticsearch?
> > 
> > Elasticsearch's versioning includes a CAS-like functionality which should  
> > help with this: see  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html(%22Versioning%22)) and  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/blog/2011/02/08/versioning.html)
> > 
> > Klaus

--

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 28, 2012, 10:23am UTC](https://discuss.elastic.co/t/can-i-perform-the-equivalent-of-a-mysql-transaction-in-elasticsearch/9835/5 "2012-11-28T10:23:28Z")

</div>

On Tue, 2012-11-27 at 19:15 -0800, Brian Jones wrote:

> I've tried implementing this but it doesn't seem like the  
> Elasticsearch API supports versioning on updates. The \_update below  
> should have failed because the document was at version 5 when the  
> update was performed with a version of 3 in the POST data.

That's interesting - it looks like update doesn't support versioning

> **[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.

which kinda makes sense from the concept of "updates are idempotent" and  
"retry updates retry\_on\_conflict times"

So yes - it looks like you either retrieve the document, update it, and  
reindex it with a version, or use the create/delete process that I  
described in another mail

clint

--

---

<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:02am UTC](https://discuss.elastic.co/t/can-i-perform-the-equivalent-of-a-mysql-transaction-in-elasticsearch/9835/6 "2017-07-06T03:02:31Z")

</div>


