Completed
Push — master ( b25384...a9cbe2 )
by Adrian
01:51
created

Index::explainQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Manticoresearch;
5
6
use Manticoresearch\Exceptions\RuntimeException;
7
8
/**
9
 * Manticore index object
10
 * @category ManticoreSearch
11
 * @package ManticoreSearch
12
 * @author Adrian Nuta <[email protected]>
13
 * @link https://manticoresearch.com
14
 */
15
class Index
16
{
17
    protected $_client;
18
    protected $_index;
19
20
    public function __construct(Client $client, $index = null)
21
    {
22
        $this->_client = $client;
23
24
        $this->_index = $index;
25
    }
26
27
    public function search($input): Search
28
    {
29
        $search = new Search($this->_client);
30
        $search->setIndex($this->_index);
31
        return $search->search($input);
32
    }
33
34
    public function getDocumentById($id)
35
    {
36
        $params = [
37
            'body' => [
38
                'index' => $this->_index,
39
                'query' => [
40
                    'equals' => ['id' => $id]
41
                ]
42
            ]
43
        ];
44
        $result = new ResultSet($this->_client->search($params, true));
45
        return $result->valid() ? $result->current() : null;
46
    }
47
48
    public function addDocument($data, $id = null)
49
    {
50
        $params = [
51
            'body' => [
52
                'index' => $this->_index,
53
                'id' => $id,
54
                'doc' => $data
55
            ]
56
        ];
57
        return $this->_client->insert($params);
58
    }
59
60
    public function addDocuments($documents)
61
    {
62
        $toinsert = [];
63
        foreach ($documents as $document) {
64
            $id = $document['id'];
65
            unset($document['id']);
66
            $toinsert[] = [
67
                'insert' => [
68
                    'index' => $this->_index,
69
                    'id' => $id,
70
                    'doc' => $document
71
                ]
72
            ];
73
        }
74
        return $this->_client->bulk(['body' => $toinsert]);
75
    }
76
77
    public function deleteDocument($id)
78
    {
79
        $params = [
80
            'body' => [
81
                'index' => $this->_index,
82
                'id' => $id
83
            ]
84
        ];
85
        return $this->_client->delete($params);
86
    }
87
88
    public function deleteDocuments($query)
89
    {
90
        if($query instanceof Query) {
91
            $query = $query->toArray();
92
        }
93
        $params = [
94
            'body' => [
95
                'index' => $this->_index,
96
                'query' => $query
97
            ]
98
        ];
99
        return $this->_client->delete($params);
100
    }
101
102
    public function updateDocument($data, $id)
103
    {
104
        $params = [
105
            'body' => [
106
                'index' => $this->_index,
107
                'id' => $id,
108
                'doc' => $data
109
            ]
110
        ];
111
        return $this->_client->update($params);
112
    }
113
114
    public function updateDocuments($data, $query)
115
    {
116
        if($query instanceof Query) {
117
            $query = $query->toArray();
118
        }
119
        $params = [
120
            'body' => [
121
                'index' => $this->_index,
122
                'query' => $query,
123
                'doc' => $data
124
            ]
125
        ];
126
        return $this->_client->update($params);
127
    }
128
129
    public function replaceDocument($data, $id)
130
    {
131
        $params = [
132
            'body' => [
133
                'index' => $this->_index,
134
                'id' => $id,
135
                'doc' => $data
136
            ]
137
        ];
138
        return $this->_client->replace($params);
139
    }
140
141
    public function replaceDocuments($documents)
142
    {
143
        $toreplace = [];
144
        foreach ($documents as $document) {
145
            $id = $document['id'];
146
            unset($document['id']);
147
            $toreplace[] = [
148
                'replace' => [
149
                    'index' => $this->_index,
150
                    'id' => $id,
151
                    'doc' => $document
152
                ]
153
            ];
154
        }
155
        return $this->_client->bulk(['body' => $toreplace]);
156
    }
157
158
    public function create($fields, $settings = [],$silent=false)
159
    {
160
        $params = [
161
            'index' => $this->_index,
162
            'body' => [
163
                'columns' => $fields,
164
                'settings' => $settings
165
            ]
166
        ];
167
        if($silent===true) {
168
            $params['body']['silent'] = true;
169
        }
170
        return $this->_client->indices()->create($params);
171
    }
172
173
    public function drop($silent = false)
174
    {
175
        $params = [
176
            'index' => $this->_index,
177
        ];
178
        if ($silent === true) {
179
            $params['body'] = ['silent' => true];
180
        }
181
        return $this->_client->indices()->drop($params);
182
    }
183
184
    public function describe()
185
    {
186
        $params = [
187
            'index' => $this->_index,
188
        ];
189
        return $this->_client->indices()->describe($params);
190
    }
191
192
    public function status()
193
    {
194
        $params = [
195
            'index' => $this->_index,
196
        ];
197
        return $this->_client->indices()->status($params);
198
    }
199
200
    public function truncate()
201
    {
202
        $params = [
203
            'index' => $this->_index,
204
        ];
205
        return $this->_client->indices()->truncate($params);
206
    }
207
208
    public function optimize($sync = false)
209
    {
210
        $params = [
211
            'index' => $this->_index,
212
        ];
213
        if ($sync === true) {
214
            $params['body'] = ['sync' => true];
215
        }
216
        return $this->_client->indices()->optimize($params);
217
    }
218
219
    public function flush()
220
    {
221
        $params = [
222
            'index' => $this->_index,
223
        ];
224
        $this->_client->indices()->flushrtindex($params);
225
    }
226
227
    public function flushramchunk()
228
    {
229
        $params = [
230
            'index' => $this->_index,
231
        ];
232
        $this->_client->indices()->flushramchunk($params);
233
    }
234
235
    public function alter($operation,$name, $type = null)
236
    {
237
        if($operation==='add') {
238
            $params = [
239
                'index' => $this->_index,
240
                'body' => [
241
                    'operation' => 'add',
242
                    'column' => ['name' => $name, 'type' => $type]
243
                ]
244
            ];
245
246
        }elseif($operation==='drop'){
247
            $params = [
248
                'index' => $this->_index,
249
                'body' => [
250
                    'operation' => 'drop',
251
                    'column' => ['name' => $name]
252
                ]
253
            ];
254
        } else {
255
            throw new RuntimeException('Alter operation not recognized');
256
        }
257
        return $this->_client->indices()->alter($params);
258
    }
259
260
    public function keywords($query, $options)
261
    {
262
        $params = [
263
            'index' => $this->_index,
264
            'body' => [
265
                'query' => $query,
266
                'options' => $options
267
            ]
268
        ];
269
        return $this->_client->keywords($params);
270
    }
271
272
    public function suggest($query, $options)
273
    {
274
        $params = [
275
            'index' => $this->_index,
276
            'body' => [
277
                'query' => $query,
278
                'options' => $options
279
            ]
280
        ];
281
        return $this->_client->suggest($params);
282
283
    }
284
285
    public function explainQuery($query)
286
    {
287
        $params = [
288
            'index' => $this->_index,
289
            'body' => [
290
                'query' => $query,
291
            ]
292
        ];
293
        return $this->_client->explainQuery($params);
294
295
    }
296
297
    public function getClient(): Client
298
    {
299
        return $this->_client;
300
    }
301
302
    public function getName(): string
303
    {
304
        return $this->_index;
305
    }
306
307
    public function setName($index): self
308
    {
309
        $this->_index = $index;
310
        return $this;
311
    }
312
}
313