Passed
Push — master ( ecb363...8a2802 )
by Adrian
04:14
created

Search::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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