Command.php 10.6 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 11 12
 */

namespace yii\elasticsearch;

use yii\base\Component;
use yii\helpers\Json;

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

	public $options = [];
43

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

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

79 80 81 82
		if ($id !== null) {
			return $this->db->put([$index, $type, $id], $options, $body);
		} else {
			return $this->db->post([$index, $type], $options, $body);
83 84 85
		}
	}

86 87 88 89 90 91 92 93 94 95
	/**
	 * 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 = [])
96
	{
97
		return $this->db->get([$index, $type, $id], $options, null);
98 99
	}

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

117 118 119 120 121 122 123 124 125 126
	/**
	 * 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)
	{
127
		return $this->db->get([$index, $type, $id]);
128 129 130 131 132 133 134 135 136 137 138 139
	}

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

143 144 145 146 147 148 149 150 151 152
	/**
	 * 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 = [])
153
	{
154
		return $this->db->delete([$index, $type, $id], $options);
155 156
	}

157 158 159 160 161 162 163 164 165
	/**
	 * 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
	 */
166 167 168 169 170
//	public function update($index, $type, $id, $data, $options = [])
//	{
//		// TODO implement
////		return $this->db->delete([$index, $type, $id], $options);
//	}
171 172 173 174

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

	/**
175 176 177 178
	 * creates an index
	 * @param $index
	 * @param array $configuration
	 * @return mixed
179 180 181 182 183
	 * @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;
184
		return $this->db->put([$index], $body);
185 186 187
	}

	/**
188 189 190
	 * deletes an index
	 * @param $index
	 * @return mixed
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
	 * deletes all indexes
	 * @return mixed
201 202 203 204
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
	 */
	public function deleteAllIndexes()
	{
205
		return $this->db->delete(['_all']);
206 207 208
	}

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

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

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

	/**
238 239
	 * @param $index
	 * @return mixed
240 241 242 243
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
	 */
	public function openIndex($index)
	{
244
		return $this->db->post([$index, '_open']);
245 246 247
	}

	/**
248 249
	 * @param $index
	 * @return mixed
250 251 252 253
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-open-close.html
	 */
	public function closeIndex($index)
	{
254
		return $this->db->post([$index, '_close']);
255 256 257
	}

	/**
258 259
	 * @param $index
	 * @return mixed
260 261 262 263
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html
	 */
	public function getIndexStatus($index = '_all')
	{
264
		return $this->db->get([$index, '_status']);
265 266 267 268 269 270
	}

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

	/**
271 272
	 * @param $index
	 * @return mixed
273 274 275 276
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-clearcache.html
	 */
	public function clearIndexCache($index)
	{
277
		return $this->db->post([$index, '_cache', 'clear']);
278 279 280
	}

	/**
281 282
	 * @param $index
	 * @return mixed
283 284
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html
	 */
285
	public function flushIndex($index = '_all')
286
	{
287
		return $this->db->post([$index, '_flush']);
288 289 290
	}

	/**
291 292
	 * @param $index
	 * @return mixed
293 294 295 296
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.html
	 */
	public function refreshIndex($index)
	{
297
		return $this->db->post([$index, '_refresh']);
298 299 300 301 302 303 304
	}

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

	/**
305 306 307 308 309
	 * @param $index
	 * @param $type
	 * @param $mapping
	 * @return mixed
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
310
	 */
311
	public function setMapping($index, $type, $mapping, $options = [])
312
	{
313 314
		$body = $mapping !== null ? (is_string($mapping) ? $mapping : Json::encode($mapping)) : null;
		return $this->db->put([$index, $type, '_mapping'], $options, $body);
315 316 317
	}

	/**
318 319 320 321
	 * @param string $index
	 * @param string $type
	 * @return mixed
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html
322 323 324
	 */
	public function getMapping($index = '_all', $type = '_all')
	{
325
		return $this->db->get([$index, $type, '_mapping']);
326 327 328
	}

	/**
329 330 331 332
	 * @param $index
	 * @param $type
	 * @return mixed
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
333 334 335
	 */
	public function deleteMapping($index, $type)
	{
336
		return $this->db->delete([$index, $type]);
337 338 339
	}

	/**
340 341 342
	 * @param $index
	 * @param string $type
	 * @return mixed
343 344 345 346
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
	 */
	public function getFieldMapping($index, $type = '_all')
	{
347
		return $this->db->put([$index, $type, '_mapping']);
348 349 350
	}

	/**
351 352 353
	 * @param $options
	 * @param $index
	 * @return mixed
354 355
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-analyze.html
	 */
356 357 358 359 360
//	public function analyze($options, $index = null)
//	{
//		// TODO implement
////		return $this->db->put([$index]);
//	}
361 362

	/**
363 364 365 366 367 368
	 * @param $name
	 * @param $pattern
	 * @param $settings
	 * @param $mappings
	 * @param int $order
	 * @return mixed
369 370 371 372 373 374 375 376
	 * @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,
377
			'mappings' => (object) $mappings,
378
		]);
379 380
		return $this->db->put(['_template', $name], $body);

381 382 383
	}

	/**
384 385
	 * @param $name
	 * @return mixed
386 387 388 389
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
	 */
	public function deleteTemplate($name)
	{
390 391
		return $this->db->delete(['_template', $name]);

392 393 394
	}

	/**
395 396
	 * @param $name
	 * @return mixed
397 398 399 400
	 * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
	 */
	public function getTemplate($name)
	{
401
		return $this->db->get(['_template', $name]);
402 403
	}
}