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
			| 
								 
											12 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 * @link http://www.yiiframework.com/
							 | 
						||
| 
								 | 
							
								 * @copyright Copyright (c) 2008 Yii Software LLC
							 | 
						||
| 
								 | 
							
								 * @license http://www.yiiframework.com/license/
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace yii\elasticsearch;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use yii\base\Component;
							 | 
						||
| 
								 | 
							
								use yii\helpers\Json;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 * The Command class implements the API for accessing the elasticsearch REST API.
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 * Check the [elasticsearch guide](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index.html)
							 | 
						||
| 
								 | 
							
								 * for details on these commands.
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 * @author Carsten Brandt <mail@cebe.cc>
							 | 
						||
| 
								 | 
							
								 * @since 2.0
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 */
							 | 
						||
| 
								 
											12 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;
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @var array list of arrays or json strings that become parts of a query
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									public $queryParts;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									public $options = [];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @param array $options
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function search($options = [])
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$query = $this->queryParts;
							 | 
						||
| 
								 
											12 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'
							 | 
						||
| 
								 | 
							
										];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->get($url, array_merge($this->options, $options), $query);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 
											12 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 = [])
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$body = is_array($data) ? Json::encode($data) : $data;
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										if ($id !== null) {
							 | 
						||
| 
								 | 
							
											return $this->db->put([$index, $type, $id], $options, $body);
							 | 
						||
| 
								 | 
							
										} else {
							 | 
						||
| 
								 | 
							
											return $this->db->post([$index, $type], $options, $body);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 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 = [])
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->get([$index, $type, $id], $options, null);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * gets multiple documents from the index
							 | 
						||
| 
								 | 
							
									 *
							 | 
						||
| 
								 | 
							
									 * TODO allow specifying type and index + fields
							 | 
						||
| 
								 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @param $type
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $ids
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param array $options
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function mget($index, $type, $ids, $options = [])
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										$body = Json::encode(['ids' => array_values($ids)]);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->get([$index, $type, '_mget'], $options, $body);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 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)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->get([$index, $type, $id]);
							 | 
						||
| 
								 
											12 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)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->head([$index, $type, $id]);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 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 = [])
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->delete([$index, $type, $id], $options);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 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
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								//	public function update($index, $type, $id, $data, $options = [])
							 | 
						||
| 
								 | 
							
								//	{
							 | 
						||
| 
								 | 
							
								//		// TODO implement
							 | 
						||
| 
								 | 
							
								////		return $this->db->delete([$index, $type, $id], $options);
							 | 
						||
| 
								 | 
							
								//	}
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									// TODO bulk http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * creates an index
							 | 
						||
| 
								 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @param array $configuration
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 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;
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->put([$index], $body);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * deletes an index
							 | 
						||
| 
								 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function deleteIndex($index)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->delete([$index]);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * deletes all indexes
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function deleteAllIndexes()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->delete(['_all']);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * checks whether an index exists
							 | 
						||
| 
								 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-exists.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function indexExists($index)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->head([$index]);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @param $type
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-types-exists.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function typeExists($index, $type)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->head([$index, $type]);
							 | 
						||
| 
								 
											12 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
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function openIndex($index)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->post([$index, '_open']);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function closeIndex($index)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->post([$index, '_close']);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function getIndexStatus($index = '_all')
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->get([$index, '_status']);
							 | 
						||
| 
								 
											12 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
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-clearcache.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function clearIndexCache($index)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->post([$index, '_cache', 'clear']);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									public function flushIndex($index = '_all')
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->post([$index, '_flush']);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function refreshIndex($index)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->post([$index, '_refresh']);
							 | 
						||
| 
								 
											12 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
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @param $type
							 | 
						||
| 
								 | 
							
									 * @param $mapping
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									public function setMapping($index, $type, $mapping, $options = [])
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$body = $mapping !== null ? (is_string($mapping) ? $mapping : Json::encode($mapping)) : null;
							 | 
						||
| 
								 | 
							
										return $this->db->put([$index, $type, '_mapping'], $options, $body);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param string $index
							 | 
						||
| 
								 | 
							
									 * @param string $type
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function getMapping($index = '_all', $type = '_all')
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->get([$index, $type, '_mapping']);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @param $type
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function deleteMapping($index, $type)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->delete([$index, $type]);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @param string $type
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function getFieldMapping($index, $type = '_all')
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->put([$index, $type, '_mapping']);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $options
							 | 
						||
| 
								 | 
							
									 * @param $index
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-analyze.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								//	public function analyze($options, $index = null)
							 | 
						||
| 
								 | 
							
								//	{
							 | 
						||
| 
								 | 
							
								//		// TODO implement
							 | 
						||
| 
								 | 
							
								////		return $this->db->put([$index]);
							 | 
						||
| 
								 | 
							
								//	}
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $name
							 | 
						||
| 
								 | 
							
									 * @param $pattern
							 | 
						||
| 
								 | 
							
									 * @param $settings
							 | 
						||
| 
								 | 
							
									 * @param $mappings
							 | 
						||
| 
								 | 
							
									 * @param int $order
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 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,
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
											'mappings' => (object) $mappings,
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										]);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->put(['_template', $name], $body);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $name
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function deleteTemplate($name)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->delete(['_template', $name]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @param $name
							 | 
						||
| 
								 | 
							
									 * @return mixed
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function getTemplate($name)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										return $this->db->get(['_template', $name]);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |