Class yii\elasticsearch\Command
| Inheritance | yii\elasticsearch\Command » yii\base\Component |
|---|---|
| Available since extension's version | 2.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
| 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 | The types to execute the query on. | yii\elasticsearch\Command |
Public Methods
Property Details
The indexes to execute the query on. Defaults to null meaning all indexes
Options to be appended to the query URL, such as "search_type" for search or "timeout" for delete
List of arrays or json strings that become parts of a query
Method Details
See also https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-adding.
| public addAlias( mixed $index, mixed $alias, array $aliasParameters = [] ): boolean | ||
| $index | mixed | |
| $alias | mixed | |
| $aliasParameters | array | |
public function addAlias($index, $alias, $aliasParameters = [])
{
return (bool)$this->db->put([$index, '_alias', $alias], [], json_encode((object)$aliasParameters));
}
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 aliasActions( array $actions ): boolean | ||
| $actions | array | |
public function aliasActions(array $actions)
{
return (bool)$this->db->post(['_aliases'], [], json_encode(['actions' => $actions]));
}
| public aliasExists( string $alias ): boolean | ||
| $alias | string | |
public function aliasExists($alias)
{
$indexes = $this->getIndexesByAlias($alias);
return !empty($indexes);
}
| public clearIndexCache( mixed $index ): mixed | ||
| $index | mixed | |
public function clearIndexCache($index)
{
return $this->db->post([$index, '_cache', 'clear']);
}
See also https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html.
| public clearScroll( array $options = [] ): mixed | ||
| $options | array | |
public function clearScroll($options = [])
{
return $this->db->delete(['_search', 'scroll'], $options);
}
| public closeIndex( mixed $index ): mixed | ||
| $index | mixed | |
public function closeIndex($index)
{
return $this->db->post([$index, '_close']);
}
Creates an index
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html.
| public createIndex( mixed $index, array $configuration = null ): mixed | ||
| $index | mixed | |
| $configuration | array | |
public function createIndex($index, $configuration = null)
{
$body = $configuration !== null ? Json::encode($configuration) : null;
return $this->db->put([$index], [], $body);
}
| public createTemplate( mixed $name, mixed $pattern, mixed $settings, mixed $mappings, integer $order = 0 ): mixed | ||
| $name | mixed | |
| $pattern | mixed | |
| $settings | mixed | |
| $mappings | mixed | |
| $order | integer | |
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);
}
Deletes a document from the index
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html.
| public delete( mixed $index, mixed $type, mixed $id, array $options = [] ): mixed | ||
| $index | mixed | |
| $type | mixed | |
| $id | mixed | |
| $options | array | |
public function delete($index, $type, $id, $options = [])
{
return $this->db->delete([$index, $type, $id], $options);
}
Deletes all indexes
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html.
| public deleteAllIndexes( ): mixed |
public function deleteAllIndexes()
{
return $this->db->delete(['_all']);
}
Sends a request to the delete by query
| public deleteByQuery( array $options = [] ): mixed | ||
| $options | array | |
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[] = '_query';
return $this->db->delete($url, array_merge($this->options, $options), $query);
}
Deletes an index
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html.
| public deleteIndex( mixed $index ): mixed | ||
| $index | mixed | |
public function deleteIndex($index)
{
return $this->db->delete([$index]);
}
| public deleteMapping( mixed $index, mixed $type ): mixed | ||
| $index | mixed | |
| $type | mixed | |
public function deleteMapping($index, $type)
{
return $this->db->delete([$index, '_mapping', $type]);
}
| public deleteTemplate( mixed $name ): mixed | ||
| $name | mixed | |
public function deleteTemplate($name)
{
return $this->db->delete(['_template', $name]);
}
Gets a document from the index
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html.
| public exists( mixed $index, mixed $type, mixed $id ): mixed | ||
| $index | mixed | |
| $type | mixed | |
| $id | mixed | |
public function exists($index, $type, $id)
{
return $this->db->head([$index, $type, $id]);
}
| public flushIndex( mixed $index = '_all' ): mixed | ||
| $index | mixed | |
public function flushIndex($index = '_all')
{
return $this->db->post([$index, '_flush']);
}
Gets a document from the index
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html.
| public get( mixed $index, mixed $type, mixed $id, array $options = [] ): mixed | ||
| $index | mixed | |
| $type | mixed | |
| $id | mixed | |
| $options | array | |
public function get($index, $type, $id, $options = [])
{
return $this->db->get([$index, $type, $id], $options);
}
See also https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving.
| public getAliasInfo( ): array |
public function getAliasInfo()
{
$aliasInfo = $this->db->get(['_alias', '*']);
return $aliasInfo ?: [];
}
See also https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving.
| public getIndexAliases( string $index ): array | ||
| $index | string | |
public function getIndexAliases($index)
{
$responseData = $this->db->get([$index, '_alias', '*']);
if (empty($responseData)) {
return [];
}
return $responseData[$index]['aliases'];
}
See also https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving.
| public getIndexInfoByAlias( string $alias ): array | ||
| $alias | string | |
public function getIndexInfoByAlias($alias)
{
$responseData = $this->db->get(['_alias', $alias]);
if (empty($responseData)) {
return [];
}
return $responseData;
}
| public getIndexStatus( mixed $index = '_all' ): mixed | ||
| $index | mixed | |
public function getIndexStatus($index = '_all')
{
return $this->db->get([$index, '_status']);
}
| public getIndexesByAlias( string $alias ): array | ||
| $alias | string | |
public function getIndexesByAlias($alias)
{
return array_keys($this->getIndexInfoByAlias($alias));
}
| public getMapping( string $index = '_all', string $type = null ): mixed | ||
| $index | string | |
| $type | string | |
public function getMapping($index = '_all', $type = null)
{
$url = [$index, '_mapping'];
if ($type !== null) {
$url[] = $type;
}
return $this->db->get($url);
}
Gets a documents _source from the index (>=v0.90.1)
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source.
| public getSource( mixed $index, mixed $type, mixed $id ): mixed | ||
| $index | mixed | |
| $type | mixed | |
| $id | mixed | |
public function getSource($index, $type, $id)
{
return $this->db->get([$index, $type, $id]);
}
| public getTemplate( mixed $name ): mixed | ||
| $name | mixed | |
public function getTemplate($name)
{
return $this->db->get(['_template', $name]);
}
Checks whether an index exists
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html.
| public indexExists( mixed $index ): mixed | ||
| $index | mixed | |
public function indexExists($index)
{
return $this->db->head([$index]);
}
Inserts a document into an index
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html.
| public insert( string $index, string $type, string|array $data, null $id = null, array $options = [] ): mixed | ||
| $index | string | |
| $type | string | |
| $data | string|array |
Json string or array of data to store |
| $id | null |
The documents id. If not specified Id will be automatically chosen |
| $options | array | |
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) {
return $this->db->put([$index, $type, $id], $options, $body);
} else {
return $this->db->post([$index, $type], $options, $body);
}
}
Gets multiple documents from the index
TODO allow specifying type and index + fields
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html.
| public mget( mixed $index, mixed $type, mixed $ids, array $options = [] ): mixed | ||
| $index | mixed | |
| $type | mixed | |
| $ids | mixed | |
| $options | array | |
public function mget($index, $type, $ids, $options = [])
{
$body = Json::encode(['ids' => array_values($ids)]);
return $this->db->get([$index, $type, '_mget'], $options, $body);
}
| public openIndex( mixed $index ): mixed | ||
| $index | mixed | |
public function openIndex($index)
{
return $this->db->post([$index, '_open']);
}
| public refreshIndex( mixed $index ): mixed | ||
| $index | mixed | |
public function refreshIndex($index)
{
return $this->db->post([$index, '_refresh']);
}
| public removeAlias( string $index, string $alias ): boolean | ||
| $index | string | |
| $alias | string | |
public function removeAlias($index, $alias)
{
return (bool)$this->db->delete([$index, '_alias', $alias]);
}
See also https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html.
| public scroll( array $options = [] ): mixed | ||
| $options | array | |
public function scroll($options = [])
{
return $this->db->get(['_search', 'scroll'], $options);
}
Sends a request to the _search API and returns the result
| public search( array $options = [] ): mixed | ||
| $options | array | |
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->type !== null) {
$url[] = $this->type;
}
$url[] = '_search';
return $this->db->get($url, array_merge($this->options, $options), $query);
}
| public setMapping( string $index, string $type, string|array $mapping, array $options = [] ): mixed | ||
| $index | string | |
| $type | string | |
| $mapping | string|array | |
| $options | array | |
public function setMapping($index, $type, $mapping, $options = [])
{
$body = $mapping !== null ? (is_string($mapping) ? $mapping : Json::encode($mapping)) : null;
return $this->db->put([$index, '_mapping', $type], $options, $body);
}
Sends a request to the _suggest API and returns the result
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html.
| public suggest( string|array $suggester, array $options = [] ): mixed | ||
| $suggester | string|array |
The suggester body |
| $options | array | |
public function suggest($suggester, $options = [])
{
if (empty($suggester)) {
$suggester = '{}';
}
if (is_array($suggester)) {
$suggester = Json::encode($suggester);
}
$url = [
$this->index !== null ? $this->index : '_all',
'_suggest'
];
return $this->db->post($url, array_merge($this->options, $options), $suggester);
}
| public typeExists( mixed $index, mixed $type ): mixed | ||
| $index | mixed | |
| $type | mixed | |
public function typeExists($index, $type)
{
return $this->db->head([$index, $type]);
}
Updates a document
See also http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html.
| public update( mixed $index, mixed $type, mixed $id, mixed $data, array $options = [] ): mixed | ||
| $index | mixed | |
| $type | mixed | |
| $id | mixed | |
| $data | mixed | |
| $options | array | |
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"]);
}
return $this->db->post([$index, $type, $id, '_update'], $options, Json::encode($body));
}
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);
| public updateAnalyzers( string $index, string|array $setting, array $options = [] ): mixed | ||
| $index | string | |
| $setting | string|array | |
| $options | array |
URL options |
public function updateAnalyzers($index, $setting, $options = [])
{
$this->closeIndex($index);
$result = $this->updateSettings($index, $setting, $options);
$this->openIndex($index);
return $result;
}
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 updateSettings( string $index, string|array $setting, array $options = [] ): mixed | ||
| $index | string | |
| $setting | string|array | |
| $options | array |
URL options |
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);
}