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