Passed
Push — 1.x ( 77e7f6...966118 )
by Adrian
02:40 queued 12s
created

Index::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
        $params = [
91
            'body' => [
92
                'index' => $this->_index,
93
                'query' => $query
94
            ]
95
        ];
96
        return $this->_client->delete($params);
97
    }
98
99
    public function updateDocument($data, $id)
100
    {
101
        $params = [
102
            'body' => [
103
                'index' => $this->_index,
104
                'id' => $id,
105
                'doc' => $data
106
            ]
107
        ];
108
        return $this->_client->update($params);
109
    }
110
111
    public function updateDocuments($data, $query)
112
    {
113
        $params = [
114
            'body' => [
115
                'index' => $this->_index,
116
                'query' => $query,
117
                'doc' => $data
118
            ]
119
        ];
120
        return $this->_client->update($params);
121
    }
122
123
    public function replaceDocument($data, $id)
124
    {
125
        $params = [
126
            'body' => [
127
                'index' => $this->_index,
128
                'id' => $id,
129
                'doc' => $data
130
            ]
131
        ];
132
        return $this->_client->replace($params);
133
    }
134
135
    public function replaceDocuments($documents)
136
    {
137
        $toreplace = [];
138
        foreach ($documents as $document) {
139
            $id = $document['id'];
140
            unset($document['id']);
141
            $toreplace[] = [
142
                'replace' => [
143
                    'index' => $this->_index,
144
                    'id' => $id,
145
                    'doc' => $document
146
                ]
147
            ];
148
        }
149
        return $this->_client->bulk(['body' => $toreplace]);
150
    }
151
152
    public function create($fields, $settings = [],$silent=false)
153
    {
154
        $params = [
155
            'index' => $this->_index,
156
            'body' => [
157
                'columns' => $fields,
158
                'settings' => $settings
159
            ]
160
        ];
161
        if($silent===true) {
162
            $params['body']['silent'] = true;
163
        }
164
        return $this->_client->indices()->create($params);
165
    }
166
167
    public function drop($silent = false)
168
    {
169
        $params = [
170
            'index' => $this->_index,
171
        ];
172
        if ($silent === true) {
173
            $params['body'] = ['silent' => true];
174
        }
175
        return $this->_client->indices()->drop($params);
176
    }
177
178
    public function describe()
179
    {
180
        $params = [
181
            'index' => $this->_index,
182
        ];
183
        return $this->_client->indices()->describe($params);
184
    }
185
186
    public function status()
187
    {
188
        $params = [
189
            'index' => $this->_index,
190
        ];
191
        return $this->_client->indices()->status($params);
192
    }
193
194
    public function truncate()
195
    {
196
        $params = [
197
            'index' => $this->_index,
198
        ];
199
        return $this->_client->indices()->truncate($params);
200
    }
201
202
    public function optimize($sync = false)
203
    {
204
        $params = [
205
            'index' => $this->_index,
206
        ];
207
        if ($sync === true) {
208
            $params['body'] = ['sync' => true];
209
        }
210
        return $this->_client->indices()->optimize($params);
211
    }
212
213
    public function flush()
214
    {
215
        $params = [
216
            'index' => $this->_index,
217
        ];
218
        $this->_client->indices()->flushrtindex($params);
219
    }
220
221
    public function flushramchunk()
222
    {
223
        $params = [
224
            'index' => $this->_index,
225
        ];
226
        $this->_client->indices()->flushramchunk($params);
227
    }
228
229
    public function alter($operation,$name, $type)
230
    {
231
        if($operation==='add') {
232
            $params = [
233
                'index' => $this->_index,
234
                'body' => [
235
                    'operation' => 'add',
236
                    'column' => ['name' => $name, 'type' => $type]
237
                ]
238
            ];
239
240
        }elseif($operation==='drop'){
241
            $params = [
242
                'index' => $this->_index,
243
                'body' => [
244
                    'operation' => 'drop',
245
                    'column' => ['name' => $name]
246
                ]
247
            ];
248
        } else {
249
            throw new RuntimeException('Alter operation not recognized');
250
        }
251
        return $this->_client->indices()->alter($params);
252
    }
253
254
    public function keywords($query, $options)
255
    {
256
        $params = [
257
            'index' => $this->_index,
258
            'body' => [
259
                '$query' => $query,
260
                'options' => $options
261
            ]
262
        ];
263
        return $this->_client->keywords($params);
264
    }
265
266
    public function suggest($query, $options)
267
    {
268
        $params = [
269
            'index' => $this->_index,
270
            'body' => [
271
                '$query' => $query,
272
                'options' => $options
273
            ]
274
        ];
275
        return $this->_client->suggest($params);
276
277
    }
278
279
    public function getClient(): Client
280
    {
281
        return $this->_client;
282
    }
283
284
    public function getName(): string
285
    {
286
        return $this->_index;
287
    }
288
289
    public function setName($index): self
290
    {
291
        $this->_index = $index;
292
        return $this;
293
    }
294
}
295