/** * Update the complete index * * @param mixed[] $ids Only update given IDs * @param bool|null $useMaintenance Read data from the maintenance database, if any * @param int $limit Limit query * @param int $maxDocsPerRequest Number of docs per bulk-request */ public function update($ids = null, $useMaintenance = null, $limit = null, $maxDocsPerRequest = self::MAX_DOCS_PER_REQUEST) { if (is_array($ids) && empty($ids)) { return; } if (is_array($ids)) { $idsDelete = array(); foreach ($ids as $id) { $idsDelete[$id] = true; } } $query = $this->_getQuery($ids, $limit); if ($useMaintenance) { $client = CM_Service_Manager::getInstance()->getDatabases()->getReadMaintenance(); $disableQueryBuffering = true; } else { $client = CM_Service_Manager::getInstance()->getDatabases()->getMaster(); $disableQueryBuffering = false; } $result = $client->createStatement($query)->execute(null, $disableQueryBuffering); $docs = array(); $i = 0; // Loops through all results. Write every $maxDocsPerRequest docs to the server while ($row = $result->fetch()) { $doc = $this->_getDocument($row); $docs[] = $doc; if (!empty($idsDelete)) { unset($idsDelete[$doc->getId()]); } // Add documents to index and empty documents array if ($i++ % $maxDocsPerRequest == 0) { $this->_type->addDocuments($docs); $docs = array(); } } // Add not yet sent documents to index if (!empty($docs)) { $this->_type->addDocuments($docs); } // Delete documents that were not updated (=not found) if (!empty($idsDelete)) { $idsDelete = array_keys($idsDelete); $this->getIndex()->getClient()->deleteIds($idsDelete, $this->getIndex()->getName(), $this->getType()->getName()); } }