如何在elasticsearch无痛脚本中取消更新
在尝试使用纯elasticsearch-painless切换PHP代码时,我注意到即使文档在更新前后相同,文档也不会返回“noop”。
我不确定每次执行代码时更新版本是否会产生任何后果?它如何扩展?
如果在 views_log
中找不到身份,我只是尝试在访问期间更新帖子的 views
,并且想知道是否有办法修复“ noop”返回,或者以某种方式取消更新?
我现在的代码如下所示:
$script = 'if (!ctx._source.views_log.contains(params.identity)) {
ctx._source.views_log.add(params.identity);
ctx._source.views += 1;
}';
$params = [
'index' => 'post',
'id' => 4861,
'body' => [
'script' => [
'source' => $script,
'lang' => "painless",
'params' => [
'identity' => $identifier
]
]
]
];
$response = $client->update($params);
ctx['op']: 使用默认的索引来更新文档。设置为none表示不进行任何操作,设置为delete则从索引中删除当前文档。
如果不满足条件,我尝试将 ctx.op
设置为 none
,但这似乎不起作用。
While working on trying to switch out PHP code with pure elasticsearch-painless, I noticed that the document doesn't return "noop" even if the document is identical before and after update.
I'm not sure if there is any consequences of having a version update for every time the code is executed? How does it scale?
I'm simply trying to update the views
of a post during visit if the identity was not found in views_log
, and was wondering either if there is a way to fix the "noop" return, or somehow have it cancel the update?
The code I have right now looks like this:
$script = 'if (!ctx._source.views_log.contains(params.identity)) {
ctx._source.views_log.add(params.identity);
ctx._source.views += 1;
}';
$params = [
'index' => 'post',
'id' => 4861,
'body' => [
'script' => [
'source' => $script,
'lang' => "painless",
'params' => [
'identity' => $identifier
]
]
]
];
$response = $client->update($params);
Following elasticsearch's documentation:
ctx['op']:
Use the default of index to update a document. Set to none to specify no operation or delete to delete the current document from the index.
I tried setting ctx.op
to none
if the condition is not met, but that didn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在写这个问题的过程中我想到了,不妨与其他人分享。
none
是 ctx.op 接受的关键字,它接受一个字符串。将无
更改为“无”
。因此,完整的脚本应如下所示:
这将给出所需的
"result": "noop"
During writing of this question I figured it out, and might as well share with others.
none
is an accepted keyword for ctx.op, it accepts a string. Changenone
to"none"
.So the full script should look like this:
This will give the desired
"result": "noop"