<?php

namespace App;

use ScoutEngines\Elasticsearch\ElasticsearchEngine as BaseEngine;

class ElasticsearchEngine extends BaseEngine
{

    /**
     * Update the given model in the index.
     *
     * @param  Collection  $models
     * @return void
     */
    public function update($models)
    {
        $params['body'] = [];

        $models->each(function($model) use (&$params)
        {
            $params['body'][] = [
                'update' => $this->getIdIndexType($model)
            ];
            $params['body'][] = [
                'doc' => $model->toSearchableArray(),
                'doc_as_upsert' => true
            ];
        });

        // dd( $params );

        $result = $this->elastic->bulk($params);

        // Unfortunately, we aren't quite ready to handle these exceptions.

        // if (isset($result['errors']) === true && $result['errors'] === true)
        // {
        //     throw new \Exception(json_encode($result));
        // }
    }

}

<?php

namespace App;

use ScoutEngines\Elasticsearch\ElasticsearchEngine as BaseEngine;

class ElasticsearchEngine extends BaseEngine
{

    /**
     * Update the given model in the index.
     *
     * @param  Collection  $models
     * @return void
     */
    public function update($models)
    {
        $params['body'] = [];

        $models->each(function($model) use (&$params)
        {
            $params['body'][] = [
                'update' => $this->getIdIndexType($model)
            ];
            $params['body'][] = [
                'doc' => $model->toSearchableArray(),
                'doc_as_upsert' => true
            ];
        });

        $result = $this->elastic->bulk($params);

        // TODO: Requeue only the models that failed?
        if (isset($result['errors']) && $result['errors'] === true)
        {
            throw new \Exception(json_encode($result));
        }
    }

}

Throw exception on Elasticsearch update error
Throw exception on Elasticsearch update error

This isn't ideal because we use the bulk update API, so some of the
bulk items can succeed, while other fail. In an ideal situation, we
would requeue only the items that failed, and allow the bulk process
to continue. We aren't quite there yet, so this is a good stopgap.
We are ready to survey these exceptions via Sentry.
