1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Search\Aggregation\Aggregation; |
4
|
|
|
use Nord\Lumen\Elasticsearch\Search\Aggregation\AggregationCollection; |
5
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\QueryDSL; |
6
|
|
|
|
7
|
|
|
class Search |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $index; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var QueryDSL |
22
|
|
|
*/ |
23
|
|
|
private $query; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $source; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $scriptFields; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $storedFields; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Sort |
42
|
|
|
*/ |
43
|
|
|
private $sort; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var AggregationCollection |
47
|
|
|
*/ |
48
|
|
|
private $aggregations; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
private $from = 0; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
private $size = 100; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
|
|
private $page = 1; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Constructor. |
67
|
|
|
*/ |
68
|
|
|
public function __construct() |
69
|
|
|
{ |
70
|
|
|
$this->aggregations = new AggregationCollection(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $index |
75
|
|
|
* |
76
|
|
|
* @return Search |
77
|
|
|
*/ |
78
|
|
|
public function setIndex(string $index) |
79
|
|
|
{ |
80
|
|
|
$this->index = $index; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getIndex() |
89
|
|
|
{ |
90
|
|
|
return $this->index; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $type |
95
|
|
|
* |
96
|
|
|
* @return Search |
97
|
|
|
*/ |
98
|
|
|
public function setType(string $type) |
99
|
|
|
{ |
100
|
|
|
$this->type = $type; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getType() |
109
|
|
|
{ |
110
|
|
|
return $this->type; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param QueryDSL $query |
115
|
|
|
* |
116
|
|
|
* @return Search |
117
|
|
|
*/ |
118
|
|
|
public function setQuery(QueryDSL $query) |
119
|
|
|
{ |
120
|
|
|
$this->query = $query; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return QueryDSL |
127
|
|
|
*/ |
128
|
|
|
public function getQuery() |
129
|
|
|
{ |
130
|
|
|
return $this->query; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Sort $sort |
135
|
|
|
* |
136
|
|
|
* @return Search |
137
|
|
|
*/ |
138
|
|
|
public function setSort(Sort $sort) |
139
|
|
|
{ |
140
|
|
|
$this->sort = $sort; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return Sort |
147
|
|
|
*/ |
148
|
|
|
public function getSort() |
149
|
|
|
{ |
150
|
|
|
return $this->sort; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param array $source |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function setSource(array $source) |
159
|
|
|
{ |
160
|
|
|
$this->source = $source; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function getSource() |
169
|
|
|
{ |
170
|
|
|
return $this->source; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return array|null |
175
|
|
|
*/ |
176
|
|
|
public function getScriptFields(): ?array |
177
|
|
|
{ |
178
|
|
|
return $this->scriptFields; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param array $scriptFields |
183
|
|
|
* |
184
|
|
|
* @return Search |
185
|
|
|
*/ |
186
|
|
|
public function setScriptFields(array $scriptFields): Search |
187
|
|
|
{ |
188
|
|
|
$this->scriptFields = $scriptFields; |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return array|null |
195
|
|
|
*/ |
196
|
|
|
public function getStoredFields(): ?array |
197
|
|
|
{ |
198
|
|
|
return $this->storedFields; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param array $storedFields |
203
|
|
|
* |
204
|
|
|
* @return Search |
205
|
|
|
*/ |
206
|
|
|
public function setStoredFields(array $storedFields): Search |
207
|
|
|
{ |
208
|
|
|
$this->storedFields = $storedFields; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return AggregationCollection |
215
|
|
|
*/ |
216
|
|
|
public function getAggregations() |
217
|
|
|
{ |
218
|
|
|
return $this->aggregations; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param Aggregation $aggregation |
223
|
|
|
* |
224
|
|
|
* @return Search |
225
|
|
|
*/ |
226
|
|
|
public function addAggregation(Aggregation $aggregation) |
227
|
|
|
{ |
228
|
|
|
$this->aggregations->add($aggregation); |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $aggregations |
235
|
|
|
* |
236
|
|
|
* @return Search |
237
|
|
|
*/ |
238
|
|
|
public function addAggregations(array $aggregations) |
239
|
|
|
{ |
240
|
|
|
foreach ($aggregations as $aggregation) { |
241
|
|
|
if ($aggregation instanceof Aggregation) { |
242
|
|
|
$this->addAggregation($aggregation); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param int $page |
251
|
|
|
* |
252
|
|
|
* @return Search |
253
|
|
|
*/ |
254
|
|
|
public function setPage(int $page) |
255
|
|
|
{ |
256
|
|
|
$this->page = $page; |
257
|
|
|
|
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return int |
263
|
|
|
*/ |
264
|
|
|
public function getFrom() |
265
|
|
|
{ |
266
|
|
|
return $this->from; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param int $from |
271
|
|
|
* |
272
|
|
|
* @return Search |
273
|
|
|
*/ |
274
|
|
|
public function setFrom(int $from) |
275
|
|
|
{ |
276
|
|
|
$this->from = $from; |
277
|
|
|
|
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return int |
283
|
|
|
*/ |
284
|
|
|
public function getSize() |
285
|
|
|
{ |
286
|
|
|
return $this->size; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param int $size |
291
|
|
|
* |
292
|
|
|
* @return Search |
293
|
|
|
*/ |
294
|
|
|
public function setSize(int $size) |
295
|
|
|
{ |
296
|
|
|
$this->size = $size; |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @return int |
303
|
|
|
*/ |
304
|
|
|
public function getPage() |
305
|
|
|
{ |
306
|
|
|
return $this->page; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return array |
311
|
|
|
*/ |
312
|
|
|
public function buildBody() |
313
|
|
|
{ |
314
|
|
|
$body = []; |
315
|
|
|
$query = $this->getQuery(); |
316
|
|
|
|
317
|
|
|
if ($query !== null) { |
318
|
|
|
$body['query'] = $query->toArray(); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if (empty($body['query'])) { |
322
|
|
|
$body['query'] = ['match_all' => new \stdClass()]; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$sort = $this->getSort(); |
326
|
|
|
|
327
|
|
|
if ($sort !== null) { |
328
|
|
|
$body['sort'] = $sort->toArray(); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
if (!empty($this->getSource())) { |
332
|
|
|
$body['_source'] = $this->getSource(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
if (!empty($this->getScriptFields())) { |
336
|
|
|
$body['script_fields'] = $this->getScriptFields(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
if (!empty($this->getStoredFields())) { |
340
|
|
|
$body['stored_fields'] = $this->getStoredFields(); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
$aggregations = $this->getAggregations(); |
344
|
|
|
if ($aggregations->count() > 0) { |
345
|
|
|
$body['aggs'] = $aggregations->toArray(); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
// Set how many results to return. |
349
|
|
|
if ($this->getSize() > 0) { |
350
|
|
|
$body['size'] = $this->getSize(); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
// Use "from" to determine "from, if it's not set we determine it from the "page" |
354
|
|
|
if ($this->getFrom() > 0) { |
355
|
|
|
$from = $this->getFrom(); |
356
|
|
|
} else { |
357
|
|
|
$from = ($this->getPage() - 1) * $this->getSize(); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
if ($from > 0) { |
361
|
|
|
$body['from'] = $from; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
return $body; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|