如何在elasticsearch无痛脚本中取消更新

发布于 2025-01-09 15:03:00 字数 1104 浏览 2 评论 0原文

在尝试使用纯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);

遵循 elasticsearch 的文档

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咋地 2025-01-16 15:03:00

在写这个问题的过程中我想到了,不妨与其他人分享。


none 是 ctx.op 接受的关键字,它接受一个字符串。将更改为“无”

因此,完整的脚本应如下所示:

$script = 'if (!ctx._source.views_log.contains(params.identity)) {
             ctx._source.views_log.add(params.identity);
             ctx._source.views += 1;
           } else {
             ctx.op = "none";
           }';

$params = [
    'index' => 'post',
    'id'    => 4861,
    'body'  => [
        'script' => [
            'source' => $script,
            'lang' => "painless",
            'params' => [
                'identity' => $identifier
            ]
        ]
    ]
];

$response = $client->update($params);

这将给出所需的 "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. Change none to "none".

So the full script should look like this:

$script = 'if (!ctx._source.views_log.contains(params.identity)) {
             ctx._source.views_log.add(params.identity);
             ctx._source.views += 1;
           } else {
             ctx.op = "none";
           }';

$params = [
    'index' => 'post',
    'id'    => 4861,
    'body'  => [
        'script' => [
            'source' => $script,
            'lang' => "painless",
            'params' => [
                'identity' => $identifier
            ]
        ]
    ]
];

$response = $client->update($params);

This will give the desired "result": "noop"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文