Skip to content

Instantly share code, notes, and snippets.

@roalcantara
Created January 28, 2018 23:10
Show Gist options
  • Save roalcantara/39e275165a4603ca8268f5b92b8e517b to your computer and use it in GitHub Desktop.
Save roalcantara/39e275165a4603ca8268f5b92b8e517b to your computer and use it in GitHub Desktop.
ElasticSearch: Append to array
const created_by = '12345566';
const script = 'if (ctx._source.raters == null) { ctx._source.raters = [] } \
if (ctx._source.raters.indexOf(params.created_by) == -1) {\
ctx._source.raters.add(params)\
}';
return this.client.update({
index: 'index',
type: 'type',
id: 'key',
body: {
script: {
lang: 'painless',
source: script,
params: {
created_by: created_by
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment