public function _reindex($safe = true, $setSettings = true, $mergeOldSettings = false, \Closure $onInsert = null)
{
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
$modelHelper = App::make('\\AlgoliaSearch\\Laravel\\ModelHelper');
$indices = $modelHelper->getIndices($this);
$indicesTmp = $safe ? $modelHelper->getIndicesTmp($this) : $indices;
if ($setSettings === true) {
$setToTmpIndices = $safe === true;
$this->_setSettings($setToTmpIndices, $mergeOldSettings);
}
static::chunk(100, function ($models) use($indicesTmp, $modelHelper, $onInsert) {
/** @var \AlgoliaSearch\Index $index */
foreach ($indicesTmp as $index) {
$records = [];
$recordsAsEntity = [];
foreach ($models as $model) {
if ($modelHelper->indexOnly($model, $index->indexName)) {
$records[] = $model->getAlgoliaRecordDefault($index->indexName);
if ($onInsert && is_callable($onInsert)) {
$recordsAsEntity[] = $model;
}
}
}
$index->addObjects($records);
if ($onInsert && is_callable($onInsert)) {
call_user_func_array($onInsert, [$recordsAsEntity]);
}
}
});
if ($safe) {
for ($i = 0; $i < count($indices); $i++) {
$modelHelper->algolia->moveIndex($indicesTmp[$i]->indexName, $indices[$i]->indexName);
}
$this->_setSettings(false);
// To a setSettings to set the slave on the master
}
}