Skip to content

Instantly share code, notes, and snippets.

@sjose1x
Last active January 30, 2021 23:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjose1x/25e71f932411e4da0d70af0240044705 to your computer and use it in GitHub Desktop.
Save sjose1x/25e71f932411e4da0d70af0240044705 to your computer and use it in GitHub Desktop.
Elasticsearch update fields using groovy scripts
ORIGINAL DOC
-------------
{
"counter": 61,
"tags": ["red", "blue"]
}
Updating Single Field if its not ther in the array
-------------------
{
"script": "if(ctx._source.tags.contains(tag)){ ctx.op=\"none\" } else { ctx._source.tags += tag }",
"params": {
"tag": "blue"
}
}
Updating Multiple Fields
-----------------------
{
"script": "ctx._source.counter += count;if(ctx._source.tags.contains(tag)){ \"none\" } else { ctx._source.tags += tag };",
"params": {
"tag": "green",
"count": 14
}
}
Create the document if its does not exist
------------------
{
"script": "ctx._source.cnt += count;if(!ctx._source.tags.contains(tag)){ ctx._source.tags += tag }",
"params": {
"tag": "red123",
"count": 14
},
"upsert": {
"cnt": 1,
"tags": [
"red"
]
}
}
### Delete documents in an index
`curl -XDELETE 'http://localhost:9200/taleo/resume/_query' -d '{"query":{"match_all":{}}}'`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment