Update with script and upsert issues

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.

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

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.

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}}

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