Command.php 9.69 KB
Newer Older
1 2
<?php
/**
3 4 5
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
6 7 8 9 10
 */

namespace yii\elasticsearch;


11
use Guzzle\Http\Exception\ClientErrorResponseException;
12
use yii\base\Component;
13
use yii\db\Exception;
14 15
use yii\helpers\Json;

16 17 18 19 20
// camelCase vs. _
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/common-options.html#_result_casing


/**
21
 * The Command class implements the API for accessing the elasticsearch REST API.
22
 *
23 24
 * Check the [elasticsearch guide](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index.html)
 * for details on these commands.
25
 *
26 27
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
28
 */
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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;
	/**
45
	 * @var array list of arrays or json strings that become parts of a query
46
	 */
47 48 49
	public $queryParts;

	public $options = [];
50

51 52 53 54 55
	/**
	 * @param array $options
	 * @return mixed
	 */
	public function search($options = [])
56
	{
57
		$query = $this->queryParts;
58 59 60 61 62 63 64 65 66 67 68
		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'
		];
69
		return $this->db->get($url, array_merge($this->options, $options), $query);
70
	}
71 72 73 74 75 76 77 78 79 80 81 82

	/**
	 * 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 = [])
83
	{
84
		$body = is_array($data) ? Json::encode($data) : $data;
85

86 87 88 89
		if ($id !== null) {
			return $this->db->put([$index, $type, $id], $options, $body);
		} else {
			return $this->db->post([$index, $type], $options, $body);
90 91 92
		}
	}

93 94 95 96 97 98 99 100 101 102
	/**
	 * 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 = [])
103
	{
104
		return $this->db->get([$index, $type, $id], $options, null, [200, 404]);
105 106
	}

107 108 109 110 111 112 113 114 115
	/**
	 * gets multiple documents from the index
	 *
	 * TODO allow specifying type and index + fields
	 * @param $index
	 * @param $type
	 * @param $id
	 * @param array $options
	 * @return mixed
116
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html
117 118 119 120
	 */
	public function mget($index, $type, $ids, $options = [])
	{
		$body = Json::encode(['ids' => array_values($ids)]);
121
		return $this->db->get([$index, $type, '_mget'], $options, $body);
122 123
	}

124 125 126 127 128 129 130 131 132 133
	/**
	 * 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)
	{
134
		return $this->db->get([$index, $type, $id]);
135 136 137 138 139 140 141 142 143 144 145 146
	}

	/**
	 * 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)
	{
147
		return $this->db->head([$index, $type, $id]);
148 149
	}

150 151 152 153 154 155 156 157 158 159
	/**
	 * 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 = [])
160
	{
161
		return $this->db->delete([$index, $type, $id], $options);
162 163
	}

164 165 166 167 168 169 170 171 172 173
	/**
	 * 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
	 */
	public function update($index, $type, $id, $data, $options = [])
174
	{
175
		// TODO
176
//		return $this->db->delete([$index, $type, $id], $options);
177 178 179 180 181 182 183 184 185 186
	}

	// TODO bulk http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html

	/**
	 * @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;
187
		return $this->db->put([$index], $body);
188 189 190 191 192 193 194
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
	 */
	public function deleteIndex($index)
	{
195
		return $this->db->delete([$index]);
196 197 198 199 200 201 202
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
	 */
	public function deleteAllIndexes()
	{
203
		return $this->db->delete(['_all']);
204 205 206 207 208 209 210
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-exists.html
	 */
	public function indexExists($index)
	{
211
		return $this->db->head([$index]);
212 213 214 215 216 217 218
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-types-exists.html
	 */
	public function typeExists($index, $type)
	{
219
		return $this->db->head([$index, $type]);
220 221 222 223 224 225 226 227 228 229 230 231 232 233
	}

	// 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

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
	 */
	public function openIndex($index)
	{
234
		return $this->db->post([$index, '_open']);
235 236 237 238 239 240 241
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
	 */
	public function closeIndex($index)
	{
242
		return $this->db->post([$index, '_close']);
243 244 245 246 247 248 249
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html
	 */
	public function getIndexStatus($index = '_all')
	{
250
		return $this->db->get([$index, '_status']);
251 252 253 254 255 256 257 258 259 260
	}

	// 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

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-clearcache.html
	 */
	public function clearIndexCache($index)
	{
261
		return $this->db->post([$index, '_cache', 'clear']);
262 263 264 265 266
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html
	 */
267
	public function flushIndex($index = '_all')
268
	{
269
		return $this->db->post([$index, '_flush']);
270 271 272 273 274 275 276
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.html
	 */
	public function refreshIndex($index)
	{
277
		return $this->db->post([$index, '_refresh']);
278 279 280 281 282 283 284 285 286 287 288 289
	}

	// 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

	/**
	 * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
	 */
	public function setMapping($index, $type, $mapping)
	{
		$body = $mapping !== null ? Json::encode($mapping) : null;
290
		return $this->db->put([$index, $type, '_mapping'], $body);
291 292 293 294 295 296 297
	}

	/**
	 * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html
	 */
	public function getMapping($index = '_all', $type = '_all')
	{
298
		return $this->db->get([$index, $type, '_mapping']);
299 300 301 302 303 304 305
	}

	/**
	 * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
	 */
	public function deleteMapping($index, $type)
	{
306
		return $this->db->delete([$index, $type]);
307 308 309 310 311 312 313
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
	 */
	public function getFieldMapping($index, $type = '_all')
	{
314
		return $this->db->put([$index, $type, '_mapping']);
315 316 317 318 319 320 321
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-analyze.html
	 */
	public function analyze($options, $index = null)
	{
322 323
		// TODO implement
//		return $this->db->put([$index]);
324 325 326 327 328 329 330 331 332 333 334
	}

	/**
	 * @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,
335
			'mappings' => (object) $mappings,
336
		]);
337 338
		return $this->db->put(['_template', $name], $body);

339 340 341 342 343 344 345
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
	 */
	public function deleteTemplate($name)
	{
346 347
		return $this->db->delete(['_template', $name]);

348 349 350 351 352 353 354
	}

	/**
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
	 */
	public function getTemplate($name)
	{
355
		return $this->db->get(['_template', $name]);
356 357
	}
}