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.
Advertisement
Answer
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"