/** * refresh search index */ public function postRefreshindex() { $searchComponent = new MediaSearchComponent(); // amount of units to index per iteration $batchsize = 500; $from = Input::get('next'); $unitCount = Entity::whereIn('tags', ['unit'])->count(); // reset index on start if ($from == 0) { $searchComponent->clear(); } // reduce last batch to remaining units if ($from + $batchsize > $unitCount) { $batchsize = $unitCount - $from; } // all units in this range $units = Entity::distinct('_id')->where('tags', ['unit'])->skip($from)->take($batchsize)->get(); // get keys for each unit in this batch $allKeys = []; for ($i = $from; $i < $from + $batchsize; $i++) { // get data of unit $unit = Entity::where('_id', $units[$i][0])->first(); // map all properties into keys with formats $keys = $this->getKeys($unit->attributesToArray()); // merge keys with set of keys and get the right format (e.g. if it occurs both at string and int we treat all of them as a string foreach ($keys as $k => $v) { if (!array_key_exists($k, $allKeys)) { $allKeys[$k] = ['key' => $keys[$k]['key'], 'label' => $keys[$k]['label'], 'format' => $keys[$k]['format'], 'documents' => [$keys[$k]['document']]]; } else { $allKeys[$k]['format'] = $searchComponent->prioritizeFormat([$allKeys[$k]['format'], $keys[$k]['format']]); // add document type if its not in the list yet if (!in_array($keys[$k]['document'], $allKeys[$k]['documents'])) { array_push($allKeys[$k]['documents'], $keys[$k]['document']); } } } } $searchComponent->store($allKeys); return ['log' => $from . ' to ' . ($from + $batchsize) . ' of ' . $unitCount, 'next' => $from + $batchsize, 'last' => $unitCount]; }