Class yii\elasticsearch\Command

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

The Command class implements the API for accessing the Elasticsearch REST API.

Check the Elasticsearch guide for details on these commands.

Public Properties

Hide inherited properties

Property Type Description Defined By
$db yii\elasticsearch\Connection yii\elasticsearch\Command
$index string|array The indexes to execute the query on. yii\elasticsearch\Command
$options array Options to be appended to the query URL, such as "search_type" for search or "timeout" for delete yii\elasticsearch\Command
$queryParts array List of arrays or json strings that become parts of a query yii\elasticsearch\Command
$type string|array|null The types to execute the query on. yii\elasticsearch\Command

Public Methods

Hide inherited methods

Method Description Defined By
addAlias() yii\elasticsearch\Command
aliasActions() Runs alias manipulations. yii\elasticsearch\Command
aliasExists() yii\elasticsearch\Command
clearIndexCache() yii\elasticsearch\Command
clearScroll() yii\elasticsearch\Command
closeIndex() yii\elasticsearch\Command
createIndex() Creates an index yii\elasticsearch\Command
createTemplate() yii\elasticsearch\Command
delete() Deletes a document from the index yii\elasticsearch\Command
deleteAllIndexes() Deletes all indexes yii\elasticsearch\Command
deleteByQuery() Sends a request to the delete by query yii\elasticsearch\Command
deleteIndex() Deletes an index yii\elasticsearch\Command
deleteTemplate() yii\elasticsearch\Command
exists() Gets a document from the index yii\elasticsearch\Command
flushIndex() yii\elasticsearch\Command
get() Gets a document from the index yii\elasticsearch\Command
getAliasInfo() yii\elasticsearch\Command
getIndexAliases() yii\elasticsearch\Command
getIndexInfoByAlias() yii\elasticsearch\Command
getIndexRecoveryStats() yii\elasticsearch\Command
getIndexStats() yii\elasticsearch\Command
getIndexesByAlias() yii\elasticsearch\Command
getMapping() yii\elasticsearch\Command
getSource() Gets a documents _source from the index (>=v0.90.1) yii\elasticsearch\Command
getTemplate() yii\elasticsearch\Command
indexExists() Checks whether an index exists yii\elasticsearch\Command
insert() Inserts a document into an index yii\elasticsearch\Command
mget() Gets multiple documents from the index yii\elasticsearch\Command
openIndex() yii\elasticsearch\Command
refreshIndex() yii\elasticsearch\Command
removeAlias() yii\elasticsearch\Command
scroll() yii\elasticsearch\Command
search() Sends a request to the _search API and returns the result yii\elasticsearch\Command
setMapping() yii\elasticsearch\Command
suggest() Sends a suggest request to the _search API and returns the result yii\elasticsearch\Command
typeExists() yii\elasticsearch\Command
update() Updates a document yii\elasticsearch\Command
updateAnalyzers() Define new analyzers for the index. yii\elasticsearch\Command
updateSettings() Change specific index level settings in real time. yii\elasticsearch\Command

Property Details

Hide inherited properties

$db public property
$index public property

The indexes to execute the query on. Defaults to null meaning all indexes

See also https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-multi-index-type.

public string|array $index null
$options public property

Options to be appended to the query URL, such as "search_type" for search or "timeout" for delete

public array $options = []
$queryParts public property

List of arrays or json strings that become parts of a query

public array $queryParts null
$type public property

The types to execute the query on. Defaults to null meaning all types

public string|array|null $type null

Method Details

Hide inherited methods

addAlias() public method
public boolean addAlias ( $index, $alias, $aliasParameters = [] )
$index string

Index that the document belongs to.

$alias string
$aliasParameters array
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function addAlias($index, $alias, $aliasParameters = [])
{
    return (bool)$this->db->put([$index, '_alias', $alias], [], json_encode((object)$aliasParameters));
}

            
aliasActions() public method

Runs alias manipulations.

If you want to add alias1 to index1 and remove alias2 from index2 you can use following commands: ~~~ $actions = [

 ['add' => ['index' => 'index1', 'alias' => 'alias1']],
 ['remove' => ['index' => 'index2', 'alias' => 'alias2']],

]; ~~~

See also https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#indices-aliases.

public boolean aliasActions ( array $actions )
$actions array
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function aliasActions(array $actions)
{
    return (bool)$this->db->post(['_aliases'], [], json_encode(['actions' => $actions]));
}

            
aliasExists() public method

public boolean aliasExists ( $alias )
$alias string
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function aliasExists($alias)
{
    $indexes = $this->getIndexesByAlias($alias);
    return !empty($indexes);
}

            
clearIndexCache() public method
public mixed clearIndexCache ( $index )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function clearIndexCache($index)
{
    return $this->db->post([$index, '_cache', 'clear']);
}

            
clearScroll() public method (available since version 2.0.4)
public mixed clearScroll ( $options = [] )
$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function clearScroll($options = [])
{
    $body = array_filter([
        'scroll_id' => ArrayHelper::remove($options, 'scroll_id', null),
    ]);
    if (empty($body)) {
        $body = (object) [];
    }
   return $this->db->delete(['_search', 'scroll'], $options, Json::encode($body));
}

            
closeIndex() public method
public mixed closeIndex ( $index )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function closeIndex($index)
{
    return $this->db->post([$index, '_close']);
}

            
createIndex() public method
public mixed createIndex ( $index, $configuration null )
$index string

Index that the document belongs to.

$configuration null|array
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function createIndex($index, $configuration = null)
{
    $body = $configuration !== null ? Json::encode($configuration) : null;
    return $this->db->put([$index], [], $body);
}

            
createTemplate() public method
public mixed createTemplate ( $name, $pattern, $settings, $mappings, $order 0 )
$name
$pattern
$settings
$mappings
$order integer
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function createTemplate($name, $pattern, $settings, $mappings, $order = 0)
{
    $body = Json::encode([
        'template' => $pattern,
        'order' => $order,
        'settings' => (object) $settings,
        'mappings' => (object) $mappings,
    ]);
    return $this->db->put(['_template', $name], [], $body);
}

            
delete() public method
public mixed delete ( $index, $type, $id, $options = [] )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

$id string

The documents id.

$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function delete($index, $type, $id, $options = [])
{
    if ($this->db->dslVersion >= 7) {
        return $this->db->delete([$index, '_doc', $id], $options);
    } else {
        return $this->db->delete([$index, $type, $id], $options);
    }
}

            
deleteAllIndexes() public method
public mixed deleteAllIndexes ( )
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function deleteAllIndexes()
{
    return $this->db->delete(['_all']);
}

            
deleteByQuery() public method

Sends a request to the delete by query

public mixed deleteByQuery ( $options = [] )
$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function deleteByQuery($options = [])
{
    if (!isset($this->queryParts['query'])) {
        throw new InvalidCallException('Can not call deleteByQuery when no query is given.');
    }
    $query = [
        'query' => $this->queryParts['query'],
    ];
    if (isset($this->queryParts['filter'])) {
        $query['filter'] = $this->queryParts['filter'];
    }
    $query = Json::encode($query);
    $url = [$this->index !== null ? $this->index : '_all'];
    if ($this->type !== null) {
        $url[] = $this->type;
    }
    $url[] = '_delete_by_query';
    return $this->db->post($url, array_merge($this->options, $options), $query);
}

            
deleteIndex() public method
public mixed deleteIndex ( $index )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function deleteIndex($index)
{
    return $this->db->delete([$index]);
}

            
deleteTemplate() public method
public mixed deleteTemplate ( $name )
$name
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function deleteTemplate($name)
{
    return $this->db->delete(['_template', $name]);
}

            
exists() public method
public mixed exists ( $index, $type, $id )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

$id string

The documents id.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function exists($index, $type, $id)
{
    if ($this->db->dslVersion >= 7) {
        return $this->db->head([$index, '_doc', $id]);
    } else {
        return $this->db->head([$index, $type, $id]);
    }
}

            
flushIndex() public method
public mixed flushIndex ( $index '_all' )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function flushIndex($index = '_all')
{
    return $this->db->post([$index, '_flush']);
}

            
get() public method
public mixed get ( $index, $type, $id, $options = [] )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

$id string

The documents id.

$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function get($index, $type, $id, $options = [])
{
    if ($this->db->dslVersion >= 7) {
        return $this->db->get([$index, '_doc', $id], $options);
    } else {
        return $this->db->get([$index, $type, $id], $options);
    }
}

            
getAliasInfo() public method
public array getAliasInfo ( )
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getAliasInfo()
{
    $aliasInfo = $this->db->get(['_alias', '*']);
    return $aliasInfo ?: [];
}

            
getIndexAliases() public method
public array getIndexAliases ( $index )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getIndexAliases($index)
{
    $responseData = $this->db->get([$index, '_alias', '*']);
    if (empty($responseData)) {
        return [];
    }
    return $responseData[$index]['aliases'];
}

            
getIndexInfoByAlias() public method
public array getIndexInfoByAlias ( $alias )
$alias string
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getIndexInfoByAlias($alias)
{
    $responseData = $this->db->get(['_alias', $alias]);
    if (empty($responseData)) {
        return [];
    }
    return $responseData;
}

            
getIndexRecoveryStats() public method
public mixed getIndexRecoveryStats ( $index '_all' )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getIndexRecoveryStats($index = '_all')
{
    return $this->db->get([$index, '_recovery']);
}

            
getIndexStats() public method
public mixed getIndexStats ( $index '_all' )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getIndexStats($index = '_all')
{
    return $this->db->get([$index, '_stats']);
}

            
getIndexesByAlias() public method

public array getIndexesByAlias ( $alias )
$alias string
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getIndexesByAlias($alias)
{
    return array_keys($this->getIndexInfoByAlias($alias));
}

            
getMapping() public method
public mixed getMapping ( $index '_all', $type null )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getMapping($index = '_all', $type = null)
{
    $url = [$index, '_mapping'];
    if ($this->db->dslVersion < 7 && $type !== null) {
        $url[] = $type;
    }
    return $this->db->get($url);
}

            
getSource() public method

Gets a documents _source from the index (>=v0.90.1)

See also https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source.

public mixed getSource ( $index, $type, $id )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

$id string

The documents id.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getSource($index, $type, $id)
{
    if ($this->db->dslVersion >= 7) {
        return $this->db->get([$index, '_doc', $id]);
    } else {
        return $this->db->get([$index, $type, $id]);
    }
}

            
getTemplate() public method
public mixed getTemplate ( $name )
$name
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function getTemplate($name)
{
    return $this->db->get(['_template', $name]);
}

            
indexExists() public method
public mixed indexExists ( $index )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function indexExists($index)
{
    return $this->db->head([$index]);
}

            
insert() public method
public mixed insert ( $index, $type, $data, $id null, $options = [] )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

$data string|array

Json string or array of data to store

$id string|null

The documents id. If not specified Id will be automatically chosen

$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function insert($index, $type, $data, $id = null, $options = [])
{
    if (empty($data)) {
        $body = '{}';
    } else {
        $body = is_array($data) ? Json::encode($data) : $data;
    }
    if ($id !== null) {
        if ($this->db->dslVersion >= 7) {
            return $this->db->put([$index, '_doc', $id], $options, $body);
        } else {
            return $this->db->put([$index, $type, $id], $options, $body);
        }
    } else {
        if ($this->db->dslVersion >= 7) {
            return $this->db->post([$index, '_doc'], $options, $body);
        } else {
            return $this->db->post([$index, $type], $options, $body);
        }
    }
}

            
mget() public method

Gets multiple documents from the index

TODO allow specifying type and index + fields

See also https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html.

public mixed mget ( $index, $type, $ids, $options = [] )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

$ids string[]

The documents ids as values in array.

$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function mget($index, $type, $ids, $options = [])
{
    $body = Json::encode(['ids' => array_values($ids)]);
    if ($this->db->dslVersion >= 7) {
        return $this->db->get([$index, '_mget'], $options, $body);
    } else {
        return $this->db->get([$index, $type, '_mget'], $options, $body);
    }
}

            
openIndex() public method
public mixed openIndex ( $index )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function openIndex($index)
{
    return $this->db->post([$index, '_open']);
}

            
refreshIndex() public method
public mixed refreshIndex ( $index )
$index string

Index that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function refreshIndex($index)
{
    return $this->db->post([$index, '_refresh']);
}

            
removeAlias() public method
public boolean removeAlias ( $index, $alias )
$index string

Index that the document belongs to.

$alias string
throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function removeAlias($index, $alias)
{
    return (bool)$this->db->delete([$index, '_alias', $alias]);
}

            
scroll() public method (available since version 2.0.4)
public mixed scroll ( $options = [] )
$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function scroll($options = [])
{
    $body = array_filter([
        'scroll' => ArrayHelper::remove($options, 'scroll', null),
        'scroll_id' => ArrayHelper::remove($options, 'scroll_id', null),
    ]);
    if (empty($body)) {
        $body = (object) [];
    }
   return $this->db->post(['_search', 'scroll'], $options, Json::encode($body));
}

            
search() public method

Sends a request to the _search API and returns the result

public mixed search ( $options = [] )
$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function search($options = [])
{
    $query = $this->queryParts;
    if (empty($query)) {
        $query = '{}';
    }
    if (is_array($query)) {
        $query = Json::encode($query);
    }
    $url = [$this->index !== null ? $this->index : '_all'];
    if ($this->db->dslVersion < 7 && $this->type !== null) {
        $url[] = $this->type;
    }
    $url[] = '_search';
    return $this->db->get($url, array_merge($this->options, $options), $query);
}

            
setMapping() public method
public mixed setMapping ( $index, $type, $mapping, $options = [] )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

$mapping string|array
$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function setMapping($index, $type, $mapping, $options = [])
{
    $body = $mapping !== null ? (is_string($mapping) ? $mapping : Json::encode($mapping)) : null;
    if ($this->db->dslVersion >= 7) {
        $endpoint = [$index, '_mapping'];
    } else {
        $endpoint = [$index, '_mapping', $type];
    }
    return $this->db->put($endpoint, $options, $body);
}

            
suggest() public method

Sends a suggest request to the _search API and returns the result

See also https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html.

public mixed suggest ( $suggester, $options = [] )
$suggester string|array

The suggester body

$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function suggest($suggester, $options = [])
{
    if (empty($suggester)) {
        $suggester = '{}';
    }
    if (is_array($suggester)) {
        $suggester = Json::encode($suggester);
    }
    $body = '{"suggest":'.$suggester.',"size":0}';
    $url = [
        $this->index !== null ? $this->index : '_all',
        '_search'
    ];
    $result = $this->db->post($url, array_merge($this->options, $options), $body);
    return $result['suggest'];
}

            
typeExists() public method
public mixed typeExists ( $index, $type )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function typeExists($index, $type)
{
    if ($this->db->dslVersion >= 7) {
        return $this->db->head([$index, '_doc']);
    } else {
        return $this->db->head([$index, $type]);
    }
}

            
update() public method
public mixed update ( $index, $type, $id, $data, $options = [] )
$index string

Index that the document belongs to.

$type string|null

Type that the document belongs to.

$id string

The documents id.

$data mixed
$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function update($index, $type, $id, $data, $options = [])
{
    $body = [
        'doc' => empty($data) ? new \stdClass() : $data,
    ];
    if (isset($options["detect_noop"])) {
        $body["detect_noop"] = $options["detect_noop"];
        unset($options["detect_noop"]);
    }
    if ($this->db->dslVersion >= 7) {
        return $this->db->post([$index, '_update', $id], $options, Json::encode($body));
    } else {
        return $this->db->post([$index, $type, $id, '_update'], $options, Json::encode($body));
    }
}

            
updateAnalyzers() public method (available since version 2.0.4)

Define new analyzers for the index.

For example if content analyzer hasn’t been defined on "myindex" yet you can use the following commands to add it:

 $setting = [
     'analysis' => [
         'analyzer' => [
             'ngram_analyzer_with_filter' => [
                 'tokenizer' => 'ngram_tokenizer',
                 'filter' => 'lowercase, snowball'
             ],
         ],
         'tokenizer' => [
             'ngram_tokenizer' => [
                 'type' => 'nGram',
                 'min_gram' => 3,
                 'max_gram' => 10,
                 'token_chars' => ['letter', 'digit', 'whitespace', 'punctuation', 'symbol']
             ],
         ],
     ]
];
$elasticQuery->createCommand()->updateAnalyzers('myindex', $setting);

See also https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html#update-settings-analysis.

public mixed updateAnalyzers ( $index, $setting, $options = [] )
$index string

Index that the document belongs to.

$setting string|array
$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function updateAnalyzers($index, $setting, $options = [])
{
    $this->closeIndex($index);
    $result = $this->updateSettings($index, $setting, $options);
    $this->openIndex($index);
    return $result;
}

            
updateSettings() public method (available since version 2.0.4)

Change specific index level settings in real time.

Note that update analyzers required to close() the index first and open() it after the changes are made, use updateAnalyzers() for it.

See also http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html.

public mixed updateSettings ( $index, $setting, $options = [] )
$index string

Index that the document belongs to.

$setting string|array
$options array

URL options

throws yii\elasticsearch\Exception
throws \yii\base\InvalidConfigException

                public function updateSettings($index, $setting, $options = [])
{
    $body = $setting !== null ? (is_string($setting) ? $setting : Json::encode($setting)) : null;
    return $this->db->put([$index, '_settings'], $options, $body);
}