yiisoft / yii2-elasticsearch

Yii 2 Elasticsearch extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
429 stars 252 forks source link

Fix ES exception when a field missed in using `updateAllCounters`. #334

Open jafaripur opened 1 year ago

jafaripur commented 1 year ago
Q A
Is bugfix? ✔️
New feature?
Breaks BC?

When using updateAllCounters for a field which is missed or is null get exception error from ElasticSearch:

Cannot invoke "Object.getClass()" because "left" is null

This behavior should cast null and missed field to 0.

what-the-diff[bot] commented 1 year ago

PR Summary

beowulfenator commented 1 year ago

@jafaripur Thank you for your improvement. This is clearly a good change. However, let's explore all possibilities and perhaps make this function even better. As far as I know, the different values for an attribute are possible. Here is my suggestion on handling them all:

Value Action
A null value Cast to integer and make it 0, then update
A "number" value Update normally
A non-number value Raise exception
An array with a single value Look at the single value and act accordingly. Preserve the array, so with a +1 increment, [123] becomes [124], not 124
An array with multiple values Raise exception

If possible, the exception should be meaningful, so it should be obvious that we are updating a value that can not be updated.