/** * @param string $localSite The wikiId to add/remove from local_sites_with_dupe * @param string $indexName The name of the index to perform updates to * @param aray $otherActions A list of arrays each containing the id within elasticsearch ('id') and the article id within $localSite ('articleId') * @return Status */ public function sendOtherIndexUpdates($localSite, $indexName, array $otherActions) { if (!$this->areIndexesAvailableForWrites(array($indexName), true)) { return Status::newFatal('cirrussearch-indexes-frozen'); } $client = $this->connection->getClient(); $status = Status::newGood(); foreach (array_chunk($otherActions, 30) as $updates) { $bulk = new \Elastica\Bulk($client); $articleIDs = array(); foreach ($updates as $update) { $title = Title::makeTitle($update['ns'], $update['dbKey']); $action = $this->decideRequiredSetAction($title); $this->log->debug("Performing `{$action}` for {$update['dbKey']} in ns {$update['ns']} on {$localSite} against id {$update['id']} in {$indexName}"); $script = new \Elastica\Script('super_detect_noop', array('source' => array('local_sites_with_dupe' => array($action => $localSite)), 'handlers' => array('local_sites_with_dupe' => 'set')), 'native'); $script->setId($update['id']); $script->setParam('_type', 'page'); $script->setParam('_index', $indexName); $bulk->addScript($script, 'update'); $titles[] = $title; } // Execute the bulk update $exception = null; try { $this->start("updating {numBulk} documents in other indexes", array('numBulk' => count($updates))); $bulk->send(); } catch (\Elastica\Exception\Bulk\ResponseException $e) { if (!$this->bulkResponseExceptionIsJustDocumentMissing($e)) { $exception = $e; } } catch (\Elastica\Exception\ExceptionInterface $e) { $exception = $e; } if ($exception === null) { $this->success(); } else { $this->failure($e); $this->failedLog->warning("OtherIndex update for articles: " . implode(',', $titles), array('exception' => $e)); $status->error('cirrussearch-failed-update-otherindex'); } } return $status; }
/** * @group functional */ public function testRetry() { $index = $this->_createIndex(); $type = $index->getType('bulk_test'); $client = $index->getClient(); $doc1 = $type->createDocument(1, array('name' => 'Mister Fantastic')); $doc1->setOpType(Action::OP_TYPE_UPDATE); $doc1->setRetryOnConflict(5); $bulk = new Bulk($client); $bulk->addDocument($doc1); $actions = $bulk->getActions(); $metadata = $actions[0]->getMetadata(); $this->assertEquals(5, $metadata['_retry_on_conflict']); $script = new \Elastica\Script(''); $script->setRetryOnConflict(5); $bulk = new Bulk($client); $bulk->addScript($script); $actions = $bulk->getActions(); $metadata = $actions[0]->getMetadata(); $this->assertEquals(5, $metadata['_retry_on_conflict']); }
/** * Converts a document into a call to super_detect_noop from the wikimedia-extra plugin. */ private function docToSuperDetectNoopScript($doc) { $params = $doc->getParams(); $params['source'] = $doc->getData(); $params['detectors'] = array('incoming_links' => 'within 20%'); $script = new \Elastica\Script('super_detect_noop', $params, 'native'); if ($doc->getDocAsUpsert()) { $script->setUpsert($doc); } return $script; }
/** * We can only do this if dynamic scripting is enabled. In elasticsearch.yml: * script.disable_dynamic: false * @see vendor/ruffin/elastica/test/bin/run_elasticsearch.sh * * @param array $terms * @return \Elastica\Aggregation\Sum */ protected function termsAggregation(array $terms) { $terms = str_replace('"', '\\"', $terms); $script = ' keywords = ["' . implode('","', $terms) . '"] total = 0 for (term in keywords) { total += _index["revisions.text"][term].tf() } return total'; $script = new \Elastica\Script($script, null, 'groovy'); $aggregation = new \Elastica\Aggregation\Sum('ttf'); // $aggregation->setScript() doesn't seem to properly set 'lang': 'groovy' // see https://github.com/ruflin/Elastica/pull/748 // $aggregation->setScript( $script ); $aggregation->setParams(array('lang' => 'groovy')); $aggregation->setParam('script', $script->getScript()); return $aggregation; }