Since: 2.0
Author: Carsten Brandt (mail@cebe.cc)
Inheritance: extends yii\base\Component
 /**
  * @param  boolean    $reset whether to clean up the test database
  * @return Connection
  */
 public function getConnection($reset = true)
 {
     $databases = self::getParam('databases');
     $params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : [];
     $db = new Connection();
     if ($reset) {
         $db->open();
     }
     return $db;
 }
 public function testOpen()
 {
     $connection = new Connection();
     $connection->autodetectCluster;
     $connection->nodes = [['http_address' => 'inet[/127.0.0.1:9200]']];
     $this->assertNull($connection->activeNode);
     $connection->open();
     $this->assertNotNull($connection->activeNode);
     $this->assertArrayHasKey('name', reset($connection->nodes));
     //        $this->assertArrayHasKey('hostname', reset($connection->nodes));
     $this->assertArrayHasKey('version', reset($connection->nodes));
     $this->assertArrayHasKey('http_address', reset($connection->nodes));
 }
Exemplo n.º 3
0
 public function init()
 {
     parent::init();
     if (is_null($this->index)) {
         throw new InvalidParamException('Property "index" must be set');
     }
 }
Exemplo n.º 4
0
 /**
  * Executes the bulk command.
  * @return mixed
  * @throws yii\base\InvalidCallException
  */
 public function execute()
 {
     //valid endpoints are /_bulk, /{index}/_bulk, and {index}/{type}/_bulk
     if ($this->index === null && $this->type === null) {
         $endpoint = ['_bulk'];
     } elseif ($this->index !== null && $this->type === null) {
         $endpoint = [$this->index, '_bulk'];
     } elseif ($this->index !== null && $this->type !== null) {
         $endpoint = [$this->index, $this->type, '_bulk'];
     } else {
         throw new InvalidCallException('Invalid endpoint: if type is defined, index must be defined too.');
     }
     if (empty($this->actions)) {
         $body = '{}';
     } elseif (is_array($this->actions)) {
         $body = '';
         foreach ($this->actions as $action) {
             $body .= Json::encode($action) . "\n";
         }
     } else {
         $body = $this->actions;
     }
     return $this->db->post($endpoint, $this->options, $body);
 }
Exemplo n.º 5
0
 public function testGetDb()
 {
     $this->mockApplication(['components' => ['elasticsearch' => Connection::className()]]);
     $this->assertInstanceOf(Connection::className(), ActiveRecord::getDb());
 }
Exemplo n.º 6
0
 /**
  * @param $name
  * @return mixed
  * @see http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
  */
 public function getTemplate($name)
 {
     return $this->db->get(['_template', $name]);
 }
Exemplo n.º 7
0
 /**
  * Insert or Update a document into the index.
  */
 public function InsertUpdate()
 {
     // Remove all new lines etc. from the json to make it valid
     $this->query = str_replace(array('\\n', '\\r'), '', $this->query);
     $connection = new Connection();
     $command = $connection->createCommand();
     $command->insert('api-builder', 'api', $this->query, $this->api->id);
 }
 /**
  * This method will initialize the [[elasticsearch]] property to make sure it refers to a valid Elasticsearch connection.
  * @throws InvalidConfigException if [[elasticsearch]] is invalid.
  */
 public function init()
 {
     parent::init();
     $this->db = Instance::ensure($this->db, Connection::className());
 }
Exemplo n.º 9
0
 /**
  * Delete a document from the index.
  */
 public function Delete()
 {
     $connection = new Connection();
     $command = $connection->createCommand();
     $command->delete('api-builder', 'api', $this->id);
 }