@@ -86,6 +86,8 @@ In your `config/scout.php` add:
86
86
87
87
## Usage
88
88
89
+ If you are unfamiliar with Laravel Scout, we suggest reading it's [ documentation] ( https://laravel.com/docs/8.x/scout ) first.
90
+
89
91
After you have installed scout and the Typesense driver, you need to add the
90
92
` Searchable ` trait to your models that you want to make searchable. Additionaly,
91
93
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;
99
101
use Typesense\LaravelTypesense\Interfaces\TypesenseDocument;
100
102
use Laravel\Scout\Searchable;
101
103
102
- class Post extends Model implements TypesenseDocument
104
+ class Todo extends Model implements TypesenseDocument
103
105
{
104
106
use Searchable;
105
-
106
- /**
107
+
108
+ /**
107
109
* Get the indexable data array for the model.
108
110
*
109
111
* @return array
110
112
*/
111
113
public function toSearchableArray()
112
114
{
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
+ );
118
124
}
119
125
126
+ /**
127
+ * The Typesense schema to be created.
128
+ *
129
+ * @return array
130
+ */
120
131
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
+ ];
135
150
}
136
151
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
+ */
137
157
public function typesenseQueryBy(): array {
138
- return [
139
- 'name',
140
- ];
141
- }
142
-
158
+ return [
159
+ 'name',
160
+ ];
161
+ }
143
162
}
144
163
```
145
164
146
165
Then, sync the data with the search service like:
147
166
148
- ` php artisan scout:import App\\Post `
167
+ ` php artisan scout:import App\\Models\\Todo `
149
168
150
169
After that you can search your models with:
151
170
152
- ` Post ::search('Bugs Bunny ')->get();`
171
+ ` Todo ::search('Test ')->get();`
153
172
154
173
## Adding via Query
155
174
The ` searchable() ` method will chunk the results of the query and add the records to your search index. Examples:
156
175
157
176
``` php
158
- $post = Post ::find(1);
159
- $post ->searchable();
177
+ $todo = Todo ::find(1);
178
+ $todo ->searchable();
160
179
161
- $posts = Post ::where('year ', '> ', '2018' )->get();
162
- $posts ->searchable();
180
+ $todos = Todo ::where('created_at ', '< ', now() )->get();
181
+ $todos ->searchable();
163
182
```
164
183
165
184
## Migrating from devloopsnet/laravel-typesense
@@ -176,6 +195,7 @@ This package was originally authored by [Abdullah Al-Faqeir](https://github.com/
176
195
Other key contributors include:
177
196
178
197
- [ hi019] ( https://github.com/hi019 )
198
+ - [ Philip Manavopoulos] ( https://github.com/manavo )
179
199
180
200
## License
181
201
0 commit comments