Author: Nicolas Ruflin (spam@ruflin.com)
示例#1
0
 public function publish(Client $client, $actionType, TableInterface $table, RecordInterface $record)
 {
     $con = new Condition(array('id', '=', $record->id));
     $row = $table->getRow(array('id', 'pageId', 'userId', 'urlTitle', 'title', 'text'), $con);
     $index = $client->getIndex('amun');
     $type = $index->getType('page');
     $id = $row['pageId'] . '-' . $record->id;
     try {
         $document = $type->getDocument($id);
         if ($actionType == RecordAbstract::INSERT || $actionType == RecordAbstract::UPDATE) {
             // get referring page
             $handler = $this->hm->getHandler('AmunService\\Content\\Page');
             $page = $handler->get($row['pageId'], array('id', 'path', 'urlTitle', 'title'));
             $data = array('id' => $id, 'userId' => $row['userId'], 'path' => $page['path'] . '/view/' . $row['id'] . '/' . $row['urlTitle'], 'title' => $row['title'], 'content' => $row['text'], 'date' => time());
             $type->updateDocument(new Document($id, $data));
         } else {
             if ($actionType == RecordAbstract::DELETE) {
                 $type->deleteDocument($document);
             }
         }
     } catch (NotFoundException $e) {
         if ($actionType == RecordAbstract::INSERT || $actionType == RecordAbstract::UPDATE) {
             // get referring page
             $handler = $this->hm->getHandler('AmunService\\Content\\Page');
             $page = $handler->get($row['pageId'], array('id', 'path', 'urlTitle', 'title'));
             $data = array('id' => $id, 'userId' => $row['userId'], 'path' => $page['path'] . '/view/' . $row['id'] . '/' . $row['urlTitle'], 'title' => $row['title'], 'content' => $row['text'], 'date' => time());
             $type->addDocument(new Document($id, $data));
         } else {
             if ($actionType == RecordAbstract::DELETE) {
                 // is already deleted
             }
         }
     }
     $type->getIndex()->refresh();
 }
示例#2
0
 /**
  * Shuts one of two nodes down (if two available)
  */
 public function testShutdown()
 {
     $client = $this->_getClient();
     $nodes = $client->getCluster()->getNodes();
     $count = count($nodes);
     if ($count < 2) {
         $this->markTestSkipped('At least two nodes have to be running, because 1 node is shutdown');
     }
     // Store node info of node with port 9200 for later
     foreach ($nodes as $key => $node) {
         if ($node->getInfo()->getPort() == 9200) {
             $info = $node->getInfo();
             unset($nodes[$key]);
         }
     }
     // Select one of the not port 9200 nodes and shut it down
     $node = array_shift($nodes);
     $node->shutdown('2s');
     // Wait until node is shutdown
     sleep(5);
     // Use still existing node
     $client = new Client(array('host' => $info->getIp(), 'port' => $info->getPort()));
     $names = $client->getCluster()->getNodeNames();
     // One node less ...
     $this->assertEquals($count - 1, count($names));
 }
 /**
  * Making a request by giving in raw json as the query
  * @param  jsonstring  $query       Raw json query
  * @return array                    An array containing the mapped entities.
  */
 public function requestByType($query, $type = 'organisation,vacancy,person', $requestType = Request::GET)
 {
     $client = new Client(array('host' => $this->es_host, 'port' => $this->es_port));
     $path = $this->getIndex() . '/' . $type . '/_search';
     $response = $client->request($path, $requestType, $query)->getData();
     return $this->esMapper->getEntities($response['hits']['hits']);
 }
 /**
  * До любых других действий
  */
 public function initialize()
 {
     $currentActionName = $this->dispatcher->getActiveMethod();
     $annotations = $this->annotations->getMethod(self::class, $currentActionName);
     if ($annotations->has('actionInfo')) {
         $annotation = $annotations->get('actionInfo');
         $actionTitle = $annotation->getNamedArgument('name');
         $this->log->info('Запустили: {actionTitle}', ['actionTitle' => $actionTitle]);
     } else {
         $currentTaskName = $this->dispatcher->getTaskName();
         $this->log->info('Запустили: {currentTaskName}::{currentActionName}', ['currentTaskName' => $currentTaskName, 'currentActionName' => $currentActionName]);
     }
     $this->indexName = $this->dispatcher->getParam('index', 'string', false);
     $this->typeName = $this->dispatcher->getParam('type', 'string', false);
     if (!$this->indexName) {
         $this->log->error('Указание индекса является обязательным параметром');
         die;
     }
     $this->sizePerShard = $this->dispatcher->getParam('sizePerShard', 'int', false) ?: $this->sizePerShard;
     $this->elasticsearchHost = $this->dispatcher->getParam('host', 'string', false) ?: $this->elasticsearchHost;
     $this->elasticsearchPort = $this->dispatcher->getParam('port', 'int', false) ?: $this->elasticsearchPort;
     $connectParams = ['host' => $this->elasticsearchHost, 'port' => $this->elasticsearchPort];
     $this->client = new Client($connectParams);
     try {
         $this->client->getStatus();
     } catch (\Elastica\Exception\Connection\HttpException $e) {
         $context = ['host' => $this->elasticsearchHost, 'port' => $this->elasticsearchPort];
         $this->log->error('Подключение к серверу elasticsearch отсутствует: http://{host}:{port}', $context);
         die;
     }
     $this->elasticaIndex = $this->client->getIndex($this->indexName);
     $this->elasticaType = $this->elasticaIndex->getType($this->typeName);
 }
示例#5
0
 public function testDeleteIndex()
 {
     $index = $this->getMockBuilder('Elastica\\Index')->disableOriginalConstructor()->setMethods(array('delete'))->getMock();
     $index->expects($this->once())->method('delete');
     $this->elasticaClient->expects($this->once())->method('getIndex')->with('comments')->will($this->returnValue($index));
     $this->client->deleteIndex('comments');
 }
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $table = new Console\Helper\TableHelper();
     $output->writeln('Cluster overview:');
     $output->writeln('');
     $output->writeln('Nodes:');
     $cluster = $this->elastica->getCluster();
     $table->setHeaders(['name', 'documents', 'node', 'ip', 'port', 'hostname', 'version', 'transport address', 'http address']);
     $nodes = $cluster->getNodes();
     foreach ($nodes as $node) {
         $name = $node->getName();
         $ip = $node->getInfo()->getIp();
         $data = $node->getInfo()->getData();
         $port = $node->getInfo()->getPort();
         $stats = $node->getStats()->get();
         //dump($stats->get());exit;
         $table->addRow([$data['name'], $stats['indices']['docs']['count'], $name, $ip, $port, $data['hostname'], $data['version'], $data['transport_address'], $data['http_address']]);
     }
     $table->render($output);
     $table->setRows([]);
     /* INFO */
     $info = $this->elastica->request('', 'GET')->getData();
     $table->setHeaders(['name', 'version', 'status', 'ok']);
     $table->addRow([$info['name'], $info['version']['number'], $info['status'], $info['ok']]);
     $table->render($output);
     $table->setRows([]);
     $output->writeln('');
 }
 /**
  * Handle index creation command
  *
  * @param Client $client
  * @param string $index
  * @param string $alias
  */
 public function handle(Client $client, $index, $alias)
 {
     $config = $this->configurations->get($alias);
     if (null === $config) {
         throw new \InvalidArgumentException();
     }
     $client->getIndex($index)->create($config);
 }
 /**
  * @param $documents
  */
 protected function addDocumentsToPlayListIndex($documents)
 {
     $elasticaClient = new Client();
     $playListIndex = $elasticaClient->getIndex('track_index');
     $trackType = $playListIndex->getType('track');
     $trackType->addDocuments($documents);
     $trackType->getIndex()->refresh();
 }
 /**
  * Handle index creation command
  *
  * @param Client $client
  * @param string $index
  * @param string $type
  * @param string $alias
  */
 public function handle(Client $client, $index, $type, $alias)
 {
     $mapping = $this->configurations->getMapping($alias, $type);
     if (!$mapping) {
         throw new \InvalidArgumentException();
     }
     $client->getIndex($index)->getType($type)->setMapping($mapping);
 }
 /**
  * Handle index creation command
  *
  * @param Client $client
  * @param string $index
  * @param string $alias
  */
 public function handle(Client $client, $index, $alias)
 {
     $settings = $this->configurations->getSettings($alias);
     if (null === $settings) {
         throw new \InvalidArgumentException();
     }
     $client->getIndex($index)->setSettings($settings);
 }
 /**
  * Handle index deletion command
  *
  * @param Client $client
  * @param string $index
  *
  * @throws IndexNotFoundException
  */
 public function handle(Client $client, $index)
 {
     $index = $client->getIndex($index);
     if (!$index->exists()) {
         throw new IndexNotFoundException($index);
     }
     $index->delete();
 }
示例#12
0
 /**
  * @return void
  */
 private function checkConnectToSearch()
 {
     try {
         $this->client->getStatus();
     } catch (\Exception $e) {
         $this->addDysfunction(self::HEALTH_MESSAGE_UNABLE_TO_CONNECT_TO_SEARCH);
         $this->addDysfunction($e->getMessage());
     }
 }
示例#13
0
 /**
  * @param array $options
  */
 public function __construct($options = [])
 {
     parent::__construct($options);
     $this->client = new Client($options);
     if (!isset($options['index'])) {
         $options['index'] = 'log';
     }
     $this->index = $this->client->getIndex($options['index']);
 }
 /**
  * @param array $payload
  *
  * @return bool
  */
 public function persist(array $payload)
 {
     $documents = array_map(function (array $user) {
         return $this->makeDocument($user);
     }, $payload);
     $responseSet = $this->elasticaClient->getIndex($this->target->index)->addDocuments($documents);
     $this->responses = $responseSet;
     return true;
 }
 /**
  * Constructor
  *
  * @param Client  $client    The elastic search client.
  * @param string  $indexName The elastic search index name.
  * @param boolean $delete    Delete the index if already exist (default = false).
  */
 public function __construct(Client $client, $indexName, $delete = false)
 {
     $this->client = $client;
     $this->index = $client->getIndex($indexName);
     // Checks if the given index is already created
     if (!$this->index->exists($indexName)) {
         // Create the index.
         $this->index->create(array(), $delete);
     }
 }
 public function testBasicGettingStarted()
 {
     $client = new \Elastica\Client();
     $index = $client->getIndex('ruflin');
     $type = $index->getType('users');
     $id = 2;
     $data = array('firstname' => 'Nicolas', 'lastname' => 'Ruflin');
     $doc = new \Elastica\Document($id, $data);
     $type->addDocument($doc);
 }
示例#17
0
 /**
  * @expectedException \Elastica\Exception\ResponseException
  */
 public function testInvalidElasticRequest()
 {
     $connection = new Connection();
     $connection->setHost('localhost');
     $connection->setPort(9500);
     $connection->setTransport('Thrift');
     $client = new Client();
     $client->addConnection($connection);
     $index = new Index($client, 'missing_index');
     $index->getStatus();
 }
示例#18
0
文件: Setup.php 项目: visapi/amun
 public function postInstall(RecordInterface $record)
 {
     $client = new Client(array('host' => $this->registry['search.host'], 'port' => $this->registry['search.port']));
     $index = $client->getIndex('amun');
     $index->create();
     $type = $index->getType('page');
     $mapping = new Mapping();
     $mapping->setType($type);
     $mapping->setProperties(array('id' => array('type' => 'string', 'include_in_all' => false), 'userId' => array('type' => 'integer', 'include_in_all' => false), 'path' => array('type' => 'string', 'include_in_all' => true), 'title' => array('type' => 'string', 'include_in_all' => true), 'content' => array('type' => 'string', 'include_in_all' => true), 'date' => array('type' => 'date', 'include_in_all' => false)));
     $mapping->send();
 }
示例#19
0
 public static function setUpBeforeClass()
 {
     $host = getenv('ELASTIC_HOST');
     $port = getenv('ELASTIC_PORT') ?: 9200;
     $indexName = getenv('ELASTIC_INDEX') ?: 'pbj_tests';
     if (empty($host) || empty($port)) {
         return;
     }
     $client = new Client(['connections' => [['host' => $host, 'port' => $port]]]);
     self::$index = $client->getIndex($indexName);
     self::createIndex();
 }
示例#20
0
 /**
  * @param \Generated\Shared\Transfer\ElasticsearchIndexDefinitionTransfer $indexDefinitionTransfer
  *
  * @return void
  */
 protected function createIndex(ElasticsearchIndexDefinitionTransfer $indexDefinitionTransfer)
 {
     $index = $this->elasticaClient->getIndex($indexDefinitionTransfer->getIndexName());
     if (!$index->exists()) {
         $this->messenger->info(sprintf('Creating elasticsearch index: "%s"', $indexDefinitionTransfer->getIndexName()));
         $settings = $indexDefinitionTransfer->getSettings();
         $index->create($settings);
     }
     foreach ($indexDefinitionTransfer->getMappings() as $mappingName => $mappingData) {
         $this->sendMapping($index, $mappingName, $mappingData);
     }
 }
示例#21
0
 /**
  * @var Extension
  * @var bool
  */
 public function indexExtension(Extension $extension, $update = false)
 {
     $id = $extension->getName();
     $packageDocument = new \Elastica\Document($id, ['id' => $id, 'name' => $id, 'description' => $extension->getDescription(), 'tags' => $extension->getVersions(), 'keywords' => $extension->getKeywords(), 'stars' => $extension->getStars()]);
     $elasticaIndex = $this->client->getIndex('packages');
     $elasticaType = $elasticaIndex->getType('packages');
     if ($update) {
         $elasticaType->updateDocument($packageDocument);
     } else {
         $elasticaType->addDocument($packageDocument);
     }
     $elasticaType->getIndex()->refresh();
 }
示例#22
0
 public function testWithNoValidConnection()
 {
     $connections = array(new \Elastica\Connection(array('host' => '255.255.255.0', 'timeout' => 2)), new \Elastica\Connection(array('host' => '45.45.45.45', 'port' => '80', 'timeout' => 2)), new \Elastica\Connection(array('host' => '10.123.213.123', 'timeout' => 2)));
     $count = 0;
     $client = new Client(array(), function () use(&$count) {
         ++$count;
     });
     $client->setConnections($connections);
     try {
         $client->request('/_aliases');
         $this->fail('Should throw exception as no connection valid');
     } catch (\Elastica\Exception\ConnectionException $e) {
         $this->assertEquals(count($connections), $count);
     }
 }
示例#23
0
 public function __construct($options, $indexName, $typeName)
 {
     if ($this->client) {
         return $this->client;
     }
     if ($options['debug']) {
         define('DEBUG', true);
     }
     $this->host = $options['host'];
     $this->port = $options['port'];
     $this->client = new \Elastica\Client(['host' => $this->host, 'port' => $this->port]);
     $this->index = $this->client->getIndex($indexName);
     $this->indexName = $indexName;
     $this->typeName = $typeName;
 }
示例#24
0
 /**
  * Get the elasticsearch index being used.
  *
  * @return Index
  */
 protected function newIndex()
 {
     if (!isset($this->client)) {
         $this->client = $this->newClient();
     }
     return $this->client->getIndex($this->config['index']);
 }
示例#25
0
 /**
  * @param \Elastica\Response $response
  *
  * @throws \Elastica\Exception\Bulk\ResponseException
  * @throws \Elastica\Exception\InvalidException
  *
  * @return \Elastica\Bulk\ResponseSet
  */
 protected function _processResponse(Response $response)
 {
     $responseData = $response->getData();
     $actions = $this->getActions();
     $bulkResponses = array();
     if (isset($responseData['items']) && is_array($responseData['items'])) {
         foreach ($responseData['items'] as $key => $item) {
             if (!isset($actions[$key])) {
                 throw new InvalidException('No response found for action #' . $key);
             }
             $action = $actions[$key];
             $opType = key($item);
             $bulkResponseData = reset($item);
             if ($action instanceof AbstractDocumentAction) {
                 $data = $action->getData();
                 if ($data instanceof Document && $data->isAutoPopulate() || $this->_client->getConfigValue(array('document', 'autoPopulate'), false)) {
                     if (!$data->hasId() && isset($bulkResponseData['_id'])) {
                         $data->setId($bulkResponseData['_id']);
                     }
                     if (isset($bulkResponseData['_version'])) {
                         $data->setVersion($bulkResponseData['_version']);
                     }
                 }
             }
             $bulkResponses[] = new BulkResponse($bulkResponseData, $action, $opType);
         }
     }
     $bulkResponseSet = new ResponseSet($response, $bulkResponses);
     if ($bulkResponseSet->hasError()) {
         throw new BulkResponseException($bulkResponseSet);
     }
     return $bulkResponseSet;
 }
 public function testServersArray()
 {
     $client = new Client();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('test');
     $start = microtime(true);
     for ($i = 1; $i <= 10000; $i++) {
         $doc = new Document($i, array('test' => 1));
         $type->addDocument($doc);
     }
     // Refresh index
     $index->refresh();
     $end = microtime(true);
     //echo $end - $start;
 }
示例#27
0
 public function testSearchWithSynonyms()
 {
     $indexSettings = json_decode(file_get_contents(__DIR__ . '/../_data/productIndex.json'), true);
     // custom synonyms
     $synonyms = [];
     $synonyms[] = "tai nghe,headphone";
     $synonyms[] = "chụp hình,chụp ảnh";
     $indexSettings['settings']['index']['analysis']['filter']['name_synonym_filter']['synonyms'] = $synonyms;
     $path = '/test/product/';
     // create index
     $this->client->request($path, Request::PUT, $indexSettings);
     // insert data test
     $productData = json_decode($this->sampleProductJson, true);
     $productNames = ["tai nghe abc", "headphone xyz", "headphone tai nghe", "tai abc nghe", "wrong name", "gậy chụp hình abc", "gậy chụp ảnh xyz", "chụp hình cho trẻ", "công việc chụp ảnh", "ảnh hình"];
     $i = 1;
     foreach ($productNames as $name) {
         $productData['searchable_name'] = $name;
         $this->client->request($path . $i++, Request::PUT, ['searchable_name' => $name, 'type' => 'sample', 'attribute' => 'any attribtue', 'value' => rand(1, 999)]);
     }
     // wait for index
     sleep(3);
     foreach ($synonyms as $syn) {
         $keywords = explode(',', $syn);
         // with unmarked field is using synonyms and not unmarked field not use
         $dataForFirstKeyword = $this->client->request($path . '_search', Request::GET, ['query' => ['match_phrase' => ['searchable_name.unmarked' => $keywords[0]]]])->getData();
         $dataForFirstKeywordNotUnmarked = $this->client->request($path . '_search', Request::GET, ['query' => ['match_phrase' => ['searchable_name' => $keywords[0]]]])->getData();
         // responses of search no synonym and search with synonyms are different
         $this->assertNotEquals($dataForFirstKeyword['hits'], $dataForFirstKeywordNotUnmarked['hits']);
         $dataForSecondKeyword = $this->client->request($path . '_search', Request::GET, ['query' => ['match_phrase' => ['searchable_name.unmarked' => $keywords[1]]]])->getData();
         // test responses of 2 synonym keywords are same
         $this->assertEquals($dataForFirstKeyword['hits'], $dataForSecondKeyword['hits']);
     }
 }
 /**
  * @param array $bannedPlugins
  * @return array
  */
 public function scanAvailablePlugins(array $bannedPlugins = array())
 {
     $this->outputIndented("Scanning available plugins...");
     $result = $this->client->request('_nodes');
     $result = $result->getData();
     $availablePlugins = array();
     $first = true;
     foreach (array_values($result['nodes']) as $node) {
         $plugins = array();
         foreach ($node['plugins'] as $plugin) {
             $plugins[] = $plugin['name'];
         }
         if ($first) {
             $availablePlugins = $plugins;
             $first = false;
         } else {
             $availablePlugins = array_intersect($availablePlugins, $plugins);
         }
     }
     if (count($availablePlugins) === 0) {
         $this->output('none');
     }
     $this->output("\n");
     if (count($bannedPlugins)) {
         $availablePlugins = array_diff($availablePlugins, $bannedPlugins);
     }
     foreach (array_chunk($availablePlugins, 5) as $pluginChunk) {
         $plugins = implode(', ', $pluginChunk);
         $this->outputIndented("\t{$plugins}\n");
     }
     return $availablePlugins;
 }
示例#29
0
 /**
  * @param Document[] $documents
  * @param Closure    $errorHandler
  */
 protected function batchUpdate(array $documents, Closure $errorHandler)
 {
     $count = count($documents);
     if ($count === 0) {
         return;
     }
     if ($this->verbose) {
         array_map(function (Document $document) {
             $data = $document->getData();
             dump($data);
         }, $documents);
     }
     $this->validateFields($documents);
     try {
         $responseSet = $this->elastica->updateDocuments($documents);
         $failedSnsidList = [];
         $versionList = [];
         foreach ($responseSet as $response) {
             $this->parseResponse($response, $failedSnsidList, $versionList);
         }
         if ($failedSnsidList) {
             call_user_func($errorHandler, $failedSnsidList);
         }
         if (is_callable($this->versionHandler)) {
             call_user_func($this->versionHandler, $versionList);
         }
     } catch (\Exception $exception) {
         if (is_callable($this->errorHandler)) {
             call_user_func($this->errorHandler, $exception);
         }
         $this->elastica = $this->makeElastica();
         // Try to keep going on updating
     }
 }
示例#30
0
 protected function _log($context)
 {
     if ($context instanceof Request) {
         $this->_lastRequest = $context;
     }
     parent::_log($context);
 }