Passed
Push — master ( 7b3d17...8aa822 )
by
unknown
02:10
created

Index::deleteDocument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
4
namespace Manticoresearch;
5
6
use Manticoresearch\Exceptions\RuntimeException;
7
use Manticoresearch\Query\Percolate;
8
use Manticoresearch\Results;
9
10
/**
11
 * Manticore index object
12
 * @category ManticoreSearch
13
 * @package ManticoreSearch
14
 * @author Adrian Nuta <[email protected]>
15
 * @link https://manticoresearch.com
16
 */
17
class Index
18
{
19
    protected $client;
20
    protected $index;
21
    protected $cluster;
22
23
    public function __construct(Client $client, $index = null)
24
    {
25
        $this->client = $client;
26
27
        $this->index = $index;
28
29
        $this->cluster = null;
30
    }
31
32
    public function search($input): Search
33
    {
34
        $search = new Search($this->client);
35
        $search->setIndex($this->index);
36
        return $search->search($input);
37
    }
38
39
    public function getDocumentById($id)
40
    {
41
        self::checkDocumentId($id);
42
        $params = [
43
            'body' => [
44
                'index' => $this->index,
45
                'query' => [
46
                    'equals' => ['id' => $id]
47
                ]
48
            ]
49
        ];
50
        $result = new ResultSet($this->client->search($params, true));
51
        return $result->valid() ? $result->current() : null;
52
    }
53
54
    public function getDocumentByIds($ids)
55
    {
56
        if (!is_array($ids)) {
57
            $ids = [$ids];
58
        }
59
        array_walk($ids, 'self::checkDocumentId');
60
        $params = [
61
            'body' => [
62
                'index' => $this->index,
63
                'query' => [
64
                    'in' => ['id' => $ids]
65
                ]
66
            ]
67
        ];
68
        return new ResultSet($this->client->search($params, true));
69
    }
70
71
    public function addDocument($data, $id = 0)
72
    {
73
        self::checkDocumentId($id);
74
        if (is_object($data)) {
75
            $data = (array) $data;
76
        } elseif (is_string($data)) {
77
            $data = json_decode($data, true);
78
        }
79
        $params = [
80
            'body' => [
81
                'index' => $this->index,
82
                'id' => $id,
83
                'doc' => $data
84
            ]
85
        ];
86
87
        if ($this->cluster !== null) {
88
            $params['body']['cluster'] = $this->cluster;
89
        }
90
        return $this->client->insert($params);
91
    }
92
93
    public function addDocuments($documents)
94
    {
95
        $toinsert = [];
96
        foreach ($documents as $document) {
97
            if (is_object($document)) {
98
                $document = (array) $document;
99
            } elseif (is_string($document)) {
100
                $document = json_decode($document, true);
101
            }
102
            if (isset($document['id'])) {
103
                $id = $document['id'];
104
                self::checkDocumentId($id);
105
                unset($document['id']);
106
            } else {
107
                $id = 0;
108
            }
109
            $insert = [
110
                'index' => $this->index,
111
                'id' => $id,
112
                'doc' => $document
113
            ];
114
            if ($this->cluster !== null) {
115
                $insert['cluster'] = $this->cluster;
116
            }
117
            $toinsert[] = ['insert' => $insert];
118
        }
119
        return $this->client->bulk(['body' => $toinsert]);
120
    }
121
122
    public function deleteDocument($id)
123
    {
124
        self::checkDocumentId($id);
125
        $params = [
126
            'body' => [
127
                'index' => $this->index,
128
                'id' => $id
129
            ]
130
        ];
131
        if ($this->cluster !== null) {
132
            $params['body']['cluster'] = $this->cluster;
133
        }
134
        return $this->client->delete($params);
135
    }
136
137
    public function deleteDocuments($query)
138
    {
139
        if ($query instanceof Query) {
140
            $query = $query->toArray();
141
        }
142
        $params = [
143
            'body' => [
144
                'index' => $this->index,
145
                'query' => $query
146
            ]
147
        ];
148
        if ($this->cluster !== null) {
149
            $params['body']['cluster'] = $this->cluster;
150
        }
151
        return $this->client->delete($params);
152
    }
153
154
    public function updateDocument($data, $id)
155
    {
156
        self::checkDocumentId($id);
157
        $params = [
158
            'body' => [
159
                'index' => $this->index,
160
                'id' => $id,
161
                'doc' => $data
162
            ]
163
        ];
164
        if ($this->cluster !== null) {
165
            $params['body']['cluster'] = $this->cluster;
166
        }
167
        return $this->client->update($params);
168
    }
169
170
    public function updateDocuments($data, $query)
171
    {
172
        if ($query instanceof Query) {
173
            $query = $query->toArray();
174
        }
175
        $params = [
176
            'body' => [
177
                'index' => $this->index,
178
                'query' => $query,
179
                'doc' => $data
180
            ]
181
        ];
182
        if ($this->cluster !== null) {
183
            $params['body']['cluster'] = $this->cluster;
184
        }
185
        return $this->client->update($params);
186
    }
187
188
    public function replaceDocument($data, $id)
189
    {
190
        self::checkDocumentId($id);
191
        if (is_object($data)) {
192
            $data = (array) $data;
193
        } elseif (is_string($data)) {
194
            $data = json_decode($data, true);
195
        }
196
        $params = [
197
            'body' => [
198
                'index' => $this->index,
199
                'id' => $id,
200
                'doc' => $data
201
            ]
202
        ];
203
        if ($this->cluster !== null) {
204
            $params['body']['cluster'] = $this->cluster;
205
        }
206
        return $this->client->replace($params);
207
    }
208
209
    public function replaceDocuments($documents)
210
    {
211
        $toreplace = [];
212
        foreach ($documents as $document) {
213
            if (is_object($document)) {
214
                $document = (array) $document;
215
            } elseif (is_string($document)) {
216
                $document = json_decode($document, true);
217
            }
218
            self::checkDocumentId($id);
219
            unset($document['id']);
220
            $replace = [
221
                'index' => $this->index,
222
                'id' => $id,
223
                'doc' => $document
224
            ];
225
            if ($this->cluster !== null) {
226
                $replace['cluster'] = $this->cluster;
227
            }
228
            $toreplace[] = ['replace' => $replace];
229
        }
230
        return $this->client->bulk(['body' => $toreplace]);
231
    }
232
233
    public function create($fields, $settings = [], $silent = false)
234
    {
235
        $params = [
236
            'index' => $this->index,
237
            'body' => [
238
                'columns' => $fields,
239
                'settings' => $settings
240
            ]
241
        ];
242
        if ($silent === true) {
243
            $params['body']['silent'] = true;
244
        }
245
        return $this->client->indices()->create($params);
246
    }
247
248
    public function drop($silent = false)
249
    {
250
        $params = [
251
            'index' => $this->index,
252
        ];
253
        if ($silent === true) {
254
            $params['body'] = ['silent' => true];
255
        }
256
        return $this->client->indices()->drop($params);
257
    }
258
259
    public function describe()
260
    {
261
        $params = [
262
            'index' => $this->index,
263
        ];
264
        return $this->client->indices()->describe($params);
265
    }
266
267
    public function status()
268
    {
269
        $params = [
270
            'index' => $this->index,
271
        ];
272
        return $this->client->indices()->status($params);
273
    }
274
275
    public function truncate()
276
    {
277
        $params = [
278
            'index' => $this->index,
279
        ];
280
        return $this->client->indices()->truncate($params);
281
    }
282
283
    public function optimize($sync = false)
284
    {
285
        $params = [
286
            'index' => $this->index,
287
        ];
288
        if ($sync === true) {
289
            $params['body'] = ['sync' => true];
290
        }
291
        return $this->client->indices()->optimize($params);
292
    }
293
294
    public function flush()
295
    {
296
        $params = [
297
            'index' => $this->index,
298
        ];
299
        $this->client->indices()->flushrtindex($params);
300
    }
301
302
    public function flushramchunk()
303
    {
304
        $params = [
305
            'index' => $this->index,
306
        ];
307
        $this->client->indices()->flushramchunk($params);
308
    }
309
310
    public function alter($operation, $name, $type = null)
311
    {
312
        if ($operation === 'add') {
313
            $params = [
314
                'index' => $this->index,
315
                'body' => [
316
                    'operation' => 'add',
317
                    'column' => ['name' => $name, 'type' => $type]
318
                ]
319
            ];
320
        } elseif ($operation === 'drop') {
321
            $params = [
322
                'index' => $this->index,
323
                'body' => [
324
                    'operation' => 'drop',
325
                    'column' => ['name' => $name]
326
                ]
327
            ];
328
        } else {
329
            throw new RuntimeException('Alter operation not recognized');
330
        }
331
        return $this->client->indices()->alter($params);
332
    }
333
334
    public function keywords($query, $options)
335
    {
336
        $params = [
337
            'index' => $this->index,
338
            'body' => [
339
                'query' => $query,
340
                'options' => $options
341
            ]
342
        ];
343
        return $this->client->keywords($params);
344
    }
345
346
    public function suggest($query, $options)
347
    {
348
        $params = [
349
            'index' => $this->index,
350
            'body' => [
351
                'query' => $query,
352
                'options' => $options
353
            ]
354
        ];
355
        return $this->client->suggest($params);
356
    }
357
358
    public function explainQuery($query)
359
    {
360
        $params = [
361
            'index' => $this->index,
362
            'body' => [
363
                'query' => $query,
364
            ]
365
        ];
366
        return $this->client->explainQuery($params);
367
    }
368
369
370
    public function percolate($docs)
371
    {
372
        $params = ['index' => $this->index, 'body' => []];
373
        if ($docs instanceof Percolate) {
374
            $params['body']['query'] = $docs->toArray();
375
        } else {
376
            if (isset($docs[0]) && is_array($docs[0])) {
377
                $params['body']['query'] = ['percolate' => ['documents' => $docs]];
378
            } else {
379
                $params['body']['query'] = ['percolate' => ['document' => $docs]];
380
            }
381
        }
382
        return new Results\PercolateResultSet($this->client->pq()->search($params, true));
383
    }
384
385
    public function percolateToDocs($docs)
386
    {
387
        $params = ['index' => $this->index, 'body' => []];
388
        if ($docs instanceof Percolate) {
389
            $params['body']['query'] = $docs->toArray();
390
        } else {
391
            if (isset($docs[0]) && is_array($docs[0])) {
392
                $params['body']['query'] = ['percolate' => ['documents' => $docs]];
393
            } else {
394
                $params['body']['query'] = ['percolate' => ['document' => $docs]];
395
            }
396
        }
397
        return new Results\PercolateDocsResultSet($this->client->pq()->search($params, true), $docs);
398
    }
399
400
401
    public function getClient(): Client
402
    {
403
        return $this->client;
404
    }
405
406
    public function getName(): string
407
    {
408
        return $this->index;
409
    }
410
411
    public function setName($index): self
412
    {
413
        $this->index = $index;
414
        return $this;
415
    }
416
417
    public function setCluster($cluster): self
418
    {
419
        $this->cluster = $cluster;
420
        return $this;
421
    }
422
423
    protected static function checkDocumentId(&$id)
424
    {
425
        if (is_string($id)) {
426
            if (!is_numeric($id)) {
427
                throw new RuntimeException('Incorrect document id passed');
428
            }
429
            $id = (int)$id;    
430
        }
431
    }
432
}
433