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

Search::setIndex()   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\Query\BoolQuery;
7
use Manticoresearch\Query\Distance;
8
use Manticoresearch\Query\Equals;
9
use Manticoresearch\Query\Match;
10
use Manticoresearch\Query\QueryString;
11
use Manticoresearch\Query\Range;
12
use Manticoresearch\Query\ScriptFields;
13
14
/**
15
 * Manticore search object
16
 * @category ManticoreSearch
17
 * @package ManticoreSearch
18
 * @author Adrian Nuta <[email protected]>
19
 * @link https://manticoresearch.com
20
 */
21
class Search
22
{
23
    /**
24
     * @var Client
25
     */
26
    protected $_client;
27
28
    protected $_query;
29
    protected $_body;
30
    /**
31
     * @var array
32
     */
33
    protected $_params = [];
34
35
    public function __construct(Client $client)
36
    {
37
        $this->_client = $client;
38
        $this->_query = new BoolQuery();
39
    }
40
41
    public function setIndex($index): self
42
    {
43
        $this->_params['index'] = $index;
44
        return $this;
45
    }
46
47
    public function setSource($source): self
48
    {
49
        $this->_params['_source'] = $source;
50
        return $this;
51
    }
52
53
    /**
54
     * @param $string
55
     * @return $this
56
     */
57
    public function search($string): self
58
    {
59
        if (is_object($string)) {
60
            $this->_query = $string;
61
            return $this;
62
        }
63
        $this->_query->must(new QueryString($string));
64
        return $this;
65
    }
66
67
    public function match($keywords, $fields = null): self
68
    {
69
        $f = "*";
70
        if ($fields !== null && is_string($fields)) {
71
            $f = $fields;
72
        }
73
        $this->_query->must(new Match($keywords, $f));
74
        return $this;
75
    }
76
77
    public function phrase($string, $fields = null): self
78
    {
79
        $f = "*";
80
        if ($fields !== null && is_string($fields)) {
81
            $f = $fields;
82
        }
83
        $this->_query->must(new Match($string, $f));
84
        return $this;
85
    }
86
87
    public function limit($limit): self
88
    {
89
        $this->_params['limit'] = $limit;
90
        return $this;
91
    }
92
93
    /**
94
     * @param $name
95
     * @param $exp
96
     * @return $this
97
     */
98
    public function expression($name, $exp): self
99
    {
100
        if (!isset($this->_params['script_fields'])) {
101
            $this->_params['script_fields'] = new ScriptFields();
102
        }
103
        $this->_params['script_fields']->add($name, $exp);
104
        return $this;
105
    }
106
107
    public function highlight($fields = [],$settings=[]): self
108
    {
109
110
        if (count($fields) === 0 && count($settings)===0) {
111
            $this->_params['highlight'] =  new \stdClass();
112
            return $this;
113
        }
114
        $this->_params['highlight'] = [];
115
        if (count($fields) > 0) {
116
            $this->_params['highlight']['fields'] =$fields;
117
        }
118
        if(count($settings)>0) {
119
            foreach($settings as $name=>$value)
120
            {
121
                $this->_params['highlight'][$name] =$value;
122
            }
123
        }
124
        return $this;
125
    }
126
127
    public function distance($args): self
128
    {
129
        $this->_query->must(new Distance($args));
130
        return $this;
131
    }
132
133
    public function filter($attr, $op = '', $values = []): self
134
    {
135
        if (is_object($attr)) {
136
            $this->_query->must($attr);
137
            return $this;
138
        }
139
        if (!is_array($values)) {
140
            $values = [$values];
141
        }
142
143
        switch ($op) {
144
            case 'range':
145
                $this->_query->must(new Range($attr, [
146
                    'gte' => $values[0],
147
                    'lte' => $values[1]
148
                ]));
149
                break;
150
            case 'lt':
151
            case 'lte':
152
            case 'gt':
153
            case 'gte':
154
                $this->_query->must(new Range($attr, [
155
                    $op => $values[0],
156
                ]));
157
                break;
158
            case 'equals':
159
                $this->_query->must(new Equals($attr, $values[0]));
160
                break;
161
        }
162
        return $this;
163
    }
164
165
    public function orFilter($attr, $op = '', $values = []): self
166
    {
167
        if (is_object($attr)) {
168
            $this->_query->should($attr);
169
            return $this;
170
        }
171
        if (!is_array($values)) {
172
            $values = [$values];
173
        }
174
        switch ($op) {
175
            case 'range':
176
                $this->_query->should(new Range($attr, [
177
                    'gte' => $values[0],
178
                    'lte' => $values[1]
179
                ]));
180
                break;
181
            case 'lt':
182
            case 'lte':
183
            case 'gte':
184
            case 'gt':
185
                $this->_query->should(new Range($attr, [
186
                    $op => $values[0],
187
                ]));
188
                break;
189
            case 'equals':
190
                $this->_query->should(new Equals($attr, $values[0]));
191
                break;
192
        }
193
        return $this;
194
    }
195
196
    public function notFilter($attr, $op = '', $values = []): self
197
    {
198
        if (is_object($attr)) {
199
            $this->_query->mustNot($attr);
200
            return $this;
201
        }
202
        if (!is_array($values)) {
203
            $values = [$values];
204
        }
205
206
        switch ($op) {
207
            case 'range':
208
                $this->_query->mustNot(new Range($attr, [
209
                    'gte' => $values[0],
210
                    'lte' => $values[1]
211
                ]));
212
                break;
213
            case 'lt':
214
            case 'lte':
215
            case 'gte':
216
            case 'gt':
217
                $this->_query->mustNot(new Range($attr, [
218
                    $op => $values[0],
219
                ]));
220
                break;
221
            case 'equals':
222
                $this->_query->mustNot(new Equals($attr, $values[0]));
223
                break;
224
        }
225
        return $this;
226
    }
227
228
    public function offset($offset): self
229
    {
230
        $this->_params['offset'] = $offset;
231
        return $this;
232
    }
233
234
    public function maxMatches($maxmatches): self
235
    {
236
        $this->_params['max_matches'] = $maxmatches;
237
        return $this;
238
    }
239
240
    public function sort($field, $direction = 'asc', $mode = null): self
241
    {
242
        // reset sorting
243
        if ($field === false) {
244
            $this->_params['sort'] = [];
245
        }
246
        //if 1st arg is array means we have a sorting expression
247
        if (is_array($field)) {
248
            //is 2nd arg is true we full set the sort with the expr, otherwise just add it
249
            if (isset($direction) && $direction === true) {
250
                $this->_params['sort'] = $field;
251
            } else {
252
                $this->_params['sort'] [] = $field;
253
            }
254
            return $this;
255
        }
256
        if (!isset($this->_params['sort'])) {
257
            $this->_params['sort'] = [];
258
        }
259
        if ($mode === null) {
260
            $this->_params['sort'] [] = [$field => $direction];
261
        } else {
262
            $this->_params['sort'] [] = [$field => ['order' => $direction, 'mode' => $mode]];
263
        }
264
265
        return $this;
266
    }
267
268
    public function profile(): self
269
    {
270
        $this->_params['profile'] = true;
271
        return $this;
272
    }
273
274
    public function get()
275
    {
276
        $this->_body = $this->compile();
277
        $resp = $this->_client->search(['body' => $this->_body], true);
278
        return new ResultSet($resp);
279
280
    }
281
282
    public function compile()
283
    {
284
        $body = $this->_params;
285
        $query = $this->_query->toArray();
286
        if ($query !== null) {
287
            $body['query'] = $query;
288
        }
289
290
        if (isset($this->_params['script_fields'])) {
291
            $body['script_fields'] = $this->_params['script_fields']->toArray();
292
            unset($this->_params['script_fields']);
293
        }
294
295
        return $body;
296
    }
297
298
    public function getBody()
299
    {
300
        return $this->_body;
301
    }
302
303
    public function reset()
304
    {
305
        $this->_params = [];
306
        $this->_query = new BoolQuery();
307
    }
308
309
    public function getClient()
310
    {
311
        return $this->_client;
312
    }
313
}
314