Skip to content

5.0. UpdateRequest Api doesn't match server spec (inline scripts) #2343

@niemyjski

Description

@niemyjski

The following script compiles fine on elastic 5.0 client but fails to execute:

var request = new UpdateRequest<Stack, Stack>(GetIndexById(stackId), ElasticType.Type, stackId) {
    RetryOnConflict = 3,
    Script = @"if (ctx._source.total_occurrences == 0 || ctx._source.first_occurrence > minOccurrenceDateUtc) {
                ctx._source.first_occurrence = minOccurrenceDateUtc;
                }
                if (ctx._source.last_occurrence < maxOccurrenceDateUtc) {
                ctx._source.last_occurrence = maxOccurrenceDateUtc;
                }
                ctx._source.total_occurrences += count;",
    Params = new Dictionary<string, object>(3) {
        { "minOccurrenceDateUtc", minOccurrenceDateUtc },
        { "maxOccurrenceDateUtc", maxOccurrenceDateUtc },
        { "count", count }
    }
};
{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[8mJiEA3][local[1]][indices:data/write/update[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "reason": "compile error",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Variable [minOccurrenceDateUt] is not defined."
      },
      "script_stack": [
        "... ource.first_occurrence > minOccurrenceDateUt) { ct ...",
        "                             ^---- HERE"
      ],
      "script": "if (ctx._source.total_occurrences == 0 || ctx._source.first_occurrence > minOccurrenceDateUt) { ctx._source.first_occurrence = minOccurrenceDateUtc; } if (ctx._source.last_occurrence < maxOccurrenceDateUtc) { ctx._source.last_occurrence = maxOccurrenceDateUtc; } ctx._source.total_occurrences += count;",
      "lang": "painless"
    }
  },
  "status": 400
}

Upon looking closer at the docs (https://www.elastic.co/guide/en/elasticsearch/reference/5.0/docs-update.html).

It looks like this should now be defined as an inline script:

POST test/type1/1/_update
{
    "script" : {
        "inline": "if (ctx._source.tags.contains(params.tag)) { ctx.op = \"delete\" } else { ctx.op = \"none\" }",
        "lang": "painless",
        "params" : {
            "tag" : "green"
        }
    }
}

Also on a side note, were you previously removing line breaks before sending the request? I'm seeing \r\n being submitted in the raw json.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions