You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
403 lines
11 KiB
403 lines
11 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
11 years ago
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
11 years ago
|
*/
|
||
|
|
||
|
namespace yii\elasticsearch;
|
||
|
|
||
|
use yii\base\Component;
|
||
|
use yii\helpers\Json;
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* The Command class implements the API for accessing the elasticsearch REST API.
|
||
11 years ago
|
*
|
||
11 years ago
|
* Check the [elasticsearch guide](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index.html)
|
||
|
* for details on these commands.
|
||
11 years ago
|
*
|
||
11 years ago
|
* @author Carsten Brandt <mail@cebe.cc>
|
||
|
* @since 2.0
|
||
11 years ago
|
*/
|
||
11 years ago
|
class Command extends Component
|
||
|
{
|
||
|
/**
|
||
|
* @var Connection
|
||
|
*/
|
||
|
public $db;
|
||
|
/**
|
||
|
* @var string|array the indexes to execute the query on. Defaults to null meaning all indexes
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search.html#search-multi-index
|
||
|
*/
|
||
|
public $index;
|
||
|
/**
|
||
|
* @var string|array the types to execute the query on. Defaults to null meaning all types
|
||
|
*/
|
||
|
public $type;
|
||
|
/**
|
||
11 years ago
|
* @var array list of arrays or json strings that become parts of a query
|
||
11 years ago
|
*/
|
||
11 years ago
|
public $queryParts;
|
||
|
|
||
|
public $options = [];
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* @param array $options
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function search($options = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
$query = $this->queryParts;
|
||
11 years ago
|
if (empty($query)) {
|
||
|
$query = '{}';
|
||
|
}
|
||
|
if (is_array($query)) {
|
||
|
$query = Json::encode($query);
|
||
|
}
|
||
|
$url = [
|
||
|
$this->index !== null ? $this->index : '_all',
|
||
|
$this->type !== null ? $this->type : '_all',
|
||
|
'_search'
|
||
|
];
|
||
11 years ago
|
return $this->db->get($url, array_merge($this->options, $options), $query);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Inserts a document into an index
|
||
|
* @param string $index
|
||
|
* @param string $type
|
||
|
* @param string|array $data json string or array of data to store
|
||
|
* @param null $id the documents id. If not specified Id will be automatically choosen
|
||
|
* @param array $options
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html
|
||
|
*/
|
||
|
public function insert($index, $type, $data, $id = null, $options = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
$body = is_array($data) ? Json::encode($data) : $data;
|
||
11 years ago
|
|
||
11 years ago
|
if ($id !== null) {
|
||
|
return $this->db->put([$index, $type, $id], $options, $body);
|
||
|
} else {
|
||
|
return $this->db->post([$index, $type], $options, $body);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* gets a document from the index
|
||
|
* @param $index
|
||
|
* @param $type
|
||
|
* @param $id
|
||
|
* @param array $options
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html
|
||
|
*/
|
||
|
public function get($index, $type, $id, $options = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
return $this->db->get([$index, $type, $id], $options, null);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* gets multiple documents from the index
|
||
|
*
|
||
|
* TODO allow specifying type and index + fields
|
||
|
* @param $index
|
||
|
* @param $type
|
||
11 years ago
|
* @param $ids
|
||
11 years ago
|
* @param array $options
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html
|
||
11 years ago
|
*/
|
||
|
public function mget($index, $type, $ids, $options = [])
|
||
|
{
|
||
|
$body = Json::encode(['ids' => array_values($ids)]);
|
||
11 years ago
|
return $this->db->get([$index, $type, '_mget'], $options, $body);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* gets a documents _source from the index (>=v0.90.1)
|
||
|
* @param $index
|
||
|
* @param $type
|
||
|
* @param $id
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html#_source
|
||
|
*/
|
||
|
public function getSource($index, $type, $id)
|
||
|
{
|
||
11 years ago
|
return $this->db->get([$index, $type, $id]);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* gets a document from the index
|
||
|
* @param $index
|
||
|
* @param $type
|
||
|
* @param $id
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html
|
||
|
*/
|
||
|
public function exists($index, $type, $id)
|
||
|
{
|
||
11 years ago
|
return $this->db->head([$index, $type, $id]);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* deletes a document from the index
|
||
|
* @param $index
|
||
|
* @param $type
|
||
|
* @param $id
|
||
|
* @param array $options
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html
|
||
|
*/
|
||
|
public function delete($index, $type, $id, $options = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
return $this->db->delete([$index, $type, $id], $options);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* updates a document
|
||
|
* @param $index
|
||
|
* @param $type
|
||
|
* @param $id
|
||
|
* @param array $options
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-update.html
|
||
|
*/
|
||
11 years ago
|
// public function update($index, $type, $id, $data, $options = [])
|
||
|
// {
|
||
|
// // TODO implement
|
||
|
//// return $this->db->delete([$index, $type, $id], $options);
|
||
|
// }
|
||
11 years ago
|
|
||
|
// TODO bulk http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html
|
||
|
|
||
|
/**
|
||
11 years ago
|
* creates an index
|
||
|
* @param $index
|
||
|
* @param array $configuration
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html
|
||
|
*/
|
||
|
public function createIndex($index, $configuration = null)
|
||
|
{
|
||
|
$body = $configuration !== null ? Json::encode($configuration) : null;
|
||
11 years ago
|
return $this->db->put([$index], $body);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* deletes an index
|
||
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
|
||
|
*/
|
||
|
public function deleteIndex($index)
|
||
|
{
|
||
11 years ago
|
return $this->db->delete([$index]);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* deletes all indexes
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
|
||
|
*/
|
||
|
public function deleteAllIndexes()
|
||
|
{
|
||
11 years ago
|
return $this->db->delete(['_all']);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* checks whether an index exists
|
||
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-exists.html
|
||
|
*/
|
||
|
public function indexExists($index)
|
||
|
{
|
||
11 years ago
|
return $this->db->head([$index]);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @param $type
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-types-exists.html
|
||
|
*/
|
||
|
public function typeExists($index, $type)
|
||
|
{
|
||
11 years ago
|
return $this->db->head([$index, $type]);
|
||
11 years ago
|
}
|
||
|
|
||
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html
|
||
|
|
||
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
|
||
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-settings.html
|
||
|
|
||
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
|
||
|
*/
|
||
|
public function openIndex($index)
|
||
|
{
|
||
11 years ago
|
return $this->db->post([$index, '_open']);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
|
||
|
*/
|
||
|
public function closeIndex($index)
|
||
|
{
|
||
11 years ago
|
return $this->db->post([$index, '_close']);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html
|
||
|
*/
|
||
|
public function getIndexStatus($index = '_all')
|
||
|
{
|
||
11 years ago
|
return $this->db->get([$index, '_status']);
|
||
11 years ago
|
}
|
||
|
|
||
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-stats.html
|
||
|
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-segments.html
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-clearcache.html
|
||
|
*/
|
||
|
public function clearIndexCache($index)
|
||
|
{
|
||
11 years ago
|
return $this->db->post([$index, '_cache', 'clear']);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html
|
||
|
*/
|
||
11 years ago
|
public function flushIndex($index = '_all')
|
||
11 years ago
|
{
|
||
11 years ago
|
return $this->db->post([$index, '_flush']);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.html
|
||
|
*/
|
||
|
public function refreshIndex($index)
|
||
|
{
|
||
11 years ago
|
return $this->db->post([$index, '_refresh']);
|
||
11 years ago
|
}
|
||
|
|
||
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html
|
||
|
|
||
|
// TODO http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-gateway-snapshot.html
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @param $type
|
||
|
* @param $mapping
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function setMapping($index, $type, $mapping, $options = [])
|
||
11 years ago
|
{
|
||
11 years ago
|
$body = $mapping !== null ? (is_string($mapping) ? $mapping : Json::encode($mapping)) : null;
|
||
|
return $this->db->put([$index, $type, '_mapping'], $options, $body);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param string $index
|
||
|
* @param string $type
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html
|
||
11 years ago
|
*/
|
||
|
public function getMapping($index = '_all', $type = '_all')
|
||
|
{
|
||
11 years ago
|
return $this->db->get([$index, $type, '_mapping']);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @param $type
|
||
|
* @return mixed
|
||
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
|
||
11 years ago
|
*/
|
||
|
public function deleteMapping($index, $type)
|
||
|
{
|
||
11 years ago
|
return $this->db->delete([$index, $type]);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $index
|
||
|
* @param string $type
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
|
||
|
*/
|
||
|
public function getFieldMapping($index, $type = '_all')
|
||
|
{
|
||
11 years ago
|
return $this->db->put([$index, $type, '_mapping']);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $options
|
||
|
* @param $index
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-analyze.html
|
||
|
*/
|
||
11 years ago
|
// public function analyze($options, $index = null)
|
||
|
// {
|
||
|
// // TODO implement
|
||
|
//// return $this->db->put([$index]);
|
||
|
// }
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* @param $name
|
||
|
* @param $pattern
|
||
|
* @param $settings
|
||
|
* @param $mappings
|
||
|
* @param int $order
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
|
||
|
*/
|
||
|
public function createTemplate($name, $pattern, $settings, $mappings, $order = 0)
|
||
|
{
|
||
|
$body = Json::encode([
|
||
|
'template' => $pattern,
|
||
|
'order' => $order,
|
||
|
'settings' => (object) $settings,
|
||
11 years ago
|
'mappings' => (object) $mappings,
|
||
11 years ago
|
]);
|
||
11 years ago
|
return $this->db->put(['_template', $name], $body);
|
||
|
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $name
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
|
||
|
*/
|
||
|
public function deleteTemplate($name)
|
||
|
{
|
||
11 years ago
|
return $this->db->delete(['_template', $name]);
|
||
|
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @param $name
|
||
|
* @return mixed
|
||
11 years ago
|
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
|
||
|
*/
|
||
|
public function getTemplate($name)
|
||
|
{
|
||
11 years ago
|
return $this->db->get(['_template', $name]);
|
||
11 years ago
|
}
|
||
|
}
|