1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Leopard\Business\Dsl; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
5
|
|
|
use ONGR\ElasticsearchDSL\Sort\FieldSort; |
6
|
|
|
use Triadev\Leopard\Busines\Dsl\Query\Specialized; |
7
|
|
|
use Triadev\Leopard\Business\Dsl\Query\Compound; |
8
|
|
|
use Triadev\Leopard\Business\Dsl\Query\Fulltext; |
9
|
|
|
use Triadev\Leopard\Business\Dsl\Query\Geo; |
10
|
|
|
use Triadev\Leopard\Business\Dsl\Query\InnerHit; |
11
|
|
|
use Triadev\Leopard\Business\Dsl\Query\Joining; |
12
|
|
|
use Triadev\Leopard\Business\Filler\EloquentFiller; |
13
|
|
|
use Triadev\Leopard\Business\Helper\IsModelSearchable; |
14
|
|
|
use Triadev\Leopard\Contract\ElasticsearchManagerContract; |
15
|
|
|
use Triadev\Leopard\Contract\FillerContract; |
16
|
|
|
use Triadev\Leopard\Searchable; |
17
|
|
|
use Triadev\Leopard\Model\SearchResult; |
18
|
|
|
use Triadev\Leopard\Business\Dsl\Query\TermLevel; |
19
|
|
|
|
20
|
|
|
class Search extends AbstractQuery |
21
|
|
|
{ |
22
|
|
|
use IsModelSearchable; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $index; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $type; |
29
|
|
|
|
30
|
|
|
/** @var Model */ |
31
|
|
|
private $model; |
32
|
|
|
|
33
|
|
|
/** @var ElasticsearchManagerContract */ |
34
|
|
|
private $manager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Search constructor. |
38
|
|
|
* @param ElasticsearchManagerContract $manager |
39
|
|
|
* @param \ONGR\ElasticsearchDSL\Search|null $search |
40
|
|
|
*/ |
41
|
29 |
|
public function __construct( |
42
|
|
|
ElasticsearchManagerContract $manager, |
43
|
|
|
?\ONGR\ElasticsearchDSL\Search $search = null |
44
|
|
|
) { |
45
|
29 |
|
parent::__construct($search); |
46
|
|
|
|
47
|
29 |
|
$this->manager = $manager; |
48
|
|
|
|
49
|
29 |
|
$this->index = config('leopard.index'); |
50
|
29 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Overwrite default index |
54
|
|
|
* |
55
|
|
|
* @param string $index |
56
|
|
|
* @return Search |
57
|
|
|
*/ |
58
|
3 |
|
public function overwriteIndex(string $index) : Search |
59
|
|
|
{ |
60
|
3 |
|
$this->index = $index; |
61
|
3 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get index |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
1 |
|
public function getIndex() : string |
70
|
|
|
{ |
71
|
1 |
|
return $this->index; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Overwrite default type |
76
|
|
|
* |
77
|
|
|
* @param string $type |
78
|
|
|
* @return Search |
79
|
|
|
*/ |
80
|
3 |
|
public function overwriteType(string $type) : Search |
81
|
|
|
{ |
82
|
3 |
|
$this->type = $type; |
83
|
3 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get type |
88
|
|
|
* |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
1 |
|
public function getType() : ?string |
92
|
|
|
{ |
93
|
1 |
|
return $this->type; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add model |
98
|
|
|
* |
99
|
|
|
* @param Model|Searchable $model |
100
|
|
|
* @return Search |
101
|
|
|
* |
102
|
|
|
* @throws \InvalidArgumentException |
103
|
|
|
*/ |
104
|
3 |
|
public function model(Model $model) : Search |
105
|
|
|
{ |
106
|
3 |
|
$this->isModelSearchable($model); |
107
|
|
|
|
108
|
2 |
|
$this->model = $model; |
109
|
|
|
|
110
|
2 |
|
if (is_string($index = $model->getDocumentIndex())) { |
111
|
2 |
|
$this->overwriteIndex($index); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
$this->overwriteType($model->getDocumentType()); |
|
|
|
|
115
|
|
|
|
116
|
2 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get |
121
|
|
|
* |
122
|
|
|
* @param FillerContract|null $filler |
123
|
|
|
* @return SearchResult |
124
|
|
|
*/ |
125
|
2 |
|
public function get(?FillerContract $filler = null) : SearchResult |
126
|
|
|
{ |
127
|
2 |
|
$searchResult = new SearchResult($this->getRaw()); |
128
|
|
|
|
129
|
2 |
|
if ($this->model) { |
130
|
1 |
|
$filler = $filler ?: new EloquentFiller(); |
131
|
1 |
|
$filler->fill($this->model, $searchResult); |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
return $searchResult; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get raw search result |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
2 |
|
public function getRaw() : array |
143
|
|
|
{ |
144
|
|
|
$params = [ |
145
|
2 |
|
'index' => $this->index, |
146
|
2 |
|
'body' => $this->toDsl() |
147
|
|
|
]; |
148
|
|
|
|
149
|
2 |
|
if ($this->type) { |
150
|
2 |
|
$params['type'] = $this->type; |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
return $this->manager->searchStatement($params); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Aggregation |
158
|
|
|
* |
159
|
|
|
* @param \Closure $aggregation |
160
|
|
|
* @return Search |
161
|
|
|
*/ |
162
|
1 |
|
public function aggregation(\Closure $aggregation) : Search |
163
|
|
|
{ |
164
|
1 |
|
$aggregation(new Aggregation($this->search)); |
165
|
1 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Term level |
170
|
|
|
* |
171
|
|
|
* @param \Closure $termLevel |
172
|
|
|
* @return Search |
173
|
|
|
*/ |
174
|
21 |
|
public function termLevel(\Closure $termLevel) : Search |
175
|
|
|
{ |
176
|
21 |
|
$termLevel(new TermLevel($this->search)); |
177
|
21 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Fulltext |
182
|
|
|
* |
183
|
|
|
* @param \Closure $fulltext |
184
|
|
|
* @return Search |
185
|
|
|
*/ |
186
|
1 |
|
public function fulltext(\Closure $fulltext) : Search |
187
|
|
|
{ |
188
|
1 |
|
$fulltext(new Fulltext($this->search)); |
189
|
1 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Geo |
194
|
|
|
* |
195
|
|
|
* @param \Closure $geo |
196
|
|
|
* @return Search |
197
|
|
|
*/ |
198
|
1 |
|
public function geo(\Closure $geo) : Search |
199
|
|
|
{ |
200
|
1 |
|
$geo(new Geo($this->search)); |
201
|
1 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Compound |
206
|
|
|
* |
207
|
|
|
* @param \Closure $compound |
208
|
|
|
* @return Search |
209
|
|
|
*/ |
210
|
7 |
|
public function compound(\Closure $compound) : Search |
211
|
|
|
{ |
212
|
7 |
|
$compound(new Compound($this->search)); |
213
|
7 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Joining |
218
|
|
|
* |
219
|
|
|
* @param \Closure $joining |
220
|
|
|
* @return Search |
221
|
|
|
*/ |
222
|
1 |
|
public function joining(\Closure $joining) : Search |
223
|
|
|
{ |
224
|
1 |
|
$joining(new Joining($this->search)); |
225
|
1 |
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Specialized |
230
|
|
|
* |
231
|
|
|
* @param \Closure $specialized |
232
|
|
|
* @return Search |
233
|
|
|
*/ |
234
|
1 |
|
public function specialized(\Closure $specialized) : Search |
235
|
|
|
{ |
236
|
1 |
|
$specialized(new Specialized($this->search)); |
237
|
1 |
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Inner hit |
242
|
|
|
* |
243
|
|
|
* @param \Closure $innerHit |
244
|
|
|
* @return Search |
245
|
|
|
*/ |
246
|
1 |
|
public function innerHit(\Closure $innerHit) : Search |
247
|
|
|
{ |
248
|
1 |
|
$innerHit(new InnerHit($this->search)); |
249
|
1 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Paginate |
254
|
|
|
* |
255
|
|
|
* @param int $page |
256
|
|
|
* @param int $limit |
257
|
|
|
* @return Search |
258
|
|
|
*/ |
259
|
1 |
|
public function paginate(int $page, int $limit = 25) : Search |
260
|
|
|
{ |
261
|
1 |
|
$this->search |
262
|
1 |
|
->setFrom($limit * ($page - 1)) |
263
|
1 |
|
->setSize($limit); |
264
|
|
|
|
265
|
1 |
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Min score |
270
|
|
|
* |
271
|
|
|
* @param int $minScore |
272
|
|
|
* @return Search |
273
|
|
|
*/ |
274
|
1 |
|
public function minScore(int $minScore) : Search |
275
|
|
|
{ |
276
|
1 |
|
$this->search->setMinScore($minScore); |
277
|
1 |
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Sort |
282
|
|
|
* |
283
|
|
|
* @param string $field |
284
|
|
|
* @param string $order |
285
|
|
|
* @param array $params |
286
|
|
|
* @return Search |
287
|
|
|
*/ |
288
|
1 |
|
public function sort(string $field, string $order = FieldSort::DESC, array $params = []) : Search |
289
|
|
|
{ |
290
|
1 |
|
$this->search->addSort(new FieldSort( |
291
|
1 |
|
$field, |
292
|
1 |
|
$order, |
293
|
1 |
|
$params |
294
|
|
|
)); |
295
|
|
|
|
296
|
1 |
|
return $this; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|