Exemplo n.º 1
0
 /**
  * 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());
     }
 }
 /**
  * (non-PHPdoc)
  * @see SugarSearchEngineInterface::delete()
  */
 public function delete(SugarBean $bean)
 {
     if (self::isSearchEngineDown()) {
         return;
     }
     if (empty($bean->id)) {
         return;
     }
     try {
         $indexName = $this->getWriteIndexForBean($bean);
         $this->logger->debug("Going to delete {$bean->id} on index {$indexName}");
         $index = new \Elastica\Index($this->_client, $indexName);
         $type = new \Elastica\Type($index, $this->getIndexType($bean));
         $type->deleteById($bean->id);
     } catch (Exception $e) {
         $this->reportException("Unable to delete index", $e);
         $this->checkException($e);
     }
 }