Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save roalcantara/79686e0822e55e44958cb83c34ec34a8 to your computer and use it in GitHub Desktop.
Save roalcantara/79686e0822e55e44958cb83c34ec34a8 to your computer and use it in GitHub Desktop.
Elasticsearch [6.8] > Painless > Upsert into arrays
POST events/user/:id/_update
{
"script": {
"lang": "painless",
"source": "if (ctx._source.on_screen_view == null) { ctx._source.on_screen_view = [] }\nfor (int i = 0; i < params.events.length; i++) {\nctx._source.on_screen_view.add(params.events[i])\n}\n",
"params": {
"events": [
"2019-07-10T21:57:46.173Z",
"2019-07-10T20:49:34.429Z",
"2019-07-10T20:50:08.135Z"
]
}
},
"upsert": {
"on_screen_view": [
"2019-07-10T21:57:46.173Z",
"2019-07-10T20:49:34.429Z",
"2019-07-10T20:50:08.135Z"
]
}
}
// references:
// https://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-update.html
// https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-walkthrough.html
// https://www.compose.com/articles/how-to-script-painless-ly-in-elasticsearch/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment