Example #1
0
 private static function indexItemInElasticsearch($libraryID, $key, $version, $timestamp, $content, $stats = array())
 {
     $type = self::getWriteType();
     $id = $libraryID . "/" . $key;
     $doc = ['id' => $id, 'libraryID' => $libraryID, 'content' => (string) $content, 'version' => $version, 'timestamp' => str_replace(" ", "T", $timestamp)];
     if ($stats) {
         foreach (self::$metadata as $prop) {
             if (isset($stats[$prop])) {
                 $doc[$prop] = (int) $stats[$prop];
             }
         }
     }
     $start = microtime(true);
     $doc = new \Elastica\Document($id, $doc, self::$elasticsearchType);
     $doc->setVersion($version);
     $doc->setVersionType('external');
     try {
         $response = $type->addDocument($doc);
     } catch (Exception $e) {
         $msg = $e->getMessage();
         if (preg_match('/version conflict, current \\[([0-9]+)\\], provided \\[([0-9]+)\\]/', $msg, $matches)) {
             if ($matches[1] == $matches[2]) {
                 error_log("WARNING: " . $msg);
                 return;
             }
         }
         throw $e;
     }
     StatsD::timing("elasticsearch.client.item_fulltext.add", (microtime(true) - $start) * 1000);
     if ($response->hasError()) {
         $msg = $response->getError();
         if (preg_match('/version conflict, current \\[([0-9]+)\\], provided \\[([0-9]+)\\]/', $msg, $matches)) {
             if ($matches[1] == $matches[2]) {
                 error_log("WARNING: " . $msg);
                 return;
             }
         }
         throw new Exception($response->getError());
     }
 }