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