Skip to content

Instantly share code, notes, and snippets.

@jspooner
Last active July 1, 2016 20:02
Show Gist options
  • Save jspooner/aca369e357ac824c5832b347f98c005a to your computer and use it in GitHub Desktop.
Save jspooner/aca369e357ac824c5832b347f98c005a to your computer and use it in GitHub Desktop.
Elastic Search array update
demographics.groovy
if (ctx._source.demographics) {
match_found = false
x = 0
for (d in ctx._source.demographics) {
if (d.source == demographic.source) {
match_found = true
ctx._source.demographics[x] = demographic // will destroy the original object. Any keys could be lost
}
x = x + 1
}
if (!match_found) {
ctx._source.demographics = ctx._source.demographics + demographic
}
} else {
ctx._source.demographics = [demographic]
}
POST devices-v1/device/_bulk
{"index":{"_id":"a.E20B3E15-36BE-43E0-A04D-3FB5CE7C0B7A"}}
{"worstgolfer":"Dave", "demographics": [{"source": "verve", "gender" :"female"}]}
POST /devices-v1/device/a.E20B3E15-36BE-43E0-A04D-3FB5CE7C0B7A/_update
{
"script": {
"file": "demographics",
"params": {
"demographic": {
"gender": "M",
"source": "NBC"
}
}
}
}
POST /devices-v1/device/a.E20B3E15-36BE-43E0-A04D-3FB5CE7C0B7A/_update
{
"script": {
"file": "demographics",
"params": {
"demographic": {
"gender": "m",
"source": "ABC"
}
}
}
}
GET /devices-v1/device/a.E20B3E15-36BE-43E0-A04D-3FB5CE7C0B7A/_source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment