Class yii\elasticsearch\BulkCommand

Inheritanceyii\elasticsearch\BulkCommand » yii\base\Component
Available since extension's version2.0.5
Source Code https://github.com/yiisoft/yii2-elasticsearch/blob/master/BulkCommand.php

The yii\elasticsearch\BulkCommand class implements the API for accessing the Elasticsearch bulk REST API.

Further details on bulk API is available in Elasticsearch guide.

Public Properties

Hide inherited properties

Property Type Description Defined By
$actions array|string Actions to be executed in this bulk command, given as either an array of arrays or as one newline-delimited string. yii\elasticsearch\BulkCommand
$db yii\elasticsearch\Connection yii\elasticsearch\BulkCommand
$index string Default index to execute the queries on. yii\elasticsearch\BulkCommand
$options array Options to be appended to the query URL. yii\elasticsearch\BulkCommand
$type string Default type to execute the queries on. yii\elasticsearch\BulkCommand

Public Methods

Hide inherited methods

Method Description Defined By
addAction() Adds an action to the command. Will overwrite existing actions if they are specified as a string. yii\elasticsearch\BulkCommand
addDeleteAction() Adds a delete action to the command. yii\elasticsearch\BulkCommand
execute() Executes the bulk command. yii\elasticsearch\BulkCommand

Property Details

Hide inherited properties

$actions public property

Actions to be executed in this bulk command, given as either an array of arrays or as one newline-delimited string. All actions except delete span two lines.

public array|string $actions null
$db public property
$index public property

Default index to execute the queries on. Defaults to null meaning that index needs to be specified in every action.

public string $index null
$options public property

Options to be appended to the query URL.

public array $options = []
$type public property

Default type to execute the queries on. Defaults to null meaning that type needs to be specified in every action.

public string $type null

Method Details

Hide inherited methods

addAction() public method

Adds an action to the command. Will overwrite existing actions if they are specified as a string.

See also https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-bulk.html.

public void addAction ( $line1, $line2 null )
$line1 array

First action expressed as an array (will be encoded to JSON automatically).

$line2 array|null

Second action expressed as an array (will be encoded to JSON automatically).

                public function addAction($line1, $line2 = null)
{
    if (!is_array($this->actions)) {
        $this->actions = [];
    }
    $this->actions[] = $line1;
    if ($line2 !== null) {
        $this->actions[] = $line2;
    }
}

            
addDeleteAction() public method

Adds a delete action to the command.

public void addDeleteAction ( $id, $index null, $type null )
$id string

Document ID

$index string|null

Index that the document belongs to. Can be set to null if the command has a default index (yii\elasticsearch\BulkCommand::$index) assigned.

$type string|null

Type that the document belongs to. Can be set to null if the command has a default type (yii\elasticsearch\BulkCommand::$type) assigned.

                public function addDeleteAction($id, $index = null, $type = null)
{
    $actionData = ['_id' => $id];
    if (!empty($index)) {
        $actionData['_index'] = $index;
    }
    if (!empty($type)) {
        $actionData['_type'] = $type;
    }
    $this->addAction(['delete' => $actionData]);
}

            
execute() public method

Executes the bulk command.

public mixed execute ( )
throws \yii\base\InvalidCallException

                public function execute()
{
    //valid endpoints are /_bulk, /{index}/_bulk, and {index}/{type}/_bulk
    //for ES7+ type is omitted
    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) {
        if ($this->db->dslVersion >= 7) {
            $endpoint = [$this->index, '_bulk'];
        } else {
            $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);
}