forked from yiisoft/yii2-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.php
667 lines (602 loc) · 19.2 KB
/
Command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\elasticsearch;
use yii\base\Component;
use yii\base\InvalidCallException;
use yii\helpers\Json;
/**
* The Command class implements the API for accessing the elasticsearch REST API.
*
* Check the [elasticsearch guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)
* for details on these commands.
*
* @author Carsten Brandt <[email protected]>
* @since 2.0
*/
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.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-multi-index-type
*/
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;
/**
* @var array options to be appended to the query URL, such as "search_type" for search or "timeout" for delete
*/
public $options = [];
/**
* Sends a request to the _search API and returns the result
* @param array $options
* @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'];
if ($this->type !== null) {
$url[] = $this->type;
}
$url[] = '_search';
return $this->db->get($url, array_merge($this->options, $options), $query);
}
/**
* 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'];
if ($this->type !== null) {
$url[] = $this->type;
}
$url[] = '_query';
return $this->db->delete($url, array_merge($this->options, $options), $query);
}
/**
* 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.elastic.co/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);
}
/**
* 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 chosen
* @param array $options
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
*/
public function insert($index, $type, $data, $id = null, $options = [])
{
if (empty($data)) {
$body = '{}';
} else {
$body = is_array($data) ? Json::encode($data) : $data;
}
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
* @param array $options
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
*/
public function get($index, $type, $id, $options = [])
{
return $this->db->get([$index, $type, $id], $options);
}
/**
* gets multiple documents from the index
*
* TODO allow specifying type and index + fields
* @param $index
* @param $type
* @param $ids
* @param array $options
* @return mixed
* @see http://www.elastic.co/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.elastic.co/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.elastic.co/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
* @param array $options
* @return mixed
* @see http://www.elastic.co/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
* @param array $options
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
*/
public function update($index, $type, $id, $data, $options = [])
{
$body = [
'doc' => empty($data) ? new \stdClass() : $data,
];
if (isset($options["detect_noop"])) {
$body["detect_noop"] = $options["detect_noop"];
unset($options["detect_noop"]);
}
return $this->db->post([$index, $type, $id, '_update'], $options, Json::encode($body));
}
// TODO bulk http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
/**
* creates an index
* @param $index
* @param array $configuration
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
*/
public function createIndex($index, $configuration = null)
{
$body = $configuration !== null ? Json::encode($configuration) : null;
return $this->db->put([$index], [], $body);
}
/**
* deletes an index
* @param $index
* @return mixed
* @see http://www.elastic.co/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.elastic.co/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.elastic.co/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.elastic.co/guide/en/elasticsearch/reference/current/indices-types-exists.html
*/
public function typeExists($index, $type)
{
return $this->db->head([$index, $type]);
}
/**
* @param string $alias
*
* @return bool
*/
public function aliasExists($alias)
{
$indexes = $this->getIndexesByAlias($alias);
return !empty($indexes);
}
/**
* @return array
* @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving
*/
public function getAliasInfo()
{
$aliasInfo = $this->db->get(['_alias', '*']);
return $aliasInfo ?: [];
}
/**
* @param string $alias
*
* @return array
* @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving
*/
public function getIndexInfoByAlias($alias)
{
$responseData = $this->db->get(['_alias', $alias]);
if (empty($responseData)) {
return [];
}
return $responseData;
}
/**
* @param string $alias
*
* @return array
*/
public function getIndexesByAlias($alias)
{
return array_keys($this->getIndexInfoByAlias($alias));
}
/**
* @param string $index
*
* @return array
* @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving
*/
public function getIndexAliases($index)
{
$responseData = $this->db->get([$index, '_alias', '*']);
if (empty($responseData)) {
return [];
}
return $responseData[$index]['aliases'];
}
/**
* @param $index
* @param $alias
* @param array $aliasParameters
*
* @return boolean
* @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-adding
*/
public function addAlias($index, $alias, $aliasParameters = [])
{
return (bool)$this->db->put([$index, '_alias', $alias], [], json_encode((object)$aliasParameters));
}
/**
* @param string $index
* @param string $alias
*
* @return boolean
* @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#deleting
*/
public function removeAlias($index, $alias)
{
return (bool)$this->db->delete([$index, '_alias', $alias]);
}
/**
* Runs alias manipulations.
* If you want to add alias1 to index1
* and remove alias2 from index2 you can use following commands:
* ~~~
* $actions = [
* ['add' => ['index' => 'index1', 'alias' => 'alias1']],
* ['remove' => ['index' => 'index2', 'alias' => 'alias2']],
* ];
* ~~~
* @param array $actions
*
* @return boolean
* @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#indices-aliases
*/
public function aliasActions(array $actions)
{
return (bool)$this->db->post(['_aliases'], [], json_encode(['actions' => $actions]));
}
/**
* Change specific index level settings in real time.
* Note that update analyzers required to [[close()]] the index first and [[open()]] it after the changes are made,
* use [[updateAnalyzers()]] for it.
*
* @param string $index
* @param string|array $setting
* @param array $options URL options
* @return mixed
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
* @since 2.0.4
*/
public function updateSettings($index, $setting, $options = [])
{
$body = $setting !== null ? (is_string($setting) ? $setting : Json::encode($setting)) : null;
return $this->db->put([$index, '_settings'], $options, $body);
}
/**
* Define new analyzers for the index.
* For example if content analyzer hasn’t been defined on "myindex" yet
* you can use the following commands to add it:
*
* ~~~
* $setting = [
* 'analysis' => [
* 'analyzer' => [
* 'ngram_analyzer_with_filter' => [
* 'tokenizer' => 'ngram_tokenizer',
* 'filter' => 'lowercase, snowball'
* ],
* ],
* 'tokenizer' => [
* 'ngram_tokenizer' => [
* 'type' => 'nGram',
* 'min_gram' => 3,
* 'max_gram' => 10,
* 'token_chars' => ['letter', 'digit', 'whitespace', 'punctuation', 'symbol']
* ],
* ],
* ]
* ];
* $elasticQuery->createCommand()->updateAnalyzers('myindex', $setting);
* ~~~
*
* @param string $index
* @param string|array $setting
* @param array $options URL options
* @return mixed
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html#update-settings-analysis
* @since 2.0.4
*/
public function updateAnalyzers($index, $setting, $options = [])
{
$this->closeIndex($index);
$result = $this->updateSettings($index, $setting, $options);
$this->openIndex($index);
return $result;
}
// TODO http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html
// TODO http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-warmers.html
/**
* @param $index
* @return mixed
* @see http://www.elastic.co/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.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html
*/
public function closeIndex($index)
{
return $this->db->post([$index, '_close']);
}
/**
* @param array $options
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
* @return mixed
* @since 2.0.4
*/
public function scroll($options = [])
{
return $this->db->get(['_search', 'scroll'], $options);
}
/**
* @param array $options
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
* @return mixed
* @since 2.0.4
*/
public function clearScroll($options = [])
{
return $this->db->delete(['_search', 'scroll'], $options);
}
/**
* @param $index
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-status.html
*/
public function getIndexStatus($index = '_all')
{
return $this->db->get([$index, '_status']);
}
// TODO http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html
// http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-segments.html
/**
* @param $index
* @return mixed
* @see http://www.elastic.co/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.elastic.co/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.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html
*/
public function refreshIndex($index)
{
return $this->db->post([$index, '_refresh']);
}
// TODO http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-optimize.html
// TODO http://www.elastic.co/guide/en/elasticsearch/reference/0.90/indices-gateway-snapshot.html
/**
* @param string $index
* @param string $type
* @param string|array $mapping
* @param array $options
* @return mixed
* @see http://www.elastic.co/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;
return $this->db->put([$index, '_mapping', $type], $options, $body);
}
/**
* @param string $index
* @param string $type
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html
*/
public function getMapping($index = '_all', $type = null)
{
$url = [$index, '_mapping'];
if ($type !== null) {
$url[] = $type;
}
return $this->db->get($url);
}
/**
* @param $index
* @param $type
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
*/
public function deleteMapping($index, $type)
{
return $this->db->delete([$index, '_mapping', $type]);
}
/**
* @param $index
* @param string $type
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
*/
// public function getFieldMapping($index, $type = '_all')
// {
// // TODO implement
// return $this->db->put([$index, $type, '_mapping']);
// }
/**
* @param $options
* @param $index
* @return mixed
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html
*/
// public function analyze($options, $index = null)
// {
// // TODO implement
//// return $this->db->put([$index]);
// }
/**
* @param $name
* @param $pattern
* @param $settings
* @param $mappings
* @param integer $order
* @return mixed
* @see http://www.elastic.co/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,
]);
return $this->db->put(['_template', $name], [], $body);
}
/**
* @param $name
* @return mixed
* @see http://www.elastic.co/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.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
*/
public function getTemplate($name)
{
return $this->db->get(['_template', $name]);
}
}