Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit 0e75a17

Browse files
Create README.md
1 parent c55ed96 commit 0e75a17

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

README.md

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# laravel-scout-typesense-engine
2+
Typesense engine for laravel/scout https://github.com/typesense/typesense .
3+
4+
This package makes it easy to add full text search support to your models with Laravel 5.3 to 7.0.
5+
6+
## Contents
7+
8+
- [Installation](#installation)
9+
- [Usage](#usage)
10+
- [Author](#author)
11+
- [License](#license)
12+
13+
14+
## Installation
15+
16+
You can install the package via composer:
17+
18+
``` bash
19+
composer require devloopsnet/laravel-scout-typesense-engine
20+
```
21+
22+
Add the service provider:
23+
24+
```php
25+
// config/app.php
26+
'providers' => [
27+
// ...
28+
Devloops\LaravelTypesense\TypesenseServiceProvider::class,
29+
],
30+
```
31+
32+
Ensure you have Laravel Scout as a provider too otherwise you will get an "unresolvable dependency" error
33+
34+
```php
35+
// config/app.php
36+
'providers' => [
37+
// ...
38+
Laravel\Scout\ScoutServiceProvider::class,
39+
],
40+
```
41+
42+
Add `SCOUT_DRIVER=typesensesearch` to your `.env` file
43+
44+
Then you should publish `scout.php` configuration file to your config directory
45+
46+
```bash
47+
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
48+
```
49+
50+
In your `config/scout.php` add:
51+
52+
```php
53+
54+
'typesensesearch' => [
55+
'master_node' => [
56+
'host' => 'HOST',
57+
'port' => '8108',
58+
'protocol' => 'http',
59+
'api_key' => 'API_KEY',
60+
],
61+
'enabled_read_replica' => FALSE,
62+
'read_replica_nodes' => [
63+
[
64+
'host' => 'HOST',
65+
'port' => '8108',
66+
'protocol' => 'http',
67+
'api_key' => 'API_KEY',
68+
],
69+
],
70+
'timeout' => 2,
71+
],
72+
```
73+
74+
## Usage
75+
76+
After you have installed scout and the Typesense driver, you need to add the
77+
`Searchable` trait to your models that you want to make searchable. Additionaly,
78+
define the fields you want to make searchable by defining the `toSearchableArray` method on the model and implement `TypesenseSearch`:
79+
80+
```php
81+
<?php
82+
83+
namespace App;
84+
85+
use Illuminate\Database\Eloquent\Model;
86+
use Devloops\LaravelTypesense\Interfaces\TypesenseSearch;
87+
use Laravel\Scout\Searchable;
88+
89+
class Post extends Model implements TypesenseSearch
90+
{
91+
use Searchable;
92+
93+
/**
94+
* Get the indexable data array for the model.
95+
*
96+
* @return array
97+
*/
98+
public function toSearchableArray()
99+
{
100+
$array = $this->toArray();
101+
102+
// Customize array...
103+
104+
return $array;
105+
}
106+
107+
public function getCollectionSchema(): array {
108+
return [
109+
'name' => $this->getTable(),
110+
'fields' => [
111+
[
112+
'name' => 'title',
113+
'type' => 'string',
114+
],
115+
[
116+
'name' => 'created_at',
117+
'type' => 'int32',
118+
],
119+
],
120+
'default_sorting_field' => 'created_at',
121+
];
122+
}
123+
124+
public function typesenseQueryBy(): array {
125+
return [
126+
'name',
127+
];
128+
}
129+
130+
}
131+
```
132+
133+
Then, sync the data with the search service like:
134+
135+
`php artisan scout:import App\\Post`
136+
137+
After that you can search your models with:
138+
139+
`Post::search('Bugs Bunny')->get();`
140+
141+
## Adding via Query
142+
The `searchable()` method will chunk the results of the query and add the records to your search index.
143+
144+
`$post = Post::find(1);`
145+
146+
// You may also add record via collection...
147+
`$post->searchable();`
148+
149+
// OR
150+
151+
`$posts = Post::where('year', '>', '2018')->get();`
152+
153+
// You may also add records via collections...
154+
`$posts->searchable();`
155+
156+
When using constraints apply it after the constraints are added to the query, as seen in the above example.
157+
158+
## OrderBy
159+
An `orderBy()` statement can now be applied to the search query similar to the `where()` statement.
160+
161+
When using constraints apply it after the constraints are added to the query, as seen in the above example.
162+
163+
## Author
164+
165+
- [Abdullah Al-Faqeir](https://github.com/abdullahfaqeir)
166+
167+
## License
168+
169+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

0 commit comments

Comments
 (0)