/** * Update index * * @param mixed $count Number of items to index * @param array $filter Filter for the batch results * * @return void */ public function update($count = 50, $filter = null) { $logger = $this->container->get('logger'); $batches = $count <= self::BATCH_MAX ? 1 : ceil($count / self::BATCH_MAX); for ($i = 1; $i <= $batches; $i++) { $batchCount = $i == $batches && $count % self::BATCH_MAX > 0 ? $count % self::BATCH_MAX : self::BATCH_MAX; $items = $this->repository->getBatch($batchCount, $filter); // TODO: Built in check if $items are less then $batchcount, we could stop iterating foreach ($this->indexClients as $client) { if ($client->isEnabled($this->name)) { $client->setService($this->service); foreach ($items as $item) { $client->setItem($item); if ($client->isTypeIndexable($this->name, $this->service->getSubType($item))) { if ($this->service->isIndexable($item)) { try { $client->add($this->service->getDocument($item)); } catch (Exception $e) { $itemData = $this->service->getDocument($item); $logger->error('Could not (completely) add item (' . $itemData['id'] . ') to indexing client. (' . __CLASS__ . ' - ' . $e->getMessage() . ')'); } } elseif ($this->service->isIndexed($item)) { try { $client->delete($this->service->getDocumentId($item)); } catch (Exception $e) { $logger->error('Could not (completely) delete item to indexing client. (' . __CLASS__ . ' - ' . $e->getMessage() . ')'); } } } } $client->flush(); } } $this->repository->setIndexedNow($items); } }