Skip to content

Commit 9c49d71

Browse files
authored
Update README example (#12)
1 parent 0252dc1 commit 9c49d71

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

README.md

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ In your `config/scout.php` add:
8686

8787
## Usage
8888

89+
If you are unfamiliar with Laravel Scout, we suggest reading it's [documentation](https://laravel.com/docs/8.x/scout) first.
90+
8991
After you have installed scout and the Typesense driver, you need to add the
9092
`Searchable` trait to your models that you want to make searchable. Additionaly,
9193
define the fields you want to make searchable by defining the `toSearchableArray` method on the model and implement `TypesenseSearch`:
@@ -99,67 +101,84 @@ use Illuminate\Database\Eloquent\Model;
99101
use Typesense\LaravelTypesense\Interfaces\TypesenseDocument;
100102
use Laravel\Scout\Searchable;
101103

102-
class Post extends Model implements TypesenseDocument
104+
class Todo extends Model implements TypesenseDocument
103105
{
104106
use Searchable;
105-
106-
/**
107+
108+
/**
107109
* Get the indexable data array for the model.
108110
*
109111
* @return array
110112
*/
111113
public function toSearchableArray()
112114
{
113-
$array = $this->toArray();
114-
115-
// Customize array...
116-
117-
return $array;
115+
return array_merge(
116+
$this->toArray(),
117+
[
118+
// Cast id to string and turn created_at into an int32 timestamp
119+
// in order to maintain compatibility with the Typesense index definition below
120+
'id' => (string) $this->id,
121+
'created_at' => $this->created_at->timestamp,
122+
]
123+
);
118124
}
119125

126+
/**
127+
* The Typesense schema to be created.
128+
*
129+
* @return array
130+
*/
120131
public function getCollectionSchema(): array {
121-
return [
122-
'name' => $this->searchableAs(),
123-
'fields' => [
124-
[
125-
'name' => 'title',
126-
'type' => 'string',
127-
],
128-
[
129-
'name' => 'created_at',
130-
'type' => 'int32',
131-
],
132-
],
133-
'default_sorting_field' => 'created_at',
134-
];
132+
return [
133+
'name' => $this->searchableAs(),
134+
'fields' => [
135+
[
136+
'name' => 'id',
137+
'type' => 'string',
138+
],
139+
[
140+
'name' => 'name',
141+
'type' => 'string',
142+
],
143+
[
144+
'name' => 'created_at',
145+
'type' => 'int64',
146+
],
147+
],
148+
'default_sorting_field' => 'created_at',
149+
];
135150
}
136151

152+
/**
153+
* The fields to be queried against. See https://typesense.org/docs/0.21.0/api/documents.html#search.
154+
*
155+
* @return array
156+
*/
137157
public function typesenseQueryBy(): array {
138-
return [
139-
'name',
140-
];
141-
}
142-
158+
return [
159+
'name',
160+
];
161+
}
143162
}
144163
```
145164

146165
Then, sync the data with the search service like:
147166

148-
`php artisan scout:import App\\Post`
167+
`php artisan scout:import App\\Models\\Todo`
149168

150169
After that you can search your models with:
151170

152-
`Post::search('Bugs Bunny')->get();`
171+
`Todo::search('Test')->get();`
153172

154173
## Adding via Query
155174
The `searchable()` method will chunk the results of the query and add the records to your search index. Examples:
156175

157176
```php
158-
$post = Post::find(1);
159-
$post->searchable();
177+
$todo = Todo::find(1);
178+
$todo->searchable();
160179

161-
$posts = Post::where('year', '>', '2018')->get();
162-
$posts->searchable();
180+
$todos = Todo::where('created_at', '<', now())->get();
181+
$todos->searchable();
163182
```
164183

165184
## Migrating from devloopsnet/laravel-typesense
@@ -176,6 +195,7 @@ This package was originally authored by [Abdullah Al-Faqeir](https://github.com/
176195
Other key contributors include:
177196

178197
- [hi019](https://github.com/hi019)
198+
- [Philip Manavopoulos](https://github.com/manavo)
179199

180200
## License
181201

0 commit comments

Comments
 (0)