PHP Class yii\elasticsearch\Connection

Since: 2.0
Author: Carsten Brandt ([email protected])
Inheritance: extends yii\base\Component
Show file Open project: yiisoft/yii2-elasticsearch Class Usage Examples

Public Properties

Property Type Description
$activeNode the active node. Key of one of the [[nodes]]. Will be randomly selected on Connection::open.
$auth Authentication data used to connect to the ElasticSearch node. Array elements: - username: the username for authentication. - password: the password for authentication. Array either MUST contain both username and password on not contain any authentication credentials.
$autodetectCluster whether to autodetect available cluster nodes on Connection::open
$connectionTimeout timeout to use for connecting to an elasticsearch node. This value will be used to configure the curl CURLOPT_CONNECTTIMEOUT option. If not set, no explicit timeout will be set for curl.
$dataTimeout timeout to use when reading the response from an elasticsearch node. This value will be used to configure the curl CURLOPT_TIMEOUT option. If not set, no explicit timeout will be set for curl.
$defaultProtocol If [[autodetectCluster]] is true, all nodes received from cluster will be set to use the protocol defined by [[defaultProtocol]]
$nodes The elasticsearch cluster nodes to connect to. This is populated with the result of a cluster nodes request when [[autodetectCluster]] is true. Additional special options: - auth: overrides [[auth]] property. For example: php [ 'http_address' => 'inet[/127.0.0.1:9200]', 'auth' => ['username' => 'yiiuser', 'password' => 'yiipw'], // Overrides the auth property of the class with specific login and password 'auth' => ['username' => 'yiiuser', 'password' => 'yiipw'], // Disabled auth regardless of auth property of the class ] - protocol: explicitly sets the protocol for the current node (useful when manually defining a HTTPS cluster)

Public Methods

Method Description
__sleep ( ) : array Closes the connection when this component is being serialized.
close ( ) Closes the currently active DB connection.
createBulkCommand ( array $config = [] ) : BulkCommand Creates a bulk command for execution.
createCommand ( array $config = [] ) : Command Creates a command for execution.
delete ( string | array $url, array $options = [], string $body = null, boolean $raw = false ) : mixed Performs DELETE HTTP request
get ( string | array $url, array $options = [], string $body = null, boolean $raw = false ) : mixed Performs GET HTTP request
getClusterState ( )
getDriverName ( ) : string Returns the name of the DB driver for the current [[dsn]].
getIsActive ( ) : boolean Returns a value indicating whether the DB connection is established.
getNodeInfo ( )
getQueryBuilder ( ) : QueryBuilder Creates new query builder instance
head ( string | array $url, array $options = [], string $body = null ) : mixed Performs HEAD HTTP request
init ( )
open ( ) Establishes a DB connection.
post ( string | array $url, array $options = [], string $body = null, boolean $raw = false ) : mixed Performs POST HTTP request
put ( string | array $url, array $options = [], string $body = null, boolean $raw = false ) : mixed Performs PUT HTTP request

Protected Methods

Method Description
decodeErrorBody ( $body ) : mixed Try to decode error information if it is valid json, return it if not.
httpRequest ( string $method, string $url, string $requestBody = null, boolean $raw = false ) : mixed Performs HTTP request
initConnection ( ) Initializes the DB connection.
populateNodes ( ) Populates [[nodes]] with the result of a cluster nodes request.
selectActiveNode ( ) select active node randomly

Private Methods

Method Description
createUrl ( string | array $path, array $options = [] ) : array Creates URL
resetCurlHandle ( )

Method Details

__sleep() public method

Closes the connection when this component is being serialized.
public __sleep ( ) : array
return array

close() public method

It does nothing if the connection is already closed.
public close ( )

createBulkCommand() public method

Creates a bulk command for execution.
Since: 2.0.5
public createBulkCommand ( array $config = [] ) : BulkCommand
$config array the configuration for the [[BulkCommand]] class
return BulkCommand the DB command

createCommand() public method

Creates a command for execution.
public createCommand ( array $config = [] ) : Command
$config array the configuration for the Command class
return Command the DB command

decodeErrorBody() protected method

Try to decode error information if it is valid json, return it if not.
protected decodeErrorBody ( $body ) : mixed
$body
return mixed

delete() public method

Performs DELETE HTTP request
public delete ( string | array $url, array $options = [], string $body = null, boolean $raw = false ) : mixed
$url string | array URL
$options array URL options
$body string request body
$raw boolean if response body contains JSON and should be decoded
return mixed response

get() public method

Performs GET HTTP request
public get ( string | array $url, array $options = [], string $body = null, boolean $raw = false ) : mixed
$url string | array URL
$options array URL options
$body string request body
$raw boolean if response body contains JSON and should be decoded
return mixed response

getClusterState() public method

public getClusterState ( )

getDriverName() public method

Returns the name of the DB driver for the current [[dsn]].
public getDriverName ( ) : string
return string name of the DB driver

getIsActive() public method

Returns a value indicating whether the DB connection is established.
public getIsActive ( ) : boolean
return boolean whether the DB connection is established

getNodeInfo() public method

public getNodeInfo ( )

getQueryBuilder() public method

Creates new query builder instance
public getQueryBuilder ( ) : QueryBuilder
return QueryBuilder

head() public method

Performs HEAD HTTP request
public head ( string | array $url, array $options = [], string $body = null ) : mixed
$url string | array URL
$options array URL options
$body string request body
return mixed response

httpRequest() protected method

Performs HTTP request
protected httpRequest ( string $method, string $url, string $requestBody = null, boolean $raw = false ) : mixed
$method string method name
$url string URL
$requestBody string request body
$raw boolean if response body contains JSON and should be decoded
return mixed if request failed

init() public method

public init ( )

initConnection() protected method

This method is invoked right after the DB connection is established. The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
protected initConnection ( )

open() public method

It does nothing if a DB connection has already been established.
public open ( )

populateNodes() protected method

Populates [[nodes]] with the result of a cluster nodes request.
Since: 2.0.4
protected populateNodes ( )

post() public method

Performs POST HTTP request
public post ( string | array $url, array $options = [], string $body = null, boolean $raw = false ) : mixed
$url string | array URL
$options array URL options
$body string request body
$raw boolean if response body contains JSON and should be decoded
return mixed response

put() public method

Performs PUT HTTP request
public put ( string | array $url, array $options = [], string $body = null, boolean $raw = false ) : mixed
$url string | array URL
$options array URL options
$body string request body
$raw boolean if response body contains JSON and should be decoded
return mixed response

selectActiveNode() protected method

select active node randomly
protected selectActiveNode ( )

Property Details

$activeNode public property

the active node. Key of one of the [[nodes]]. Will be randomly selected on Connection::open.
public $activeNode

$auth public property

Authentication data used to connect to the ElasticSearch node. Array elements: - username: the username for authentication. - password: the password for authentication. Array either MUST contain both username and password on not contain any authentication credentials.
See also: http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_configuration.html#_example_configuring_http_basic_auth
public $auth

$autodetectCluster public property

whether to autodetect available cluster nodes on Connection::open
public $autodetectCluster

$connectionTimeout public property

timeout to use for connecting to an elasticsearch node. This value will be used to configure the curl CURLOPT_CONNECTTIMEOUT option. If not set, no explicit timeout will be set for curl.
public $connectionTimeout

$dataTimeout public property

timeout to use when reading the response from an elasticsearch node. This value will be used to configure the curl CURLOPT_TIMEOUT option. If not set, no explicit timeout will be set for curl.
public $dataTimeout

$defaultProtocol public property

If [[autodetectCluster]] is true, all nodes received from cluster will be set to use the protocol defined by [[defaultProtocol]]
Since: 2.0.5
public $defaultProtocol

$nodes public property

The elasticsearch cluster nodes to connect to. This is populated with the result of a cluster nodes request when [[autodetectCluster]] is true. Additional special options: - auth: overrides [[auth]] property. For example: php [ 'http_address' => 'inet[/127.0.0.1:9200]', 'auth' => ['username' => 'yiiuser', 'password' => 'yiipw'], // Overrides the auth property of the class with specific login and password 'auth' => ['username' => 'yiiuser', 'password' => 'yiipw'], // Disabled auth regardless of auth property of the class ] - protocol: explicitly sets the protocol for the current node (useful when manually defining a HTTPS cluster)
See also: http://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html#cluster-nodes-info
public $nodes