/** * @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)); }
public function init() { parent::init(); if (is_null($this->index)) { throw new InvalidParamException('Property "index" must be set'); } }
/** * 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); }
public function testGetDb() { $this->mockApplication(['components' => ['elasticsearch' => Connection::className()]]); $this->assertInstanceOf(Connection::className(), ActiveRecord::getDb()); }
/** * @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]); }
/** * 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()); }
/** * Delete a document from the index. */ public function Delete() { $connection = new Connection(); $command = $connection->createCommand(); $command->delete('api-builder', 'api', $this->id); }