Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with scripted update - "params" are ignored #487

Closed
madebydna opened this issue Jan 3, 2018 · 3 comments
Closed

Problem with scripted update - "params" are ignored #487

madebydna opened this issue Jan 3, 2018 · 3 comments

Comments

@madebydna
Copy link

I'm trying to do a scripted partial update but my params are being ignored, or they are evaluated as null:

body = {
  script: "if (!ctx._source.containsKey(\"title_akas\")) { ctx._source.title_akas = [] }
    ctx._source.title_akas.add(params.aka)",
  params: {
    aka: { text: "New Title AKA" }
  },
  lang: "painless"
}

client = Elasticsearch::Client.new(host: Rails.application.secrets.es_url, log: false)
client.update index: "test", type: "program", id: "1234", body: body

The result of the operation is that a null title_akas is added:

"title_akas": [
   null
],...

title_akas is a nested field in the index:

  "title_akas": {
       "type": "nested",
       "properties": {
       "text": {
           "type": "text"
       }
    }
  },

The same scripted update works via the console:

POST test/program/1234/_update
{
  "script": {
    "inline": "if (!ctx._source.containsKey(\"title_akas\")) { ctx._source.title_akas = [] } ctx._source.title_akas.add(params.aka)",
    "lang": "painless",
    "params": {
      "aka": {
        "text": "New Title AKA"
      }
    }
  }
}
@felipeelias
Copy link

felipeelias commented Jan 5, 2018

The script should be a hash, the same way it is done via console

body = {
  script: {
    source: "if (!ctx._source.containsKey(\"title_akas\")) { ctx._source.title_akas = [] } ctx._source.title_akas.add(params.aka)",
    params: {
      aka: { text: "New Title AKA" }
    },
    lang: "painless"
  }
}

@madebydna
Copy link
Author

madebydna commented Jan 5, 2018

@felipeelias thanks, that worked! I was following the example in the documentation that suggested that the script key in the body hash is a string.

 # @example Add a tag to document `tags` property using a `script`
 #
 #     client.update index: 'myindex', type: 'mytype', id: '1',
 #                   body: { script: 'ctx._source.tags += tag', params: { tag: 'x' } }

@karmi
Copy link
Contributor

karmi commented Jan 14, 2018

Hi @madebydna, yes, @felipeelias is right, the format has changed, but the inline documentation has not been updated.

I've updated the example, it should be:

client.update index: 'myindex', type: 'mytype', id: '1',
                       body: { script: { source: 'ctx._source.tags.add(params.tag)', params: { tag: 'x' } } }

Thanks for bringing it to my attention!

@karmi karmi closed this as completed Jan 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants