Last active
January 30, 2021 23:58
-
-
Save sjose1x/25e71f932411e4da0d70af0240044705 to your computer and use it in GitHub Desktop.
Elasticsearch update fields using groovy scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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