yii\elasticsearch\ActiveRecord::updateAllCounters PHP Method

updateAllCounters() public static method

For example, to add 1 to age of all customers whose status is 2, ~~~ Customer::updateAllCounters(['age' => 1], ['status' => 2]); ~~~
public static updateAllCounters ( array $counters, array $condition = [] ) : integer
$counters array the counters to be updated (attribute name => increment value). Use negative values if you want to decrement the counters.
$condition array the conditions that will be passed to the `where()` method when building the query. Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
return integer the number of rows updated
    public static function updateAllCounters($counters, $condition = [])
    {
        $primaryKeys = static::primaryKeysByCondition($condition);
        if (empty($primaryKeys) || empty($counters)) {
            return 0;
        }
        $bulkCommand = static::getDb()->createBulkCommand(["index" => static::index(), "type" => static::type()]);
        foreach ($primaryKeys as $pk) {
            $script = '';
            foreach ($counters as $counter => $value) {
                $script .= "ctx._source.{$counter} += {$counter};\n";
            }
            $bulkCommand->addAction(["update" => ["_id" => $pk]], ["script" => $script, "params" => $counters, "lang" => "groovy"]);
        }
        $response = $bulkCommand->execute();
        $n = 0;
        $errors = [];
        foreach ($response['items'] as $item) {
            if (isset($item['update']['status']) && $item['update']['status'] == 200) {
                $n++;
            } else {
                $errors[] = $item['update'];
            }
        }
        if (!empty($errors) || isset($response['errors']) && $response['errors']) {
            throw new Exception(__METHOD__ . ' failed updating records counters.', $errors);
        }
        return $n;
    }