Command.php 13.5 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;

use yii\base\Component;
11 12
use yii\base\InvalidCallException;
use yii\base\InvalidParamException;
13 14
use yii\helpers\Json;

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

45

46
    /**
47
     * Sends a request to the _search API and returns the result
48
     * @param array $options
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
     * @return mixed
     */
    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',
            $this->type !== null ? $this->type : '_all',
            '_search'
        ];

        return $this->db->get($url, array_merge($this->options, $options), $query);
    }

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    /**
     * Sends a request to the delete by query
     * @param array $options
     * @return mixed
     */
    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',
            $this->type !== null ? $this->type : '_all',
            '_query'
        ];

        return $this->db->delete($url, array_merge($this->options, $options), $query);
    }

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    /**
     * Sends a request to the _suggest API and returns the result
     * @param string|array $suggester the suggester body
     * @param array $options
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters.html
     */
    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);
    }

118 119
    /**
     * Inserts a document into an index
120 121 122 123 124
     * @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 chosen
     * @param array $options
125 126 127 128 129
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html
     */
    public function insert($index, $type, $data, $id = null, $options = [])
    {
130 131 132 133 134
        if (empty($data)) {
            $body = '{}';
        } else {
            $body = is_array($data) ? Json::encode($data) : $data;
        }
135 136 137 138 139 140 141 142 143 144 145 146 147

        if ($id !== null) {
            return $this->db->put([$index, $type, $id], $options, $body);
        } else {
            return $this->db->post([$index, $type], $options, $body);
        }
    }

    /**
     * gets a document from the index
     * @param $index
     * @param $type
     * @param $id
148
     * @param array $options
149 150 151 152 153
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html
     */
    public function get($index, $type, $id, $options = [])
    {
154
        return $this->db->get([$index, $type, $id], $options);
155 156 157 158 159 160 161 162 163
    }

    /**
     * gets multiple documents from the index
     *
     * TODO allow specifying type and index + fields
     * @param $index
     * @param $type
     * @param $ids
164
     * @param array $options
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-multi-get.html
     */
    public function mget($index, $type, $ids, $options = [])
    {
        $body = Json::encode(['ids' => array_values($ids)]);

        return $this->db->get([$index, $type, '_mget'], $options, $body);
    }

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

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

    /**
     * deletes a document from the index
     * @param $index
     * @param $type
     * @param $id
206
     * @param array $options
207 208 209 210 211 212 213 214 215 216 217 218 219
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html
     */
    public function delete($index, $type, $id, $options = [])
    {
        return $this->db->delete([$index, $type, $id], $options);
    }

    /**
     * updates a document
     * @param $index
     * @param $type
     * @param $id
220
     * @param array $options
221 222 223
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-update.html
     */
224 225 226 227 228
//	public function update($index, $type, $id, $data, $options = [])
//	{
//		// TODO implement
////		return $this->db->delete([$index, $type, $id], $options);
//	}
229

230 231 232 233 234
    // TODO bulk http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html

    /**
     * creates an index
     * @param $index
235
     * @param array $configuration
236 237 238 239 240 241 242
     * @return mixed
     * @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;

243
        return $this->db->put([$index], [], $body);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    }

    /**
     * deletes an index
     * @param $index
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
     */
    public function deleteIndex($index)
    {
        return $this->db->delete([$index]);
    }

    /**
     * deletes all indexes
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
     */
    public function deleteAllIndexes()
    {
        return $this->db->delete(['_all']);
    }

    /**
     * checks whether an index exists
     * @param $index
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-exists.html
     */
    public function indexExists($index)
    {
        return $this->db->head([$index]);
    }

    /**
     * @param $index
     * @param $type
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-types-exists.html
     */
    public function typeExists($index, $type)
    {
        return $this->db->head([$index, $type]);
    }

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

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

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

    /**
     * @param $index
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html
     */
    public function getIndexStatus($index = '_all')
    {
        return $this->db->get([$index, '_status']);
    }

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

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

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

    /**
     * @param $index
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.html
     */
    public function refreshIndex($index)
    {
        return $this->db->post([$index, '_refresh']);
    }

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

    /**
     * @param $index
     * @param $type
     * @param $mapping
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
     */
    public function setMapping($index, $type, $mapping, $options = [])
    {
        $body = $mapping !== null ? (is_string($mapping) ? $mapping : Json::encode($mapping)) : null;

374
        return $this->db->put([$index, '_mapping', $type], $options, $body);
375 376 377
    }

    /**
378 379
     * @param string $index
     * @param string $type
380 381 382 383 384
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html
     */
    public function getMapping($index = '_all', $type = '_all')
    {
385
        return $this->db->get([$index, '_mapping', $type]);
386 387 388 389 390 391 392 393 394 395
    }

    /**
     * @param $index
     * @param $type
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
     */
    public function deleteMapping($index, $type)
    {
396
        return $this->db->delete([$index, '_mapping', $type]);
397 398 399 400
    }

    /**
     * @param $index
401
     * @param string $type
402 403 404
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
     */
405 406 407 408 409
//    public function getFieldMapping($index, $type = '_all')
//    {
//		// TODO implement
//        return $this->db->put([$index, $type, '_mapping']);
//    }
410 411 412 413 414 415 416

    /**
     * @param $options
     * @param $index
     * @return mixed
     * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-analyze.html
     */
417 418 419 420 421
//	public function analyze($options, $index = null)
//	{
//		// TODO implement
////		return $this->db->put([$index]);
//	}
422

423 424 425 426 427
    /**
     * @param $name
     * @param $pattern
     * @param $settings
     * @param $mappings
428
     * @param integer $order
429 430 431 432 433 434 435 436 437 438 439 440
     * @return mixed
     * @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,
            'mappings' => (object) $mappings,
        ]);

441
        return $this->db->put(['_template', $name], [], $body);
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

    }

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

    }

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