/**
* Import an Eloquent
*
* @param Model $model
* @param array $relations
* @param int $batchSize
* @param callable $callback
* @internal param $type
*/
public function import(Model $model, $relations = [], $batchSize = 750, callable $callback = null)
{
$batch = 0;
$asQueryLoggind = $model->getConnection()->logging();
$model->getConnection()->disableQueryLog();
while (true) {
// Increase the batch number
$batch += 1;
// Load records from the database
$records = $model->newInstance()->with($relations)->skip($batchSize * ($batch - 1))->take($batchSize)->get();
// Break out of the loop if we are out of records
if (count($records) == 0) {
break;
}
// Call the callback function to provide feedback on the import process
if ($callback) {
$callback($batch);
}
// Transform each record before sending it to Elasticsearch
$data = [];
foreach ($records as $record) {
$data[] = ['index' => ['_id' => $record->getEsId()]];
$data[] = $record->transform(!empty($relations));
}
// Bulk import the data to Elasticsearch
$this->bulk($data);
}
if ($asQueryLoggind) {
$model->getConnection()->enableQueryLog();
}
}