elastic_tutorial/The Elasticsearch Documentation/5_script-Medium/3_update.es
2025-04-09 09:37:23 +03:30

83 lines
1.8 KiB
JavaScript

PUT my-index/_doc/1
{
"counter": 1,
"tags": [
"red"
]
}
POST my-index/_update/1
{
"script": {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params": {
"count": 4
}
}
}
// ctx -> doc
// ctx._source -> مقادیر داخل doc
// ctx._source.counter -> مقدار counter در doc
// نتیجه: counter برابر با 5 میشود
POST my-index/_update/1
{
"script": {
"source": "ctx._source.tags.add(params['tag'])",
"lang": "painless",
"params": {
"tag": "blue"
}
}
}
// نتیجه: به tags مفدار blue add میشود
POST my-index/_update/1
{
"script": {
"source": "if (ctx._source.tags.contains(params['tag'])) { ctx._source.tags.remove(ctx._source.tags.indexOf(params['tag'])) }",
"lang": "painless",
"params": {
"tag": "blue"
}
}
}
// if (ctx._source.tags.contains(params['tag'])) --> آیا tags مقدار tag را دارد؟(blue)
// ctx._source.tags.remove(ctx._source.tags.indexOf(params['tag'])) --> حذف مقدار tag(blue)
POST my-index/_update/1
{
"script" : "ctx._source.new_field = 'value_of_new_field'"
}
// افزودن فیلد
POST my-index/_update/1
{
"script" : "ctx._source.remove('new_field')"
}
// حذف فیلد
POST my-index/_update/1
{
"script": {
"source": "if (ctx._source.tags.contains(params['tag'])) { ctx.op = 'delete' } else { ctx.op = 'none' }",
"lang": "painless",
"params": {
"tag": "green"
}
}
}
// if (ctx._source.tags.contains(params['tag'])) --> آیا tags green را دارد؟
// op -> opration: انجام عملیات
// اگر دارد آن را حذف کن
// اگر نداشت هیچی