# Conditional updates with Update API

**URL:** https://discuss.elastic.co/t/conditional-updates-with-update-api/80909
**Category:** Elasticsearch
**Created:** [April 1, 2017, 9:36pm UTC](https://discuss.elastic.co/t/conditional-updates-with-update-api/80909 "2017-04-01T21:36:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![btucker](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/btucker/32/16975_2.png) [@btucker](https://discuss.elastic.co/u/btucker)
#### Post date: [April 1, 2017, 9:36pm UTC](https://discuss.elastic.co/t/conditional-updates-with-update-api/80909/1 "2017-04-01T21:36:41Z")

</div>

Here's the situation I'm trying to solve for:

I have documents getting upserted by multiple processes using the Bulk API. They look like this:

```
{
   "ver": 1,
   "foo": 3
}
{
   "ver": 2,
   "foo": 4
}
{
   "ver": 4,
   "foo": 5
}
{
   "ver": 3,
   "foo": 4
}

```

I would like this final state of ES to have:

```
{
   "ver": 4,
   "foo": 5
}

```

Because they are coming through multiple queues, they are not guaranteed to be in order. What I would like is for the update to only occur if "ver" is higher than the one currently stored.

Approaches I've considered:

1. Use versioning support to supply the previous version number such that the update only succeeds if this is the next sequential document. Problems with this: if one of the updates is missing from the stream, later updates would never be written.

2. Use `version_type=external`. This seems like exactly what we want, but it is not supported by the Update API.

3. Using an inline script. Problem here as I understand it is then needing to manual handle every field of the document in the script, since you can't have both a `script` & `doc` in the update.

Any suggestions?

---

<div class="post-metadata">

### Author: ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)
#### Post date: [April 2, 2017, 10:52am UTC](https://discuss.elastic.co/t/conditional-updates-with-update-api/80909/2 "2017-04-02T10:52:24Z")

</div>

> [@btucker](#):
>
> Use version\_type=external. This seems like exactly what we want, but it is not supported by the Update API.

"External" versioning is the correct solution. Use index API [Index API | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html)

---

<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: [April 30, 2017, 10:52am UTC](https://discuss.elastic.co/t/conditional-updates-with-update-api/80909/3 "2017-04-30T10:52:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
