Skip to content

Commit b8db2cf

Browse files
committed
Adding examples
1 parent 839c46e commit b8db2cf

File tree

3 files changed

+88
-21
lines changed

3 files changed

+88
-21
lines changed

README.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
# Laravel Typesense Tools
22

3-
`synergitech/laravel-typesense-tools` adds Artisan commands that help manage Typesense collections and aliases in Laravel applications using Scout.
4-
5-
## What this package provides
6-
7-
- `search:setup` to ensure collections exist and (optionally) import data.
8-
- `search:switch-alias` to point aliases at the current index collections.
9-
- `search:delete-index {suffix}` to remove old suffixed collections.
10-
- `search:cleanup` to remove collections that are not referenced by an alias.
3+
This package adds Artisan commands that help manage Typesense collections and aliases in Laravel applications using Scout.
114

125
## Requirements
136

@@ -22,33 +15,39 @@
2215
composer require synergitech/laravel-typesense-tools
2316
```
2417

18+
## What this package provides
19+
20+
- `search:setup` to ensure collections exist and (optionally) import data.
21+
- `search:switch-alias` to point aliases at the current index collections.
22+
- `search:delete-index {suffix}` to remove old suffixed collections.
23+
- `search:cleanup` to remove collections that are not referenced by an alias.
24+
2525
## Configuration expectations
2626

2727
This package reads model configuration from:
2828

2929
- `config('scout.typesense.model-settings')`
3030

31-
For each configured model, this package expects:
31+
For each configured model, define settings like:
3232

33-
- `indexableAs()` (used for collection names)
34-
- `searchableAs()` (used for alias names and deletion targets)
33+
- `name` (typically `Model::indexableAs()`)
34+
- `collection-schema` (Typically `User::getCollectionSchema()`)
35+
- `search-parameters.query_by` (comma-separated searchable fields)
3536

36-
When creating missing collections, `search:setup` uses the model schema from:
37-
38-
- `scout.typesense.model-settings.<ModelClass>.collection-schema`
37+
See the full examples in [`examples/models/User.php`](examples/models/User.php) and [`examples/config/scout.php`](examples/config/scout.php).
3938

4039
Example `config/scout.php` shape:
4140

4241
```php
42+
use App\Models\User;
43+
4344
'typesense' => [
4445
'model-settings' => [
45-
App\\Models\\Post::class => [
46-
'collection-schema' => [
47-
'name' => 'posts_tmp_20260325',
48-
'fields' => [
49-
['name' => 'id', 'type' => 'string'],
50-
['name' => 'title', 'type' => 'string'],
51-
],
46+
User::class => [
47+
'name' => User::indexableAs(),
48+
'collection-schema' => User::getCollectionSchema(),
49+
'search-parameters' => [
50+
'query_by' => implode(',', User::typesenseQueryBy()),
5251
],
5352
],
5453
],

examples/config/scout.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use App\Models\User;
4+
5+
return [
6+
// ... Other Scout config goes here
7+
8+
'typesense' => [
9+
'model-settings' => [
10+
User::class => [
11+
'name' => User::indexableAs(),
12+
'collection-schema' => User::getCollectionSchema(),
13+
'search-parameters' => [
14+
'query_by' => implode(',', User::typesenseQueryBy()),
15+
],
16+
],
17+
],
18+
],
19+
];

examples/models/User.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Laravel\Scout\Searchable;
6+
7+
class User
8+
{
9+
use Searchable;
10+
11+
public static function indexableAs(): string
12+
{
13+
return 'users_' . env('TYPESENSE_VERSION_SUFFIX', 'default');
14+
}
15+
16+
public static function getCollectionSchema(): array
17+
{
18+
return [
19+
'name' => self::indexableAs(),
20+
'fields' => [
21+
[
22+
'name' => 'id',
23+
'type' => 'string',
24+
],
25+
[
26+
'name' => 'name',
27+
'type' => 'string',
28+
],
29+
[
30+
'name' => 'email',
31+
'type' => 'string',
32+
],
33+
[
34+
'name' => 'created_at',
35+
'type' => 'int64',
36+
],
37+
],
38+
'default_sorting_field' => 'created_at',
39+
];
40+
}
41+
42+
public static function typesenseQueryBy(): array
43+
{
44+
return [
45+
'name',
46+
'email',
47+
];
48+
}
49+
}

0 commit comments

Comments
 (0)