-
Notifications
You must be signed in to change notification settings - Fork 730
Expand file tree
/
Copy pathIndex.php
More file actions
875 lines (777 loc) · 29.8 KB
/
Index.php
File metadata and controls
875 lines (777 loc) · 29.8 KB
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
<?php
declare(strict_types=1);
namespace Elastica;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\MissingParameterException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Elastic\Transport\Exception\NoNodeAvailableException;
use Elastica\Bulk\ResponseSet;
use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
use Elastica\Exception\ClientException;
use Elastica\Exception\InvalidException;
use Elastica\Exception\NotFoundException;
use Elastica\Index\Recovery as IndexRecovery;
use Elastica\Index\Settings as IndexSettings;
use Elastica\Index\Stats as IndexStats;
use Elastica\Query\AbstractQuery;
use Elastica\ResultSet\BuilderInterface;
use Elastica\Script\AbstractScript;
/**
* Elastica index object.
*
* Handles reads, deletes and configurations of an index
*
* @author Nicolas Ruflin <spam@ruflin.com>
*
* @phpstan-import-type TCreateQueryArgsMatching from Query
*/
class Index implements SearchableInterface
{
/**
* Index name.
*
* @var string Index name
*/
protected string $_name;
/**
* Client object.
*
* @var Client Client object
*/
protected Client $_client;
/**
* Creates a new index object.
*
* All the communication to and from an index goes of this object
*
* @param Client $client Client object
* @param string $name Index name
*/
public function __construct(Client $client, string $name)
{
$this->_client = $client;
$this->_name = $name;
}
/**
* Return Index Stats.
*
* @return IndexStats
*/
public function getStats()
{
return new IndexStats($this);
}
/**
* Return Index Recovery.
*
* @return IndexRecovery
*/
public function getRecovery()
{
return new IndexRecovery($this);
}
/**
* Sets the mappings for the current index.
*
* @param Mapping $mapping MappingType object
* @param array $query querystring when put mapping (for example update_all_types)
*/
public function setMapping(Mapping $mapping, array $query = []): Response
{
return $mapping->send($this, $query);
}
/**
* Gets all mappings for the current index.
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function getMapping(): array
{
$response = $this->getClient()->indices()->getMapping(['index' => $this->getName()]);
$data = $response->asArray();
// Get first entry as if index is an Alias, the name of the mapping is the real name and not alias name
$mapping = \array_shift($data);
return $mapping['mappings'] ?? [];
}
/**
* Returns the index settings object.
*
* @return IndexSettings
*/
public function getSettings()
{
return new IndexSettings($this);
}
/**
* @param array|string $data
*
* @return Document
*/
public function createDocument(string $id = '', $data = [])
{
return new Document($id, $data, $this);
}
/**
* Uses _bulk to send documents to the server.
*
* @param Document[] $docs Array of Elastica\Document
* @param array $options Array of query params to use for query. For possible options check es api
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws BulkResponseException
* @throws ClientException
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
*/
public function updateDocuments(array $docs, array $options = []): ResponseSet
{
foreach ($docs as $doc) {
$doc->setIndex($this->getName());
}
return $this->getClient()->updateDocuments($docs, $options);
}
/**
* Update entries in the db based on a query.
*
* @param AbstractQuery|array|Query|string|null $query Query object or array
* @param AbstractScript $script Script
* @param array $options Optional params
*
* @phpstan-param TCreateQueryArgsMatching $query
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function updateByQuery($query, AbstractScript $script, array $options = []): Response
{
$q = Query::create($query)->getQuery();
$params = [
'index' => $this->getName(),
'body' => [
'query' => \is_array($q) ? $q : $q->toArray(),
'script' => $script->toArray()['script'],
],
];
return $this->_client->toElasticaResponse(
$this->_client->updateByQuery(\array_merge($params, $options))
);
}
/**
* Adds the given document to the search index.
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function addDocument(Document $doc): Response
{
$params = ['index' => $this->getName()];
if (null !== $doc->getId() && '' !== $doc->getId()) {
$params['id'] = $doc->getId();
}
$options = $doc->getOptions(
[
'consistency',
'op_type',
'parent',
'percolate',
'pipeline',
'refresh',
'replication',
'retry_on_conflict',
'routing',
'timeout',
]
);
$params['body'] = $doc->getData();
$params = \array_merge($params, $options);
$response = $this->_client->toElasticaResponse($this->_client->index($params));
$data = $response->getData();
// set autogenerated id to document
if ($response->isOk() && (
$doc->isAutoPopulate() || $this->getClient()->getConfigValue(['document', 'autoPopulate'], false)
)) {
if (isset($data['_id']) && !$doc->hasId()) {
$doc->setId($data['_id']);
}
$doc->setVersionParams($data);
}
return $response;
}
/**
* Uses _bulk to send documents to the server.
*
* @param array|Document[] $docs Array of Elastica\Document
* @param array $options Array of query params to use for query. For possible options check es api
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws BulkResponseException
* @throws ClientException
*
* @return ResponseSet
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
*/
public function addDocuments(array $docs, array $options = [])
{
foreach ($docs as $doc) {
$doc->setIndex($this->getName());
}
return $this->getClient()->addDocuments($docs, $options);
}
/**
* Get the document from search index.
*
* @param int|string $id Document id
* @param array $options options for the get request
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
* @throws NotFoundException
*/
public function getDocument($id, array $options = []): Document
{
$params = \array_merge([
'id' => $id,
'index' => $this->getName(),
], $options);
try {
$response = $this->getClient()->get($params);
$result = $response->asArray();
if (isset($result['fields'])) {
$data = $result['fields'];
} elseif (isset($result['_source'])) {
$data = $result['_source'];
} else {
$data = [];
}
$doc = new Document((string) $id, $data, $this->getName());
$doc->setVersionParams($result);
return $doc;
} catch (ClientResponseException $e) {
// 404 means the index alias doesn't exist which means no indexes have it.
if (404 === $e->getResponse()->getStatusCode()) {
throw new NotFoundException('doc id '.$id.' not found');
}
// If we don't have a 404 then this is still unexpected so rethrow the exception.
throw $e;
}
}
/**
* Deletes a document by its unique identifier.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function deleteById(string $id, array $options = []): Response
{
if (!\trim($id)) {
throw new NotFoundException('Doc id "'.$id.'" not found and can not be deleted');
}
$params = [
'id' => \trim($id),
'index' => $this->getName(),
];
return $this->_client->toElasticaResponse(
$this->_client->delete(\array_merge($params, $options))
);
}
/**
* Deletes documents matching the given query.
*
* @param AbstractQuery|array|Query|string|null $query Query object or array
* @param array $options Optional params
*
* @phpstan-param TCreateQueryArgsMatching $query
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function deleteByQuery($query, array $options = []): Response
{
$query = Query::create($query)->getQuery();
$params = \array_merge([
'index' => $this->getName(),
'body' => ['query' => \is_array($query) ? $query : $query->toArray()],
], $options);
return $this->_client->toElasticaResponse(
$this->_client->deleteByQuery($params)
);
}
/**
* Opens a Point-in-Time on the index.
*
* @see: https://www.elastic.co/guide/en/elasticsearch/reference/current/point-in-time-api.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function openPointInTime(string $keepAlive): Response
{
return $this->_client->toElasticaResponse(
$this->_client->openPointInTime(['index' => $this->getName(), 'keep_alive' => $keepAlive])
);
}
/**
* Deletes the index.
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function delete(): Response
{
return $this->_client->toElasticaResponse(
$this->_client->indices()->delete(['index' => $this->getName()])
);
}
/**
* Uses the "_bulk" endpoint to delete documents from the server.
*
* @param Document[] $docs Array of documents
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws BulkResponseException
* @throws ClientException
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
*/
public function deleteDocuments(array $docs): ResponseSet
{
foreach ($docs as $doc) {
$doc->setIndex($this->getName());
}
return $this->getClient()->deleteDocuments($docs);
}
/**
* Force merges index.
*
* Detailed arguments can be found here in the ES documentation.
*
* @param array $args Additional arguments
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function forcemerge($args = []): Response
{
return $this->_client->toElasticaResponse(
$this->_client->indices()->forcemerge(\array_merge(['index' => $this->getName(), $args]))
);
}
/**
* Refreshes the index.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function refresh(): Response
{
return $this->_client->toElasticaResponse(
$this->_client->indices()->refresh()
);
}
/**
* Creates a new index with the given arguments.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
*
* @param array $args Additional arguments to pass to the Create endpoint
* @param array $options Associative array of options (option=>value)
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function create(array $args = [], array $options = []): Response
{
if ($options['recreate'] ?? false) {
try {
$this->delete();
} catch (ClientResponseException $e) {
// Index can't be deleted, because it doesn't exist
}
}
unset($options['recreate']);
$params = \array_merge([
'index' => $this->getName(),
'body' => $args,
], $options);
return $this->_client->toElasticaResponse(
$this->_client->indices()->create($params)
);
}
/**
* Checks if the given index exists ans is created.
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function exists(): bool
{
$response = $this->getClient()->indices()->exists(['index' => $this->getName()]);
return 200 === $response->getStatusCode();
}
public function createSearch($query = '', ?array $options = null, ?BuilderInterface $builder = null): Search
{
$search = new Search($this->getClient(), $builder);
$search->addIndex($this);
$search->setOptionsAndQuery($options, $query);
return $search;
}
public function search($query = '', ?array $options = null): ResultSet
{
$search = $this->createSearch($query, $options);
return $search->search('', null);
}
public function count($query = ''): int
{
$search = $this->createSearch($query);
return $search->count('', false);
}
/**
* Iterates over all documents, matching given query, using "search after" based pagination.
*
* @see \Elastica\Query::setSearchAfter()
*
* @param mixed $query search query with sort by unique document field.
* @param int $batchSize the number of rows to be returned in each batch (e.g. each query size).
* @param array|null $options search request options.
* @return \Generator|\Elastica\Document[] list of all documents matched the given query as iterator.
*/
public function each($query = '', int $batchSize = 100, ?array $options = null): \Generator
{
if ($batchSize < 1) {
throw new InvalidException('Batch size must be greater than 0.');
}
$query = clone Query::create($query);
if (!$query->hasParam('sort')) {
throw new InvalidException('Query must have "sort" parameter in order to use "search after" based iteration.');
}
$query->setSize($batchSize);
while (true) {
$resultSet = $this->search($query, $options);
foreach ($resultSet->getDocuments() as $document) {
yield $document;
}
if (count($resultSet->getDocuments()) < $batchSize) {
break;
}
$query->setSearchAfter($document->getSort());
}
}
/**
* Iterates over all documents in batches, matching given query, using "search after" based pagination.
*
* @see \Elastica\Query::setSearchAfter()
*
* @param mixed $query search query with sort by unique document field.
* @param int $batchSize the number of rows to be returned in each batch (e.g. each query size).
* @param array|null $options search request options.
* @return \Generator|\Elastica\Document[][] list of document batches matched the given query as iterator.
*/
public function batch($query = '', int $batchSize = 100, ?array $options = null): \Generator
{
if ($batchSize < 1) {
throw new InvalidException('Batch size must be greater than 0.');
}
$query = clone Query::create($query);
if (!$query->hasParam('sort')) {
throw new InvalidException('Query must have "sort" parameter in order to use "search after" based iteration.');
}
$query->setSize($batchSize);
while (true) {
$resultSet = $this->search($query, $options);
$documents = $resultSet->getDocuments();
if (empty($documents)) {
break;
}
yield $documents;
if (count($documents) < $batchSize) {
break;
}
$lastDocument = array_pop($documents);
$query->setSearchAfter($lastDocument->getSort());
}
}
/**
* Opens an index.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function open(): Response
{
return $this->_client->toElasticaResponse(
$this->_client->indices()->open(['index' => $this->getName()])
);
}
/**
* Closes the index.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function close(): Response
{
return $this->_client->toElasticaResponse(
$this->_client->indices()->close(['index' => $this->getName()])
);
}
/**
* Returns the index name.
*/
public function getName(): string
{
return $this->_name;
}
/**
* Returns index client.
*/
public function getClient(): Client
{
return $this->_client;
}
/**
* Adds an alias to the current index.
*
* @param bool $replace If set, an existing alias will be replaced
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function addAlias(string $name, bool $replace = false): Response
{
$data = ['actions' => []];
if ($replace) {
$status = new Status($this->getClient());
foreach ($status->getIndicesWithAlias($name) as $index) {
$data['actions'][] = ['remove' => ['index' => $index->getName(), 'alias' => $name]];
}
}
$data['actions'][] = ['add' => ['index' => $this->getName(), 'alias' => $name]];
// // TODO: Use only UpdateAliases when dropping support for elasticsearch/elasticsearch 7.x
// $endpoint = \class_exists(UpdateAliases::class) ? new UpdateAliases() : new Update();
// $endpoint->setBody($data);
return $this->_client->toElasticaResponse(
$this->_client->indices()->updateAliases(['index' => $this->getName(), 'body' => $data])
);
}
/**
* Removes an alias pointing to the current index.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function removeAlias(string $name): Response
{
return $this->_client->toElasticaResponse(
$this->_client->indices()->deleteAlias(['index' => $this->getName(), 'name' => $name])
);
}
/**
* Returns all index aliases.
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*
* @return string[]
*/
public function getAliases(): array
{
$response = $this->getClient()->indices()->getAlias(['name' => '*']);
$responseData = $response->asArray();
if (!isset($responseData[$this->getName()])) {
return [];
}
$data = $responseData[$this->getName()];
if (!empty($data['aliases'])) {
return \array_keys($data['aliases']);
}
return [];
}
/**
* Checks if the index has the given alias.
*/
public function hasAlias(string $name): bool
{
return \in_array($name, $this->getAliases(), true);
}
/**
* Clears the cache of an index.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-clearcache.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function clearCache(): Response
{
// TODO: add additional cache clean arguments
return $this->_client->toElasticaResponse(
$this->_client->indices()->clearCache()
);
}
/**
* Flushes the index to storage.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-flush.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function flush(array $options = []): Response
{
return $this->_client->toElasticaResponse(
$this->_client->indices()->flush($options)
);
}
/**
* Can be used to change settings during runtime. One example is to use it for bulk updating.
*
* @param array $data Data array
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function setSettings(array $data): Response
{
return $this->_client->toElasticaResponse(
$this->_client->indices()->putSettings(['index' => $this->getName(), 'body' => $data])
);
}
/**
* Run the analysis on the index.
*
* @param array $body request body for the `_analyze` API, see API documentation for the required properties
* @param array $args Additional arguments
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html
*
* @throws MissingParameterException if a required parameter is missing
* @throws NoNodeAvailableException if all the hosts are offline
* @throws ClientResponseException if the status code of response is 4xx
* @throws ServerResponseException if the status code of response is 5xx
* @throws ClientException
*/
public function analyze(array $body, $args = []): array
{
$params = \array_merge([
'index' => $this->getName(),
'body' => $body,
], $args);
$response = $this->getClient()->indices()->analyze($params);
$data = $response->asArray();
// Support for "Explain" parameter, that returns a different response structure from Elastic
// @see: https://www.elastic.co/guide/en/elasticsearch/reference/current/_explain_analyze.html
if (isset($body['explain']) && $body['explain']) {
return $data['detail'];
}
return $data['tokens'];
}
/**
* Update document, using update script.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
*
* @param AbstractScript|Document $data Document or Script with update data
* @param array $options array of query params to use for query
*/
public function updateDocument($data, array $options = []): Response
{
if (!($data instanceof Document) && !($data instanceof AbstractScript)) {
throw new \InvalidArgumentException('Data should be a Document or Script');
}
if (!$data->hasId()) {
throw new InvalidException('Document or Script id is not set');
}
return $this->_client->updateDocument($data->getId(), $data, $this->getName(), $options);
}
}