项目里有人用script,更新部分字段
大概是这样的
POST sz_index_mean/_update_by_query
{
"query": {
"match_phrase": {
"sz.sz_title": "Java加密技术"
}
},
"script": {
"source": "ctx._source.sz['sz_top_time'] = long类型的数据",
}
}
当long类型的数据在int类型范围内时,
"source": "ctx._source.sz['sz_top_time'] = 123"
是可以更新成功的。
然而,当long类型的数据不在int类型范围内时,
"source": "ctx._source.sz['sz_top_time'] = 1637563531010"
则报了错
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Invalid int constant [1637563531010]. If you want a long constant then change it to [1637563531010L]."
}
意思是1637563531010是一个无效的int类型常量,如果你期望一个long类型的常量应修改为1637563531010L
当我修改为
"source": "ctx._source.sz['sz_top_time'] = 1637563531010L"
时,发现更新成功了。
显然这种需要关心数据类型是否在末尾加"L"的写法是令人抓狂的,下面用params优雅地实现一下
以 params.top_time 代替上面直接拼接的内容
POST sz_index_mean/_update_by_query
{
"query": {
"match_phrase": {
"sz.sz_title": "Java加密技术"
}
},
"script": {
"source": "ctx._source.sz['sz_top_time'] = params.top_time",
"params": {
"top_time": 1637563531010
}
}
}
嗯,也成功了。