Passed
Push — 1.x ( 1fc25c...4e8027 )
by Adrian
05:24 queued 13s
created

Index::deleteDocument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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