getIndex() public method

Returns the index for the given connection.
public getIndex ( string $name ) : Index
$name string Index name to create connection to
return Index Index for the given name
 /**
  * До любых других действий
  */
 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);
 }
 /**
  * @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;
 }
Beispiel #3
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']);
 }
 /**
  * @return Index
  */
 private function getIndex()
 {
     if (null === $this->index) {
         $this->index = $this->client->getIndex($this->indexName);
         if (!$this->index->exists()) {
             $this->index->create();
         }
     }
     return $this->index;
 }
Beispiel #5
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);
     }
 }
Beispiel #6
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();
 }
 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;
 }
Beispiel #8
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']);
 }
Beispiel #9
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();
 }
 /**
  * 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 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 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();
 }
 /**
  * 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);
 }
 /**
  * @return Index
  * @throws ElasticaManagerIndexNotFoundException
  */
 public function getIndexByAlias()
 {
     $defaultAlias = $this->getDefaultAlias();
     if (!$this->hasAlias($defaultAlias)) {
         throw new ElasticaManagerIndexNotFoundException($defaultAlias, true);
     }
     $elasticaIndex = $this->client->getIndex($defaultAlias);
     return $elasticaIndex;
 }
 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);
 }
 /**
  * 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);
     }
 }
 /**
  * Initialize the ElasticSearch client, and setup settings for the client.
  * 
  * @return void
  */
 public function initialize()
 {
     spl_autoload_register(array($this, 'autoLoad'));
     $this->_connectionOptions = array('url' => $this->modx->getOption('sisea.elastic.hostname', null, 'http://127.0.0.1') . ':' . $this->modx->getOption('sisea.elastic.port', null, 9200) . '/');
     try {
         $this->client = new \Elastica\Client($this->_connectionOptions);
         $this->index = $this->client->getIndex(strtolower($this->modx->getOption('sisea.elastic.index', null, 'siplesearchindex')));
         if (!$this->index->exists()) {
             $indexSetup = $this->modx->getObject('modSnippet', array('name' => 'SimpleSearchElasticIndexSetup'));
             if ($indexSetup) {
                 $indexOptions = $this->modx->fromJSON($this->modx->runSnippet('SimpleSearchElasticIndexSetup'));
             } else {
                 $indexOptions = $this->modx->fromJSON($this->modx->runSnippet('SimpleSearchElasticIndexSetup_default'));
             }
             $this->index->create($indexOptions, true);
         }
     } catch (Exception $e) {
         $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Error connecting to ElasticSearch server: ' . $e->getMessage());
     }
 }
Beispiel #19
0
 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();
 }
 /**
  * @param AMQPMessage $message
  * @return bool
  */
 public function process(AMQPMessage $message)
 {
     $ids = Json::decode((string) $message->body, Json::FORCE_ARRAY);
     /** @var Screenplay[] $documents */
     $documents = [];
     foreach ($ids as $id) {
         /** @var Screenplay|NULL $screenplay */
         $screenplay = $this->scenariosRepository->find($id);
         if ($screenplay === NULL) {
             $this->onError("Screenplay with ID #{$id} not found!");
             return FALSE;
         }
         $documents[] = $this->createDocument($screenplay);
     }
     /** @var Index $indexEntity */
     $indexEntity = $this->indicesRepository->findOneBy([], ['createdAt' => 'DESC']);
     $index = $this->client->getIndex($indexEntity->getName());
     $type = $index->getType('screenplay');
     $type->addDocuments($documents);
     return TRUE;
 }
Beispiel #21
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();
 }
 /**
  * @param string[] $add Array of indices to add
  * @param string[] $remove Array of indices to remove
  * @return Status
  */
 protected function updateIndices(array $add, array $remove)
 {
     $client = $this->client;
     $remove = array_filter($remove, function ($name) use($client) {
         return $client->getIndex($name)->exists();
     });
     if ($remove) {
         $this->outputIndented("\tRemoving old indices...\n");
         foreach ($remove as $indexName) {
             $this->outputIndented("\t\t{$indexName}...");
             $this->client->getIndex($indexName)->delete();
             $this->output("done\n");
         }
     }
     return Status::newGood();
 }
 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;
 }
 protected function setUp()
 {
     $typeName = Cache::TYPE_NAME;
     $this->type = $this->prophesize('\\Elastica\\Type');
     $this->type->request(Argument::any(), Argument::cetera())->willReturn(true);
     $this->type->getName()->willReturn($typeName);
     $this->index = $this->prophesize('\\Elastica\\Index');
     $this->index->getType($typeName)->willReturn($this->type->reveal());
     $this->index->exists()->willReturn(true);
     $nsDoc = new Document('DoctrineNamespaceCacheKey[]', [Cache::VALUE_FIELD => serialize($this->namespaceId)]);
     $this->type->getIndex()->willReturn($this->index->reveal());
     $this->type->getDocument("DoctrineNamespaceCacheKey[]")->willReturn($nsDoc);
     $this->client = $this->prophesize('\\Elastica\\Client');
     $this->client->getIndex($this->indexName)->willReturn($this->index->reveal());
     $this->cache = new Cache($this->client->reveal(), ['index' => $this->indexName]);
 }
Beispiel #25
0
 public function testTwoServersSame()
 {
     // Creates a new index 'xodoa' and a type 'user' inside this index
     $client = new Client(array('connections' => array(array('host' => 'localhost', 'port' => 9200), array('host' => 'localhost', 'port' => 9200))));
     $index = $client->getIndex('elastica_test1');
     $index->create(array(), true);
     $type = $index->getType('user');
     // Adds 1 document to the index
     $doc1 = new Document(1, array('username' => 'hans', 'test' => array('2', '3', '5')));
     $type->addDocument($doc1);
     // Adds a list of documents with _bulk upload to the index
     $docs = array();
     $docs[] = new Document(2, array('username' => 'john', 'test' => array('1', '3', '6')));
     $docs[] = new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7')));
     $type->addDocuments($docs);
     // Refresh index
     $index->refresh();
     $resultSet = $type->search('rolf');
 }
 /**
  * Some memory usage stats
  *
  * Really simple and quite stupid ...
  */
 public function testServersArray()
 {
     $client = new Client();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('test');
     $data = array('text1' => 'Very long text for a string', 'text2' => 'But this is not very long', 'text3' => 'random or not random?');
     $startMemory = memory_get_usage();
     for ($n = 1; $n < 10; $n++) {
         $docs = array();
         for ($i = 1; $i <= 3000; $i++) {
             $docs[] = new Document(uniqid(), $data);
         }
         $type->addDocuments($docs);
         $docs = array();
     }
     unset($docs);
     $endMemory = memory_get_usage();
     $this->assertLessThan(1.2, $endMemory / $startMemory);
 }
Beispiel #27
0
 /**
  * @httpMethod GET
  * @path /
  */
 public function doIndex()
 {
     $url = new Url($this->base->getSelf());
     $count = $url->getParam('count') > 0 ? $url->getParam('count') : 8;
     $count = $count > 16 ? 16 : $count;
     $search = $this->get->search('string');
     if (!empty($search)) {
         $search = strlen($search) > 64 ? substr($search, 0, 64) : $search;
         $queryString = new QueryString();
         //$queryString->setDefaultOperator('AND');
         $queryString->setQuery($search);
         $query = new Query();
         $query->setQuery($queryString);
         $query->setFrom($url->getParam('startIndex'));
         $query->setLimit($count);
         $query->setHighlight(array('pre_tags' => array('<mark>'), 'post_tags' => array('</mark>'), 'fields' => array('title' => new \stdClass(), 'content' => new \stdClass())));
         // get elasticsearch client
         $client = new Client(array('host' => $this->registry['search.host'], 'port' => $this->registry['search.port']));
         $index = $client->getIndex('amun');
         $searchResult = $index->search($query);
         $result = new ResultSet($searchResult->getTotalHits(), $url->getParam('startIndex'), $count);
         foreach ($searchResult as $row) {
             $data = $row->getData();
             $data['url'] = $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . $data['path'];
             $data['date'] = new DateTime('@' . $data['date']);
             // if we have an highlite overwrite the title or content
             $highlights = $row->getHighlights();
             if (isset($highlights['title'])) {
                 $data['title'] = implode(' ... ', $highlights['title']);
             }
             if (isset($highlights['content'])) {
                 $data['content'] = implode(' ... ', $highlights['content']);
             }
             $result->addData($data);
         }
         $this->template->assign('resultSearch', $result);
         $paging = new Paging($url, $result);
         $this->template->assign('pagingSearch', $paging, 0);
         return $result;
     }
 }
 public function testUnicodeData()
 {
     $client = new \Elastica\Client();
     $index = $client->getIndex('curl_test');
     $type = $index->getType('item');
     // Force HEAD request to set CURLOPT_NOBODY = true
     $index->exists();
     $id = 22;
     $data = array('id' => $id, 'name' => '
         Сегодня, я вижу, особенно грустен твой взгляд, /
         И руки особенно тонки, колени обняв. /
         Послушай: далеко, далеко, на озере Чад /
         Изысканный бродит жираф.');
     $doc = new \Elastica\Document($id, $data);
     $type->addDocument($doc);
     $index->refresh();
     $doc = $type->getDocument($id);
     // Document should be retrieved correctly
     $this->assertSame($data, $doc->getData());
     $this->assertEquals($id, $doc->getId());
 }
Beispiel #29
0
 /**
  * @dataProvider configProvider
  */
 public function testSearchRequest($config)
 {
     // Creates a new index 'xodoa' and a type 'user' inside this index
     $client = new Client($config);
     $index = $client->getIndex('elastica_test1');
     $index->create(array(), true);
     $type = $index->getType('user');
     // Adds 1 document to the index
     $doc1 = new Document(1, array('username' => 'hans', 'test' => array('2', '3', '5')));
     $doc1->setVersion(0);
     $type->addDocument($doc1);
     // Adds a list of documents with _bulk upload to the index
     $docs = array();
     $docs[] = new Document(2, array('username' => 'john', 'test' => array('1', '3', '6')));
     $docs[] = new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7')));
     $type->addDocuments($docs);
     // Refresh index
     $index->refresh();
     $resultSet = $type->search('rolf');
     $this->assertEquals(1, $resultSet->getTotalHits());
 }
 /**
  * @param array $cities
  * @return \Elastica\Response
  */
 public function run(array $cities)
 {
     $client = new Client();
     //Create a new index
     $index = $client->getIndex(self::INDEX_NAME);
     $index->create(array(), true);
     //Set type
     $type = $index->getType(self::TYPE_NAME);
     //Add Document
     $documents = array();
     foreach ($cities as $city) {
         // Fetching content from the database
         $documents[] = new Document($city['ID'], array('name' => $city['Name']));
     }
     $type->addDocuments($documents);
     $index->refresh();
     //Example simple query
     $query = '{"query":{"query_string":{"query":"Eindhoven"}}}';
     $path = $index->getName() . '/' . $type->getName() . '/_search';
     $response = $client->request($path, Request::GET, $query);
     return $response;
 }