面试题答案
一键面试在Elasticsearch中,可以使用Painless脚本语言来实现上述需求。以下是示例:
POST /your_index_name/_update_by_query
{
"script": {
"source": "if (ctx._source.price > 100 && ctx._source.discount < 0.8) { ctx._source.new_price = ctx._source.price * ctx._source.discount; }",
"lang": "painless"
}
}
在上述示例中:
ctx._source
表示当前文档的源数据。- 通过
if
语句进行条件判断,判断price
是否大于100且discount
是否小于0.8。 - 如果条件满足,则更新
new_price
字段为price
乘以discount
。 lang
指定脚本语言为Painless,这是Elasticsearch推荐的脚本语言。
请将 your_index_name
替换为实际的索引名称。