ScoutEngines\Elasticsearch\ElasticsearchEngine::update PHP Method

update() public method

Update the given model in the index.
public update ( Illuminate\Database\Eloquent\Collection $models ) : void
$models Illuminate\Database\Eloquent\Collection
return void
    public function update($models)
    {
        $params['body'] = [];
        $models->each(function ($model) use(&$params) {
            $params['body'][] = ['update' => ['_id' => $model->getKey(), '_index' => $this->index, '_type' => $model->searchableAs()]];
            $params['body'][] = ['doc' => $model->toSearchableArray(), 'doc_as_upsert' => true];
        });
        $this->elastic->bulk($params);
    }

Usage Example

 public function test_update_adds_objects_to_index()
 {
     $client = Mockery::mock('Elasticsearch\\Client');
     $client->shouldReceive('bulk')->with(['body' => [['update' => ['_id' => 1, '_index' => 'scout', '_type' => 'table']], ['doc' => ['id' => 1], 'doc_as_upsert' => true]]]);
     $engine = new ElasticsearchEngine($client, 'scout');
     $engine->update(Collection::make([new ElasticsearchEngineTestModel()]));
 }