# Update with script and upsert issues

**URL:** https://discuss.elastic.co/t/update-with-script-and-upsert-issues/90693
**Category:** Elasticsearch
**Created:** [June 24, 2017, 12:52am UTC](https://discuss.elastic.co/t/update-with-script-and-upsert-issues/90693 "2017-06-24T00:52:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kbridburg](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kbridburg](https://discuss.elastic.co/u/kbridburg)
#### Post date: [June 24, 2017, 12:52am UTC](https://discuss.elastic.co/t/update-with-script-and-upsert-issues/90693/1 "2017-06-24T00:52:46Z")

</div>

I'm trying to send the following

```
{"update":{"_index":"indexName","_type":"textt","_id":"2"}}
{"script":{"inline":"if(ctx._source.version<params.uploadVersion){ctx.op="update"}else{ctx.op="none"}","params":{"uploadVersion":5}},"upsert": {"id":"2","text":"some text","version":5}}

```

as part of a bulk command and am receiving the following response

```
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Content-Length: 456
  Content-Type: application/json; charset=UTF-8
}}

```

I'm sure that it is this portion of the bulk command causing the issue.

The overall goal of this is to update a document only if its Version number is greater than the currently stored one, and to store the document if no version exists. At first I attempted to use "doc" and "doc\_as\_upsert" but I found another forum post saying "script" and "doc\_as\_upsert" are incompatible.

No using the elasticsearch \_version is not an option

If anyone can figure out a way to solve this it would be much appreciated.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [June 26, 2017, 7:15am UTC](https://discuss.elastic.co/t/update-with-script-and-upsert-issues/90693/2 "2017-06-26T07:15:55Z")

</div>

Hey,

the `type` and `index` names require an underscore in it's front. Also, can you provide the **exact** request being sent, because this one does not take of of escaping the double ticks.

On top, the Elasticsearch version being used would be useful as well as putting your code in a proper formatted code snippet (you can use markdown here).

--Alex

---

<div class="post-metadata">

### Author: ![kbridburg](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kbridburg](https://discuss.elastic.co/u/kbridburg)
#### Post date: [June 26, 2017, 9:59pm UTC](https://discuss.elastic.co/t/update-with-script-and-upsert-issues/90693/4 "2017-06-26T21:59:00Z")

</div>

Thanks for pointing out the code snippet functionality! Am I using it correctly now?

I'm using elasticsearch 5.3

type and index do have underscores, something about the formatting of the page removed them.

There isn't much more to the full request. It's more or less:

```
curl POST 'hostname/indexName/textt/_bulk' -d '
{"update":{"_index":"indexName","_type":"textt","_id":"2"}}
{"script":{"inline":"if(ctx._source.version&lt;params.uploadVersion){ctx.op="update"}else{ctx.op="none"}","params":{"uploadVersion":5}},"upsert": {"id":"2","text":"some text","version":5}}
'

```

I'm sure that the specific two lines I just posted are causing the issue. I'm generating these commands and either formatting the document to this update command or an index command based on version number. When its only index commands it works.

---

<div class="post-metadata">

### Author: ![kbridburg](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kbridburg](https://discuss.elastic.co/u/kbridburg)
#### Post date: [June 27, 2017, 1:31am UTC](https://discuss.elastic.co/t/update-with-script-and-upsert-issues/90693/5 "2017-06-27T01:31:22Z")

</div>

I'm no longer recieving that error, there was an issue with script compiling (I didn't have the "" around noop and update). However now no matter what I have for uploadVersion the result is noop

```
{"update":{"_index":"indexName","_type":"text","_id":"1"}}
{"script":{"inline":"if(ctx._source.version<params.uploadVersion){ctx.op=\"update\"}else{ctx.op=\"none\"}","params":{"uploadVersion":2}},"upsert":{"id":"1","text":"text\r\n","version":2}}
```

---

<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 25, 2017, 1:31am UTC](https://discuss.elastic.co/t/update-with-script-and-upsert-issues/90693/6 "2017-07-25T01:31:39Z")

</div>

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