2
2
3
3
namespace Devloops \LaravelTypesense \Engines ;
4
4
5
+ use Devloops \LaravelTypesense \Typesense ;
5
6
use Exception ;
6
- use Laravel \Scout \Builder ;
7
- use Illuminate \Support \Str ;
8
- use Laravel \Scout \Engines \Engine ;
9
- use Illuminate \Support \Collection ;
10
7
use Illuminate \Support \LazyCollection ;
8
+ use Illuminate \Support \Collection ;
9
+ use Illuminate \Support \Str ;
11
10
use Illuminate \Database \Eloquent \Model ;
12
- use Devloops \LaravelTypesense \Typesense ;
13
11
use Illuminate \Database \Eloquent \SoftDeletes ;
12
+ use Laravel \Scout \Builder ;
13
+ use Laravel \Scout \Engines \Engine ;
14
14
15
15
/**
16
- * Class TypesenseSearchEngine
16
+ * Class TypesenseSearchEngine.
17
17
*
18
- * @package Devloops\LaravelTypesense\Engines
19
18
* @date 4/5/20
19
+ *
20
20
* @author Abdullah Al-Faqeir <[email protected] >
21
21
*/
22
22
class TypesenseSearchEngine extends Engine
@@ -59,15 +59,15 @@ class TypesenseSearchEngine extends Engine
59
59
/**
60
60
* TypesenseSearchEngine constructor.
61
61
*
62
- * @param \Devloops\LaravelTypesense\Typesense $typesense
62
+ * @param \Devloops\LaravelTypesense\Typesense $typesense
63
63
*/
64
64
public function __construct (Typesense $ typesense )
65
65
{
66
66
$ this ->typesense = $ typesense ;
67
67
}
68
68
69
69
/**
70
- * @param \Illuminate\Database\Eloquent\Collection<int, Model>|Model[] $models
70
+ * @param \Illuminate\Database\Eloquent\Collection<int, Model>|Model[] $models
71
71
*
72
72
* @throws \Http\Client\Exception
73
73
* @throws \JsonException
@@ -87,7 +87,7 @@ public function update($models): void
87
87
}
88
88
89
89
/**
90
- * @param \Illuminate\Database\Eloquent\Collection $models
90
+ * @param \Illuminate\Database\Eloquent\Collection $models
91
91
*
92
92
* @throws \Http\Client\Exception
93
93
* @throws \Typesense\Exceptions\TypesenseClientError
@@ -102,7 +102,7 @@ public function delete($models): void
102
102
}
103
103
104
104
/**
105
- * @param \Laravel\Scout\Builder $builder
105
+ * @param \Laravel\Scout\Builder $builder
106
106
*
107
107
* @return mixed
108
108
* @throws \Http\Client\Exception
@@ -114,9 +114,9 @@ public function search(Builder $builder): mixed
114
114
}
115
115
116
116
/**
117
- * @param \Laravel\Scout\Builder $builder
118
- * @param int $perPage
119
- * @param int $page
117
+ * @param \Laravel\Scout\Builder $builder
118
+ * @param int $perPage
119
+ * @param int $page
120
120
*
121
121
* @return mixed
122
122
* @throws \Http\Client\Exception
@@ -128,39 +128,39 @@ public function paginate(Builder $builder, $perPage, $page): mixed
128
128
}
129
129
130
130
/**
131
- * @param \Laravel\Scout\Builder $builder
132
- * @param int $page
133
- * @param int $perPage
131
+ * @param \Laravel\Scout\Builder $builder
132
+ * @param int $page
133
+ * @param int $perPage
134
134
*
135
135
* @return array
136
136
*/
137
137
private function buildSearchParams (Builder $ builder , int $ page , int $ perPage ): array
138
138
{
139
139
$ params = [
140
- 'q ' => $ builder ->query ,
141
- 'query_by ' => implode (', ' , $ builder ->model ->typesenseQueryBy ()),
142
- 'filter_by ' => $ this ->filters ($ builder ),
143
- 'per_page ' => $ perPage ,
144
- 'page ' => $ page ,
140
+ 'q ' => $ builder ->query ,
141
+ 'query_by ' => implode (', ' , $ builder ->model ->typesenseQueryBy ()),
142
+ 'filter_by ' => $ this ->filters ($ builder ),
143
+ 'per_page ' => $ perPage ,
144
+ 'page ' => $ page ,
145
145
'highlight_start_tag ' => $ this ->startTag ,
146
- 'highlight_end_tag ' => $ this ->endTag ,
146
+ 'highlight_end_tag ' => $ this ->endTag ,
147
147
];
148
148
149
149
if ($ this ->limitHits > 0 ) {
150
150
$ params ['limit_hits ' ] = $ this ->limitHits ;
151
151
}
152
152
153
- if (! empty ($ this ->groupBy )) {
153
+ if (!empty ($ this ->groupBy )) {
154
154
$ params ['group_by ' ] = implode (', ' , $ this ->groupBy );
155
155
$ params ['group_limit ' ] = $ this ->groupByLimit ;
156
156
}
157
157
158
- if (! empty ($ this ->locationOrderBy )) {
158
+ if (!empty ($ this ->locationOrderBy )) {
159
159
$ params ['sort_by ' ] = $ this ->parseOrderByLocation (...$ this ->locationOrderBy );
160
160
}
161
161
162
- if (! empty ($ builder ->orders )) {
163
- if (! empty ($ params ['sort_by ' ])) {
162
+ if (!empty ($ builder ->orders )) {
163
+ if (!empty ($ params ['sort_by ' ])) {
164
164
$ params ['sort_by ' ] .= ', ' ;
165
165
} else {
166
166
$ params ['sort_by ' ] = '' ;
@@ -172,12 +172,12 @@ private function buildSearchParams(Builder $builder, int $page, int $perPage): a
172
172
}
173
173
174
174
/**
175
- * Parse location order by for sort_by
175
+ * Parse location order by for sort_by.
176
176
*
177
- * @param string $column
178
- * @param float $lat
179
- * @param float $lng
180
- * @param string $direction
177
+ * @param string $column
178
+ * @param float $lat
179
+ * @param float $lng
180
+ * @param string $direction
181
181
*
182
182
* @return string
183
183
* @noinspection PhpPureAttributeCanBeAddedInspection
@@ -192,7 +192,7 @@ private function parseOrderByLocation(string $column, float $lat, float $lng, st
192
192
/**
193
193
* Parse sort_by fields
194
194
*
195
- * @param array $orders
195
+ * @param array $orders
196
196
*
197
197
* @return string
198
198
*/
@@ -206,8 +206,8 @@ private function parseOrderBy(array $orders): string
206
206
}
207
207
208
208
/**
209
- * @param \Laravel\Scout\Builder $builder
210
- * @param array $options
209
+ * @param \Laravel\Scout\Builder $builder
210
+ * @param array $options
211
211
*
212
212
* @return mixed
213
213
* @throws \Http\Client\Exception
@@ -226,7 +226,7 @@ protected function performSearch(Builder $builder, array $options = []): mixed
226
226
/**
227
227
* Prepare filters
228
228
*
229
- * @param Builder $builder
229
+ * @param Builder $builder
230
230
*
231
231
* @return string
232
232
*/
@@ -244,8 +244,8 @@ protected function filters(Builder $builder): string
244
244
/**
245
245
* Parse typesense filters
246
246
*
247
- * @param array|string $value
248
- * @param string $key
247
+ * @param array|string $value
248
+ * @param string $key
249
249
*
250
250
* @return string
251
251
*/
@@ -258,7 +258,7 @@ public function parseFilters(array|string $value, string $key): string
258
258
}
259
259
260
260
/**
261
- * @param mixed $results
261
+ * @param mixed $results
262
262
*
263
263
* @return \Illuminate\Support\Collection
264
264
*/
@@ -270,9 +270,9 @@ public function mapIds($results): Collection
270
270
}
271
271
272
272
/**
273
- * @param \Laravel\Scout\Builder $builder
274
- * @param mixed $results
275
- * @param \Illuminate\Database\Eloquent\Model $model
273
+ * @param \Laravel\Scout\Builder $builder
274
+ * @param mixed $results
275
+ * @param \Illuminate\Database\Eloquent\Model $model
276
276
*
277
277
* @return \Illuminate\Database\Eloquent\Collection
278
278
*/
@@ -307,7 +307,7 @@ public function getTotalCount($results): int
307
307
}
308
308
309
309
/**
310
- * @param \Illuminate\Database\Eloquent\Model $model
310
+ * @param \Illuminate\Database\Eloquent\Model $model
311
311
*
312
312
* @throws \Http\Client\Exception
313
313
* @throws \Typesense\Exceptions\TypesenseClientError
@@ -329,9 +329,9 @@ protected function usesSoftDelete($model): bool
329
329
}
330
330
331
331
/**
332
- * @param \Laravel\Scout\Builder $builder
333
- * @param mixed $results
334
- * @param \Illuminate\Database\Eloquent\Model $model
332
+ * @param \Laravel\Scout\Builder $builder
333
+ * @param mixed $results
334
+ * @param \Illuminate\Database\Eloquent\Model $model
335
335
*
336
336
* @return \Illuminate\Support\LazyCollection
337
337
*/
@@ -360,8 +360,8 @@ public function lazyMap(Builder $builder, $results, $model): LazyCollection
360
360
}
361
361
362
362
/**
363
- * @param string $name
364
- * @param array $options
363
+ * @param string $name
364
+ * @param array $options
365
365
*
366
366
* @return void
367
367
* @throws \Exception
@@ -374,7 +374,7 @@ public function createIndex($name, array $options = []): void
374
374
/**
375
375
* You can aggregate search results into groups or buckets by specify one or more group_by fields. Separate multiple fields with a comma.
376
376
*
377
- * @param mixed $groupBy
377
+ * @param mixed $groupBy
378
378
*
379
379
* @return $this
380
380
*/
@@ -387,7 +387,7 @@ public function groupBy(array $groupBy): static
387
387
/**
388
388
* Maximum number of hits to be returned for every group. (default: 3)
389
389
*
390
- * @param int $groupByLimit
390
+ * @param int $groupByLimit
391
391
*
392
392
* @return $this
393
393
*/
@@ -400,7 +400,7 @@ public function groupByLimit(int $groupByLimit): static
400
400
/**
401
401
* The start tag used for the highlighted snippets. (default: <mark>)
402
402
*
403
- * @param string $startTag
403
+ * @param string $startTag
404
404
*
405
405
* @return $this
406
406
*/
@@ -413,7 +413,7 @@ public function setHighlightStartTag(string $startTag): static
413
413
/**
414
414
* The end tag used for the highlighted snippets. (default: </mark>)
415
415
*
416
- * @param string $endTag
416
+ * @param string $endTag
417
417
*
418
418
* @return $this
419
419
*/
@@ -428,7 +428,7 @@ public function setHighlightEndTag(string $endTag): static
428
428
*
429
429
* (page * per_page) should be less than this number for the search request to return results.
430
430
*
431
- * @param int $limitHits
431
+ * @param int $limitHits
432
432
*
433
433
* @return $this
434
434
*/
@@ -441,26 +441,26 @@ public function limitHits(int $limitHits): static
441
441
/**
442
442
* Add location to order by clause
443
443
*
444
- * @param string $column
445
- * @param float $lat
446
- * @param float $lng
447
- * @param string $direction
444
+ * @param string $column
445
+ * @param float $lat
446
+ * @param float $lng
447
+ * @param string $direction
448
448
*
449
449
* @return $this
450
450
*/
451
451
public function orderByLocation (string $ column , float $ lat , float $ lng , string $ direction ): static
452
452
{
453
453
$ this ->locationOrderBy = [
454
- 'column ' => $ column ,
455
- 'lat ' => $ lat ,
456
- 'lng ' => $ lng ,
454
+ 'column ' => $ column ,
455
+ 'lat ' => $ lat ,
456
+ 'lng ' => $ lng ,
457
457
'direction ' => $ direction ,
458
458
];
459
459
return $ this ;
460
460
}
461
461
462
462
/**
463
- * @param string $name
463
+ * @param string $name
464
464
*
465
465
* @return array
466
466
* @throws \Http\Client\Exception
0 commit comments