4
4
5
5
use Devloops \LaravelTypesense \Typesense ;
6
6
use Exception ;
7
- use Illuminate \Support \LazyCollection ;
8
- use Illuminate \Support \Collection ;
9
- use Illuminate \Support \Str ;
10
7
use Illuminate \Database \Eloquent \Model ;
11
8
use Illuminate \Database \Eloquent \SoftDeletes ;
9
+ use Illuminate \Support \Collection ;
10
+ use Illuminate \Support \LazyCollection ;
11
+ use Illuminate \Support \Str ;
12
12
use Laravel \Scout \Builder ;
13
13
use Laravel \Scout \Engines \Engine ;
14
14
@@ -82,7 +82,7 @@ public function update($models): void
82
82
$ models ->each ->pushSoftDeleteMetadata ();
83
83
}
84
84
85
- $ this ->typesense ->importDocuments ($ collection , $ models ->map (fn ($ m ) => $ m ->toSearchableArray ())
85
+ $ this ->typesense ->importDocuments ($ collection , $ models ->map (fn ($ m ) => $ m ->toSearchableArray ())
86
86
->toArray ());
87
87
}
88
88
@@ -104,9 +104,10 @@ public function delete($models): void
104
104
/**
105
105
* @param \Laravel\Scout\Builder $builder
106
106
*
107
- * @return mixed
108
107
* @throws \Http\Client\Exception
109
108
* @throws \Typesense\Exceptions\TypesenseClientError
109
+ *
110
+ * @return mixed
110
111
*/
111
112
public function search (Builder $ builder ): mixed
112
113
{
@@ -115,12 +116,13 @@ public function search(Builder $builder): mixed
115
116
116
117
/**
117
118
* @param \Laravel\Scout\Builder $builder
118
- * @param int $perPage
119
- * @param int $page
119
+ * @param int $perPage
120
+ * @param int $page
120
121
*
121
- * @return mixed
122
122
* @throws \Http\Client\Exception
123
123
* @throws \Typesense\Exceptions\TypesenseClientError
124
+ *
125
+ * @return mixed
124
126
*/
125
127
public function paginate (Builder $ builder , $ perPage , $ page ): mixed
126
128
{
@@ -129,21 +131,21 @@ public function paginate(Builder $builder, $perPage, $page): mixed
129
131
130
132
/**
131
133
* @param \Laravel\Scout\Builder $builder
132
- * @param int $page
133
- * @param int $perPage
134
+ * @param int $page
135
+ * @param int $perPage
134
136
*
135
137
* @return array
136
138
*/
137
139
private function buildSearchParams (Builder $ builder , int $ page , int $ perPage ): array
138
140
{
139
141
$ 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 ,
145
- 'highlight_start_tag ' => $ this ->startTag ,
146
- 'highlight_end_tag ' => $ this ->endTag ,
142
+ 'q ' => $ builder ->query ,
143
+ 'query_by ' => implode (', ' , $ builder ->model ->typesenseQueryBy ()),
144
+ 'filter_by ' => $ this ->filters ($ builder ),
145
+ 'per_page ' => $ perPage ,
146
+ 'page ' => $ page ,
147
+ 'highlight_start_tag ' => $ this ->startTag ,
148
+ 'highlight_end_tag ' => $ this ->endTag ,
147
149
];
148
150
149
151
if ($ this ->limitHits > 0 ) {
@@ -175,8 +177,8 @@ private function buildSearchParams(Builder $builder, int $page, int $perPage): a
175
177
* Parse location order by for sort_by.
176
178
*
177
179
* @param string $column
178
- * @param float $lat
179
- * @param float $lng
180
+ * @param float $lat
181
+ * @param float $lng
180
182
* @param string $direction
181
183
*
182
184
* @return string
@@ -186,11 +188,12 @@ private function parseOrderByLocation(string $column, float $lat, float $lng, st
186
188
{
187
189
$ direction = Str::lower ($ direction ) === 'asc ' ? 'asc ' : 'desc ' ;
188
190
$ str = $ column .'( ' .$ lat .', ' .$ lng .') ' ;
191
+
189
192
return $ str .': ' .$ direction ;
190
193
}
191
194
192
195
/**
193
- * Parse sort_by fields
196
+ * Parse sort_by fields.
194
197
*
195
198
* @param array $orders
196
199
*
@@ -202,16 +205,18 @@ private function parseOrderBy(array $orders): string
202
205
foreach ($ orders as $ order ) {
203
206
$ sortByArr [] = $ order ['column ' ].': ' .$ order ['direction ' ];
204
207
}
208
+
205
209
return implode (', ' , $ sortByArr );
206
210
}
207
211
208
212
/**
209
213
* @param \Laravel\Scout\Builder $builder
210
- * @param array $options
214
+ * @param array $options
211
215
*
212
- * @return mixed
213
216
* @throws \Http\Client\Exception
214
217
* @throws \Typesense\Exceptions\TypesenseClientError
218
+ *
219
+ * @return mixed
215
220
*/
216
221
protected function performSearch (Builder $ builder , array $ options = []): mixed
217
222
{
@@ -220,11 +225,12 @@ protected function performSearch(Builder $builder, array $options = []): mixed
220
225
if ($ builder ->callback ) {
221
226
return call_user_func ($ builder ->callback , $ documents , $ builder ->query , $ options );
222
227
}
228
+
223
229
return $ documents ->search ($ options );
224
230
}
225
231
226
232
/**
227
- * Prepare filters
233
+ * Prepare filters.
228
234
*
229
235
* @param Builder $builder
230
236
*
@@ -234,18 +240,18 @@ protected function filters(Builder $builder): string
234
240
{
235
241
return collect ($ builder ->wheres )
236
242
->map ([
237
- $ this ,
238
- 'parseFilters ' ,
243
+ $ this ,
244
+ 'parseFilters ' ,
239
245
])
240
246
->values ()
241
247
->implode (' && ' );
242
248
}
243
249
244
250
/**
245
- * Parse typesense filters
251
+ * Parse typesense filters.
246
252
*
247
253
* @param array|string $value
248
- * @param string $key
254
+ * @param string $key
249
255
*
250
256
* @return string
251
257
*/
@@ -254,6 +260,7 @@ public function parseFilters(array|string $value, string $key): string
254
260
if (is_array ($ value )) {
255
261
return sprintf ('%s:%s ' , $ key , implode ('' , $ value ));
256
262
}
263
+
257
264
return sprintf ('%s:=%s ' , $ key , $ value );
258
265
}
259
266
@@ -270,15 +277,15 @@ public function mapIds($results): Collection
270
277
}
271
278
272
279
/**
273
- * @param \Laravel\Scout\Builder $builder
274
- * @param mixed $results
280
+ * @param \Laravel\Scout\Builder $builder
281
+ * @param mixed $results
275
282
* @param \Illuminate\Database\Eloquent\Model $model
276
283
*
277
284
* @return \Illuminate\Database\Eloquent\Collection
278
285
*/
279
286
public function map (Builder $ builder , $ results , $ model ): \Illuminate \Database \Eloquent \Collection
280
287
{
281
- if ((int )($ results ['found ' ] ?? 0 ) === 0 ) {
288
+ if ((int ) ($ results ['found ' ] ?? 0 ) === 0 ) {
282
289
return $ model ->newCollection ();
283
290
}
284
291
@@ -288,6 +295,7 @@ public function map(Builder $builder, $results, $model): \Illuminate\Database\El
288
295
->all ();
289
296
290
297
$ objectIdPositions = array_flip ($ objectIds );
298
+
291
299
return $ model ->getScoutModelsByIds ($ builder , $ objectIds )
292
300
->filter (static function ($ model ) use ($ objectIds ) {
293
301
return in_array ($ model ->getScoutKey (), $ objectIds , false );
@@ -303,7 +311,7 @@ public function map(Builder $builder, $results, $model): \Illuminate\Database\El
303
311
*/
304
312
public function getTotalCount ($ results ): int
305
313
{
306
- return (int )($ results ['found ' ] ?? 0 );
314
+ return (int ) ($ results ['found ' ] ?? 0 );
307
315
}
308
316
309
317
/**
@@ -329,15 +337,15 @@ protected function usesSoftDelete($model): bool
329
337
}
330
338
331
339
/**
332
- * @param \Laravel\Scout\Builder $builder
333
- * @param mixed $results
340
+ * @param \Laravel\Scout\Builder $builder
341
+ * @param mixed $results
334
342
* @param \Illuminate\Database\Eloquent\Model $model
335
343
*
336
344
* @return \Illuminate\Support\LazyCollection
337
345
*/
338
346
public function lazyMap (Builder $ builder , $ results , $ model ): LazyCollection
339
347
{
340
- if ((int )($ results ['found ' ] ?? 0 ) === 0 ) {
348
+ if ((int ) ($ results ['found ' ] ?? 0 ) === 0 ) {
341
349
return LazyCollection::make ($ model ->newCollection ());
342
350
}
343
351
@@ -361,10 +369,11 @@ public function lazyMap(Builder $builder, $results, $model): LazyCollection
361
369
362
370
/**
363
371
* @param string $name
364
- * @param array $options
372
+ * @param array $options
365
373
*
366
- * @return void
367
374
* @throws \Exception
375
+ *
376
+ * @return void
368
377
*/
369
378
public function createIndex ($ name , array $ options = []): void
370
379
{
@@ -381,11 +390,12 @@ public function createIndex($name, array $options = []): void
381
390
public function groupBy (array $ groupBy ): static
382
391
{
383
392
$ this ->groupBy = $ groupBy ;
393
+
384
394
return $ this ;
385
395
}
386
396
387
397
/**
388
- * Maximum number of hits to be returned for every group. (default: 3)
398
+ * Maximum number of hits to be returned for every group. (default: 3).
389
399
*
390
400
* @param int $groupByLimit
391
401
*
@@ -394,11 +404,12 @@ public function groupBy(array $groupBy): static
394
404
public function groupByLimit (int $ groupByLimit ): static
395
405
{
396
406
$ this ->groupByLimit = $ groupByLimit ;
407
+
397
408
return $ this ;
398
409
}
399
410
400
411
/**
401
- * The start tag used for the highlighted snippets. (default: <mark>)
412
+ * The start tag used for the highlighted snippets. (default: <mark>).
402
413
*
403
414
* @param string $startTag
404
415
*
@@ -407,11 +418,12 @@ public function groupByLimit(int $groupByLimit): static
407
418
public function setHighlightStartTag (string $ startTag ): static
408
419
{
409
420
$ this ->startTag = $ startTag ;
421
+
410
422
return $ this ;
411
423
}
412
424
413
425
/**
414
- * The end tag used for the highlighted snippets. (default: </mark>)
426
+ * The end tag used for the highlighted snippets. (default: </mark>).
415
427
*
416
428
* @param string $endTag
417
429
*
@@ -420,11 +432,12 @@ public function setHighlightStartTag(string $startTag): static
420
432
public function setHighlightEndTag (string $ endTag ): static
421
433
{
422
434
$ this ->endTag = $ endTag ;
435
+
423
436
return $ this ;
424
437
}
425
438
426
439
/**
427
- * Maximum number of hits that can be fetched from the collection (default: no limit)
440
+ * Maximum number of hits that can be fetched from the collection (default: no limit).
428
441
*
429
442
* (page * per_page) should be less than this number for the search request to return results.
430
443
*
@@ -435,37 +448,40 @@ public function setHighlightEndTag(string $endTag): static
435
448
public function limitHits (int $ limitHits ): static
436
449
{
437
450
$ this ->limitHits = $ limitHits ;
451
+
438
452
return $ this ;
439
453
}
440
454
441
455
/**
442
- * Add location to order by clause
456
+ * Add location to order by clause.
443
457
*
444
458
* @param string $column
445
- * @param float $lat
446
- * @param float $lng
459
+ * @param float $lat
460
+ * @param float $lng
447
461
* @param string $direction
448
462
*
449
463
* @return $this
450
464
*/
451
465
public function orderByLocation (string $ column , float $ lat , float $ lng , string $ direction ): static
452
466
{
453
467
$ this ->locationOrderBy = [
454
- 'column ' => $ column ,
455
- 'lat ' => $ lat ,
456
- 'lng ' => $ lng ,
457
- 'direction ' => $ direction ,
468
+ 'column ' => $ column ,
469
+ 'lat ' => $ lat ,
470
+ 'lng ' => $ lng ,
471
+ 'direction ' => $ direction ,
458
472
];
473
+
459
474
return $ this ;
460
475
}
461
476
462
477
/**
463
478
* @param string $name
464
479
*
465
- * @return array
466
480
* @throws \Http\Client\Exception
467
481
* @throws \Typesense\Exceptions\ObjectNotFound
468
482
* @throws \Typesense\Exceptions\TypesenseClientError
483
+ *
484
+ * @return array
469
485
*/
470
486
public function deleteIndex ($ name ): array
471
487
{
0 commit comments